1. 程式人生 > >Linux- Nginx用戶認證

Linux- Nginx用戶認證

tex .com ges proc mar echo 需要 size col

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; #用戶名密碼所在路徑
}
}

? yum install -y httpd

? htpasswd -c /usr/local/nginx/conf/htpasswd aming #-c 生成這個文件,並指定aming用戶,創建第二個時,就不要-c
技術分享圖片
? -t && -s reload //測試配置並重新加載 #當你的配置文件有錯誤時,實際不會生效,
技術分享圖片

? mkdir /data/wwwroot/test.com
? echo “test.com”>/data/wwwroot/test.com/index.html
? curl -x127.0.0.1:80 test.com -I//狀態碼為401說明需要驗證
? curl -uaming:passwd 訪問狀態碼變為200

? 編輯windows的hosts文件,然後在瀏覽器中訪問test.com會有輸入用戶、密碼的彈窗
? 針對目錄的用戶認證
location /admin/
{
auth_basic "Auth";
auth_basic_user_file /usr/local/nginx/conf/htpasswd;
}
訪問某個目錄
技術分享圖片
針對某個文件
技術分享圖片

Linux- Nginx用戶認證