我想在反向代理上的整個虛擬主機中新增存取控制。我正在使用 nginx 子請求身份驗證來執行此操作。預期的互動是,使用者將收到一條包含登入頁面連結的錯誤訊息,或在請求的 URL 處呈現登入頁面。登入過程完成後,應該有某種機制供使用者導航/重新載入最初請求的 URL。反向代理(即沒有 PHP)本身沒有腳本功能,這限制了透過身份驗證過程捕獲和傳播原始 URL 的選項。
我的期望:如果請求驗證失敗(即http://authprovider.example.com:8081/gateway/index.php返回 401) 我希望在請求的 URL 處返回特定內容,無需重定向且狀態為 4xx。
server {
listen 80;
server_name www.example.com;
root /var/www/html;
error_page 401 iprestricted.html;
## This provides feedback to the user when request is disallowed
## (including a link to login)
# location /iprestricted.html {
# try_files $uri $uri/ =404;
# }
# This implements the sub-request check....
location /restricted {
internal;
proxy_pass http://authprovider.example.com:8081/gateway/index.php ;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Original-URI $request_uri;
}
location / {
auth_request /restricted;
proxy_pass http://localwebserver/;
}
}
然而:
如果location /iprestricted.html{...}
被註解掉,我會得到一個重定向循環http://www.example.com
如果未註釋,則任何請求都會收到帶有 Location /iprestricted.html 的 302 回應,該回應傳回 200 狀態碼
如何實現不重定向的子請求認證?
是否有另一種方法可以捕獲原始 URL 並僅使用 nginx 配置將其傳播到身份驗證步驟?
我確實嘗試添加add_header WWW-Authenticate "Basic realm=bipdevtest";
上面的每個位置,但這並沒有在 HTTP 回應中發回。
答案1
總是一個好主意實時FM
如果 auth_request 提供者傳回,Nginx 將中繼 WWW-Authenticate 標頭(http://authprovider.example.com:8081/gateway/index.php在上面)。
不過,我將保留這個問題,因為我對導致 302 重定向和 200 回應的自訂錯誤頁面感到困惑/擔憂。