惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

阮一峰的网络日志
阮一峰的网络日志
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Schneier on Security
The Last Watchdog
The Last Watchdog
Cyberwarzone
Cyberwarzone
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cyber Attacks, Cyber Crime and Cyber Security
L
Lohrmann on Cybersecurity
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
The Cloudflare Blog
V
V2EX
博客园_首页
博客园 - 聂微东
Vercel News
Vercel News
人人都是产品经理
人人都是产品经理
G
GRAHAM CLULEY
T
Tenable Blog
Last Week in AI
Last Week in AI
Y
Y Combinator Blog
L
LINUX DO - 最新话题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
SecWiki News
SecWiki News
博客园 - 三生石上(FineUI控件)
S
Secure Thoughts
N
News | PayPal Newsroom
T
The Blog of Author Tim Ferriss
The GitHub Blog
The GitHub Blog
T
Troy Hunt's Blog
博客园 - 【当耐特】
Forbes - Security
Forbes - Security
H
Hacker News: Front Page
A
About on SuperTechFans
B
Blog RSS Feed
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
罗磊的独立博客
D
DataBreaches.Net
P
Privacy & Cybersecurity Law Blog
Schneier on Security
Schneier on Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Google DeepMind News
Google DeepMind News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Jina AI
Jina AI
D
Docker
P
Proofpoint News Feed

Rat's Blog - Nginx

使用Nginx反向代理,自建CDN加速节点 - Rat's Blog LNMP环境下,利用Nginx反代Google网站的方法 - Rat's Blog Nginx环境下对部分网站做防盗链设置及外链的跳转 - Rat's Blog CentOS 7安装配置Nginx 1.10、PHP 5.6、MySQL 5.7教程 利用Nginx反向代理来简单镜像HTTP(S)网站的方法 - Rat's Blog LNMP环境下使用CDN后获取访客真实IP的方法 - Rat's Blog Nginx环境使用auth_basic密码保护wordpress后台登录界面 - Rat's Blog Nginx环境开启ssl后强制https 301全部指向www的方法 - Rat's Blog 防止Linux VPS主机Nginx环境根目录被解析的方法 - Rat's Blog
Nginx给网站添加用户认证配置( Basic HTTP authentication) - Rat's Blog
博主: Rat's · 2017-06-11 · via Rat's Blog - Nginx

说明:ngx_http_auth_basic_module模块实现让访问者只有输入正确的用户密码才允许访问web内容。web上的一些内容不想被其他人知道,但是又想让部分人看到。nginxhttp auth模块以及Apache http auth都是很好的解决方案。

请输入图片描述
这里以军哥的LNMP为例,默认情况下nginx已经安装了ngx_http_auth_basic_module模块。

Nginx认证配置实例

1、生成认证文件

# printf "test:$(openssl passwd -crypt 123456)\n" >>/home/htpasswd
# cat /home/htpasswd 
test:xyJkVhXGAZ8tM

注意:这里账号:test,密码:123456,记住认证文件路径

2、配置网站conf文件

server{
       listen 80;
       server_name  www.moerats.com moerats.com;
       index index.html index.php;
       root /home/wwwroot/www.moerats.com;       
       location /
       {
                auth_basic "Please enter your username and password";
                auth_basic_user_file /home/htpasswd; 
                autoindex on;
       }
}

注意:一定要注意auth_basic_user_file路径,否则会不厌其烦的出现403。

3、重启Nginx

/etc/init.d/nginx restart

LNMP下为Nginx目录设置访问验证的用户名密码

有时候需要象Apache那样为指定的目录添加访问验证,一般在Apache下使用htpasswd来添加,而htpasswd是包含在apache2-utils里,一般LNMP一键安装包或自己编译安装LNMP都不会安装apache2-utils

1、创建类htpasswd文件
执行下面命令:

wget -c https://www.moerats.com/usr/down/htpasswd.sh;bash htpasswd.sh

按提示输入用户名、密码、及认证文件名。脚本会自动生成认证文件。记录下脚本返回的文件路径。如:/usr/local/nginx/conf/vpser.net.auth

2、为Nginx添加auth认证配置
下面是以某域名下面的soft目录为例,在域名的server段里加上如下代码:

location ^~ /soft/
{
auth_basic "Authorized users only";
auth_basic_user_file 这里写前面脚本返回的文件路径;
}

Authorized users only为提示信息,可以修改成自己想让他提示的信息;auth_basic_user_file后面需要填htpasswd.sh脚本返回的人家文件的路径。按上面的提示修改好配置后,重启nginx,访问http://yourdomainname/soft/ 就会提示输入用户名和密码。

注意:加上认证之后该目录下的PHP将不会被解析,会出现下载提示,如果想可以解析PHP可以将上面的配置改为:

location ^~ /soft/ {
    location ~ .*\.(php|php5)?$ {
        fastcgi_pass unix:/tmp/php-cgi.sock;
        fastcgi_index index.php;
        include fastcgi.conf;
    }
    auth_basic "Authorized users only";
    auth_basic_user_file 这里写前面脚本返回的文件路径;
}

本教程适合LNMP一键安装包或自己安装的LNMP,只不过目录和配置文件可能位置不一样。

设置完执行:/usr/local/nginx/sbin/nginx -t测试配置是否有错误。
再执行:/usr/local/nginx/sbin/nginx -s reload载入配置文件。

部分参考:https://www.vpser.net/build/nginx-htpasswd.html


版权声明:本文为原创文章,版权归 Rat's Blog 所有,转载请注明出处!

本文链接:https://www.moerats.com/archives/171/

如教程需要更新,或者相关链接出现404,可以在文章下面评论留言。