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
用戶密碼文件,文件內容類似如下:
1 2 |
ttlsauser1:password1 ttlsauser2:password2:comment |
nginx認證配置實例
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。
生成密碼
可以使用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 |
效果如下:
http_auth_basic_module
完成~