2017-02-19 64 views
2

我試圖找到一種方法來重定向用戶到不同的位置,如果他提供無效的憑據,或根本沒有憑據。NGINX - 重定向驗證失敗

我的配置是這樣的:

server { 
    listen   9999; 

    auth_basic  "Authentication Required"; 
    auth_basic_user_file passwords; 

這將導致用戶的瀏覽器不斷地提示輸入憑據,直到他取消提示 - 它返回一個401錯誤。

如果認證失敗(無論出於何種原因),我想將用戶重定向到不同的頁面 - NGINX可以這麼做嗎?

+0

是的。有可能的。請檢查我添加的配置。 –

+0

有一個小錯誤。請立即檢查。我正在更新我的答案。 –

回答

0

工作正常。 只要確保您的placeholder.html存在。否則它將通過404.

server { 
     listen 80; 
     server_name testing.com; 

     root /var/www/; 

    location/{ 
     index index.html index.htm; 
      auth_basic "Restricted"; 
      auth_basic_user_file /etc/nginx/.htpasswd; 
    } 
    error_page 401 /placeholder.html; 
    location = /placeholder.html { 
       auth_basic off; 
     } 

} 
+0

這會導致「重定向太多」錯誤。我認爲401的問題在於nginx * always *將返回401給瀏覽器,直到用戶輸入正確的憑證或發送空的授權標頭爲止。 –

+0

@DanMarkhasin好的,我會檢查並回復你 –