Nginx下的配置也挺方便的,我們可以沿用由Apache的htpasswd模塊生成的.htpasswd文件作爲密碼文件。注意,nginx的http auth basic的密碼是用crypt(3)加密的,而apache是md5加密。所以生成時:
-
/ usr / local / apache2 / bin / htpasswd -c -d pass_file用戶名
-
#回車輸入密碼,-c表示生成文件,-d根據crypt加密。
對於lnmp用戶,一般不安裝apache,說下怎麼在nginx下生成htpasswd
下載這個python文件:http : //trac.edgewall.org/export/10770/trunk/contrib/htpasswd.py (nginx Wiki裏推薦的)
運行示例
-
chmod 777 htpasswd.py
-
./htpasswd.py -c -b htpasswd用戶名密碼
-
#-c爲生成文件htpasswd爲文件名
我們將這個htpasswd文件放到nginx / conf下,記得chmod 400 htpasswd來保護一下。
然後修改nginx.conf:
-
服務器{
-
server_name d8.neolee.com;
-
root / var /www/d8.neolee.com;
-
包括 / etc / nginx / fastcgi_php;
-
位置 / {
-
auth_basic “請輸入密碼” ;
-
auth_basic_user_file / usr / local / nginx / conf / htpasswd;
-
索引index.php;
-
如果(!-e $ request_filename){
-
最後重寫^(。*)$ /index.php;
-
}
-
}
-
}
加入了
-
auth_basic “請輸入密碼” ;
-
auth_basic_user_file / usr / local / nginx / conf / htpasswd;
重啓nginx即可。