[轉載] Nginx中使用htpasswd配置Http認證
·轉載· Nginx中使用htpasswd配置Http認證
轉載博主的版權申明:
版權宣告:本文為博主原創文章,轉載請註明出處。如有錯誤,歡迎大家批評指正。 https://blog.csdn.net/a_bang/article/details/72630578
為了增強網站的安全性,可以通過設定HTTP認證的方式實現,而nginx的ngx_http_auth_basic_module模組為我們提供了方便。訪問者在瀏覽默寫模組或者網頁的時候,只有通過認證才能正常顯示,否則會報401 Authorization Required,認證錯誤時會報403。如下圖:
這裡寫圖片描述
下面來說一下實現方式:
安裝httpd
yum -y install httpd
建立認證資料檔案
[[email protected] nginx]# htpasswd -c /usr/opt/nginx/passwd.db root
New password:
Re-type new password:
Adding password for user root
[[email protected] nginx]# cat passwd.db
root:14W4BSbnMrVEg
[[email protected] nginx]#
執行上面語句,如上下圖所示,密碼經過加密後儲存在passwd.db檔案中。
nginx中配置auth_basic 和auth_basic_user_file
這裡寫圖片描述
重啟nginx
[[email protected] nginx]# nginx -t //檢查配置是否正常
nginx: the configuration file /usr/opt/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/opt/nginx/nginx.conf test is successful
[[email protected] nginx]# nginx -s reload //平滑重啟
開啟網頁,就會看到最開始的效果圖。
nginx basic auth
主要包含兩個值:auth_basic 和auth_basic_user_file
auth_basic
語法: auth_basic string | off;
預設值: auth_basic off;
配置段: http, server, location, limit_except
預設表示不開啟認證,後面如果跟上字元,這些字元會在彈窗中顯示。
auth_basic_user_file
語法: auth_basic_user_file file;
預設值: —
配置段: http, server, location, limit_except
htpasswd
基本格式:
htpasswd [-cmdpsD] passwordfile username
htpasswd -b[cmdpsD] passwordfile username password
htpasswd -n[mdps] username
htpasswd -nb[mdps] username password
引數說明:
b
使用批處理方式,從命令列中獲得密碼,而不顯示提示要求輸入。 此選項的使用應該極為謹慎,因為命令列中的密碼是清晰可見的。
c
建立passwdfile檔案。如果passwdfile已經存在,則它被重寫並截斷。 此選項不能與-n選項同時使用。
n
在標準輸出裝置上顯示結果,而不更新檔案。 用於生成可以為Apache非文字輸出儲存格式所接受的密碼記錄。 此選項在命令列中的語法有所改變,因為passwdfile引數(通常是第一個)被省略了。 此選項不能與-c選項同時使用。
m
使用MD5加密密碼。在Windows, Netware 和TPF上,這是預設的。
d
使用crypt()加密密碼。在除了Windows, Netware和TPF的平臺上(比如linux),這是預設的。 雖然它在所有平臺上可以為htpasswd所支援, 但是在Windows, Netware和TPF上不能為httpd伺服器所支援。
s
使用SHA加密密碼。 它是為了方便轉入或移植到使用LDAP Directory Interchange Format (ldif)的Netscape而設計的。
p
使用純文字的密碼。雖然在所有平臺上htpasswd都可以建立這樣的密碼, 但是httpd後臺只在Windows, Netware和TPF上支援純文字的密碼。
passwdfile
包含使用者名稱和密碼的檔案的名稱。 如果使用了-c,而此檔案不存在則建立,如果已經存在,則重寫並截斷此檔案。
username
需要在passwdfile中建立或更新的使用者名稱。 如果此檔案中username不存在,則增加一項,如果已經存在,則改變其密碼。
password
將被加密並存儲到檔案中的純文字的密碼。僅用於和-b選項同時使用。
注:htpasswd所管理的檔案可以包含兩種型別的密碼,有些使用者的密碼使用MD5加密的,而同一個檔案中的其他使用者是用crypt()加密的。
---------------------
作者:a_bang
來源:CSDN
原文:https://blog.csdn.net/a_bang/article/details/72630578
版權宣告:本文為博主原創文章,轉載請附上博文連結!