1.有時我們web服務器上的某個文件夾只允許特定人員訪問,這時我們需要在nginx配置文件中配置該文件夾的訪問權限。
2.生成用戶名單
在nginx中我們使用htpasswd來生成用戶名單
下載這個python文件:http://trac.edgewall.org/export/10770/trunk/contrib/htpasswd.py (nginx wiki里推薦的)
運行示例:
chmod 777 htpasswd.py ./htpasswd.py -c -b htpasswd username password
#-c為生成文件 htpasswd為文件名
nginx 的 http auth basic 的密碼是用 crypt(3) 加密的
我們把生成的htpasswd文件放到/etc/nginx目錄中,修改權限chmod 400 htpasswd來保護一下該文件。
3.修改nginx配置文件
server {
server_name www.test.com;
root /usr/share/nginx/html;
location /devdoc {
autoindex on;#顯示文件列表
index index.html index.htm;#默認首頁
charset utf-8;#編碼
auth_basic "Restricted";#訪問權限類型
auth_basic_user_file /etc/nginx/htpasswd;#用戶名單
}
}
重啟nginx即可。訪問網站www.test.com/devdoc,需要輸入我們設置的用戶名密碼登錄才能訪問文件。如下圖所示: