vim /usr/local/nginx/conf/vhost/test.com.conf//寫入如下内容
server
{
listen 80;
server_name test.com;
index index.html index.htm index.php;
root /data/wwwroot/test.com;
location /
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
auth_basic:定義使用者認證的名字
auth_basic_user_file:使用者名密碼檔案
• yum install -y httpd
• /usr/bin/htpasswd -c /usr/local/nginx/conf/htpasswd lsx //需要用到Apache的htpasswd工具。-c 後面指定生成密碼檔案的路徑
/usr/bin/htpasswd /usr/local/nginx/conf/htpasswd lshx //在次建立使用者不需要-c
New password:
Re-type new password:
cat /usr/local/nginx/conf/htpasswd
lsx:$apr1$/TT4ltP7$cAIoPauMzfObrxUoO.Lxv0
lshx:$apr1$u/csEPsG$S1dMwu2u.TRDTvl4x9VDv.
-t && -s reload //測試配置并重新加載
-s reload 的好處是修改的檔案有錯誤不會去破壞程式
測試:
mkdir /data/wwwroot/test.com
echo “test.com”>/data/wwwroot/test.com/index.html
curl -x192.168.211.152:80 wiki.com -I //狀态碼為401說明需要驗證
HTTP/1.1 401 Unauthorized
man curl
-u --user <user:password>
curl -uaming:passwd
編輯windows的hosts檔案,然後在浏覽器中通路test.com會有輸入使用者、密碼的彈窗
curl -ulsx:123 -x192.168.211.152:80 wiki.com -I //通路狀态碼變為200
HTTP/1.1 200 OK
以上是針對整站認證
下面針對站點下的某個目錄
location /admin/
curl -x192.168.211.152:80 wiki.com/lsx -I //這裡設定目錄lsx
echo "123">/data/wwwroot/test/lsx/index.html
curl -ulsx:123 -x192.168.211.152:80 test.com/lsx/index.html
123
本文轉自 蝦米的春天 51CTO部落格,原文連結:http://blog.51cto.com/lsxme/2057257,如需轉載請自行聯系原作者