可以使用基本和消化身份驗證方法在NGINX中實現HTTP身份驗證。這是有關如何設置它們的分步指南:
基本身份驗證:
創建一個密碼文件:首先,您需要創建一個包含用戶名和密碼的文件。使用htpasswd
命令創建和管理此文件。
<code>sudo htpasswd -c /etc/nginx/.htpasswd username</code>
這將提示您輸入指定用戶的密碼。無需-c
標誌即可添加其他用戶。
配置NGINX:修改您的Nginx配置文件以包括身份驗證詳細信息。將以下內容添加到您的服務器或位置塊:
<code class="nginx">location /protected/ { auth_basic "Restricted Area"; auth_basic_user_file /etc/nginx/.htpasswd; }</code>
這將需要身份驗證以訪問/protected/
Directory。
重新啟動NGINX:進行更改後,重新啟動NGINX應用新配置:
<code>sudo systemctl restart nginx</code>
摘要身份驗證:
創建一個密碼文件:類似於基本驗證,您需要一個密碼文件。您可以使用htdigest
之類的工具來創建它:
<code>sudo htdigest -c /etc/nginx/.htdigest "Realm Name" username</code>
用所需的領域名稱替換“領域名稱”。
配置Nginx: Digest Auth需要ngx_http_auth_digest_module
,可能不包含在nginx的默認構建中。如果有的話,請將NGINX配置如下:
<code class="nginx">location /protected/ { auth_digest "Restricted Area"; auth_digest_user_file /etc/nginx/.htdigest; }</code>
基本和消化身份驗證都有其自身的安全含義:
基本身份驗證:
摘要身份驗證:
比較:
NGINX中的身份驗證領域用於分組需要以通用名稱身份驗證的資源。這可以幫助更好的用戶管理,並為用戶提供有關他們正在訪問的內容的清晰背景。這是如何配置nginx以使用身份驗證領域:
帶有領域的基本身份驗證:
<code class="nginx">location /protected/ { auth_basic "Restricted Area"; # This is the realm name auth_basic_user_file /etc/nginx/.htpasswd; }</code>
引號中的文本是在身份驗證提示期間將顯示給用戶的領域名稱。
使用領域的消化身份驗證:
<code class="nginx">location /protected/ { auth_digest "Restricted Area"; # This is the realm name auth_digest_user_file /etc/nginx/.htdigest; }</code>
與基本驗證相似,引號中的文字是境界名稱。
多個領域:
您可以為不同位置設置不同的領域,以管理對服務器不同部分的訪問。
<code class="nginx">location /admin/ { auth_basic "Admin Area"; auth_basic_user_file /etc/nginx/.htpasswd_admin; } location /user/ { auth_basic "User Area"; auth_basic_user_file /etc/nginx/.htpasswd_user; }</code>
此示例為管理員和用戶區域使用不同的領域和不同的密碼文件,從而增強用戶管理。
雖然NGINX不本質地支持在同一位置塊中組合基本和消化身份驗證,但您可以通過使用不同的身份驗證方法設置單獨的位置來實現增強安全性的形式。這是您可以配置它的方法:
較不敏感領域的基本授權:
<code class="nginx">location /less_sensitive/ { auth_basic "Less Sensitive Area"; auth_basic_user_file /etc/nginx/.htpasswd_less_sensitive; }</code>
對更敏感領域的Digest auth:
<code class="nginx">location /more_sensitive/ { auth_digest "More Sensitive Area"; auth_digest_user_file /etc/nginx/.htdigest_more_sensitive; }</code>
後備身份驗證:
如果您希望用戶具有訪問內容的後備方法,則可以使用替代身份驗證方法設置單獨的位置:
<code class="nginx">location /fallback/ { auth_basic "Fallback Area"; auth_basic_user_file /etc/nginx/.htpasswd_fallback; }</code>
儘管該設置在技術上不從技術上結合在同一位置中的兩種方法,但它允許您利用服務器不同區域的基本和消化身份驗證的優勢,從而通過基於數據的靈敏度提供適當的身份驗證機制來增強安全性。
以上是如何在NGINX中實現HTTP身份驗證(基本auth,imigest auth)?的詳細內容。更多資訊請關注PHP中文網其他相關文章!