nginx用户认证配置( Basic HTTP authentication)
ngx_http_auth_basic_module模块实现让访问着,只有输入正确的用户密码才允许访问web内容。web上的一些内容不想被其他人知道,但是又想让部分人看到。nginx的http auth模块以及Apache http auth都是很好的解决方案。
默认情况下nginx已经安装了ngx_http_auth_basic_module模块,如果不需要这个模块,可以加上 –without-http_auth_basic_module 。
nginx basic auth指令
语法: auth_basic string | off;
默认值: auth_basic off;
配置段: http, server, location, limit_except
默认表示不开启认证,后面如果跟上字符,这些字符会在弹窗中显示。
语法: auth_basic_user_file file;
默认值: —
配置段: http, server, location, limit_except
用户密码文件,文件内容类似如下:
ttlsauser1:password1
ttlsauser2:password2:comment
nginx认证配置实例
?
1
2
3
4
5
6
7
8
9
10
11
12
13
server{
server_name www.tuohang tuohang;
index index.html index.php;
root /data/site/www.tuohang;
location /
{
auth_basic “nginx basic http test for tuohang”;
auth_basic_user_file conf/htpasswd;
autoindex on;
}
}
备注:一定要注意auth_basic_user_file路径,否则会不厌其烦的出现403。
生成密码
可以使用htpasswd,或者使用openssl
?
1
2
3
# printf “ttlsa:$(openssl passwd -crypt 123456)\n” >>conf/htpasswd
# cat conf/htpasswd
ttlsa:xyJkVhXGAZ8tM
账号:ttlsa
密码:123456
reload nginx
?
1
# /usr/local/nginx-1.5.2/sbin/nginx -s reload
效果如下:
完成~
Nginx阻止用户代理
些时候,需要阻止某些用户代理访问网站,比如ab,wget,curl等等,这就需要使用到$http_user_agent变量。
修改nginx.conf
?
1
2
3
4
5
6
7
if ($http_user_agent ~* (Wget|ab) ) {
return 403;
}
if ($http_user_agent ~* LWP::Simple|BBBike|wget) {
return 403;
}
重启nginx
?
1
# /usr/local/nginx-1.7.0/sbin/nginx -s reload
声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。