nginx用戶認證配置( Basic HTTP authentication)

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

nginx basic auth指令

語法:     auth_basic string | off;
默認值:     auth_basic off;
配置段:     http, server, location, limit_exceptnginx

默認表示不開啓認證,後面若是跟上字符,這些字符會在彈窗中顯示。web

語法:     auth_basic_user_file file;
默認值:     —
配置段:     http, server, location, limit_exceptbash

用戶密碼文件,文件內容相似以下:spa

ttlsauser1:password1
ttlsauser2:password2:comment

nginx認證配置實例

server{
       server_name  www.ttlsa.com ttlsa.com;

        index index.html index.<a href="http://www.ttlsa.com/php/" title="php"target="_blank">php</a>;
        root /data/site/www.ttlsa.com;       

        location /
        {
                auth_basic "nginx basic http test for ttlsa.com";
                auth_basic_user_file /data/webserver/nginx/conf/htpasswd; 
                autoindex on;
        }
}

備註:必定要注意auth_basic_user_file路徑,不然會不厭其煩的出現403。code

生成密碼server

能夠使用htpasswd,或者使用opensslhtm

# printf "ttlsa:$(openssl passwd -crypt 123456)\n" >>conf/htpasswd
# cat conf/htpasswd 
ttlsa:xyJkVhXGAZ8tM

帳號:ttlsa
密碼:123456blog

reload nginx

/data/webserver/nginx/sbin/nginx -s reload

效果以下:

Apache

http_auth_basic_module

完成~

相關文章
相關標籤/搜索