2017-02-19 68 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好的,我会检查并回复你 –