1. 程式人生 > 其它 >Nginx 認證模組

Nginx 認證模組

# Nginx使用者認證模組
# 主要應用業務:檔案下載。當用戶需要下載某些檔案的時候,我們增加使用者名稱和密碼機制來進行使用者驗證。
# 該功能的實現是通過ngx_http_auth_basic_module模組來實現的
# 預設情況是已經安裝了的,如果不需要使用就在configure的時候加上--without-http_auth_basic_module

# auth_basic指令:使用“http基本認知”協議啟用使用者名稱和密碼的驗證
# 開啟後,服務端會返回401,指定的字串會返回到客戶端給使用者提示資訊,但是不同的瀏覽器顯示內容不一致。
# 語法:auth_basic string:off;
# 預設值: off; # 位置:http、server、location、limit_except # auth_basic_user_file:指定使用者名稱和密碼所在檔案 # 語法:auth_basic_user_file file; # 預設值:無 # 位置:http、server、location、limit_except # 例子: location /download { root /usr/local; autoindex on; autoindex_exact_size on; autoindex_format html; autoindex_localtime on; auth_basic
'Please input your auth'; auth_basic_user_file htpasswd; } # 使用者名稱和密碼的檔案怎麼生成: # 1.安裝生成工具httpd-tools yum install -y httpd-tools # 2.建立一個新檔案,記錄使用者名稱和密碼 htpasswd -c /usr/local/nginx/conf/htpasswd username # 3.在指定檔案新增一個使用者名稱和密碼 htpasswd -b /usr/local/nginx/conf/htpasswd username password # 4.刪除指定檔案中的某個使用者 htpasswd -D /usr/local/nginx/conf/htpasswd username
# 5.驗證使用者名稱和密碼是否正確 htpasswd -v /usr/local/nginx/conf/htpasswd username