ngx_http_auth_basic_module模块实现让访问着,只有输入正确的用户密码才容许访问web内容。web上的一些内容不想被其余人知道,可是又想让部分人看到。nginx的http auth模块以及Apache http auth都是很好的解决方案。php
默认状况下nginx已经安装了ngx_http_auth_basic_module模块,若是不须要这个模块,能够加上 --without-http_auth_basic_module 。html
语法: auth_basic string | off;
默认值: auth_basic off;
配置段: http, server, location, limit_exceptnginx
默认表示不开启认证,后面若是跟上字符,这些字符会在弹窗中显示。web
语法: auth_basic_user_file file;
默认值: —
配置段: http, server, location, limit_exceptide
用户密码文件,文件内容相似以下:spa
1
2
|
ttlsauser1:password1
ttlsauser2:password2:comment
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
server{
server_name www.ttlsa.com ttlsa.com;
index index.html index.php;
root /data/site/www.ttlsa.com;
location /
{
auth_basic "nginx basic http test for ttlsa.com";
auth_basic_user_file conf/htpasswd;
autoindex on;
}
}
|
备注:必定要注意auth_basic_user_file路径,不然会不厌其烦的出现403。code
生成密码server
能够使用htpasswd,或者使用opensslhtm
1
2
3
|
# printf "ttlsa:$(openssl passwd -crypt 123456)\n" >>conf/htpasswd
# cat conf/htpasswd
ttlsa:xyJkVhXGAZ8tM
|
帐号:ttlsa
密码:123456blog
reload nginx
1
|
# /usr/local/nginx-1.5.2/sbin/nginx -s reload
|
效果以下:
完成~