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
|
效果以下:
完成~