預設情況下nginx已經安裝了ngx_http_auth_basic_module子產品,如果不需要這個子產品,可以加上 --without-http_auth_basic_module 。
文法: auth_basic string | off;
預設值: auth_basic off;
配置段: http, server, location, limit_except
預設表示不開啟認證,後面如果跟上字元,這些字元會在彈窗中顯示。
文法: auth_basic_user_file file;
預設值: —
使用者密碼檔案,檔案内容類似如下:
1
2
ttlsauser1:password1
ttlsauser2:password2:comment
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
# printf "ttlsa:$(openssl passwd -crypt 123456)\n" >>conf/htpasswd
# cat conf/htpasswd
ttlsa:xyJkVhXGAZ8tM
賬号:ttlsa
密碼:123456
reload nginx
# /usr/local/nginx-1.5.2/sbin/nginx -s reload
效果如下:
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiInBnauADNwcTM1QGNjRjNwUGZidDO4QzYiJTMidDMwIGMjFzNfdWbp9CXt92Yu4GZjlGbh5SZslmZxl3Lc9CX6MHc0RHaiojIsJye.jpg)
http_auth_basic_module
完成~