【问题标题】:nginx auth_request error handling (unset error_page?)nginx auth_request 错误处理(未设置 error_page?)
【发布时间】:2018-04-15 12:12:59
【问题描述】:

使用 nginx auth_request 时,只接受 4 个返回码:2xx401403500。处理这些需要设置error_page。这对auth_request 来说很好,但是所有返回这些代码的下游应用程序最终都将使用相同的error_page 处理方式。

示例

location /test {
  auth_request /foo;
  error_page 500 /its-broke.html;
  proxy_pass http://bar;
}

如果 auth_request/foo 返回 500,则显示 its-broke.html。这是我想要的行为。但是,如果 bar 返回 500 its-broke.html 也会显示。我不想要这个。相反,我希望将bar500 error 响应传输到浏览器。

auth_request 将捕获任何5xx 并将其作为500 处理,所以我不能只让foo 返回520。这会导致以下错误,并且无论如何都会返回500

auth request unexpected status: 520 while sending to client

有什么想法可以解决这个问题吗?我也找不到在代理通过之前取消设置 error_page 的方法。

【问题讨论】:

    标签: nginx auth-request


    【解决方案1】:

    你可以通过一些努力做到这一点

    events {
        worker_connections 1024;
    }
    http {
    
        server {
    
            listen 80;
    
            location @autherror {
    
                return 500 "Auth returned 500";
            }
    
            location @proxy_api {
    
                proxy_pass http://127.0.0.1:8081;
            }
    
            location / {
    
                location /test {
    
                    auth_request /auth;
                    error_page 500 = @autherror;
                    try_files dummy_non_existing_url = @proxy_api;
                }
    
            }
            location /auth {
    
                return 200 "auth passed";
            }
    
        }
    
        server {
    
            listen 8081;
            location / {
                return 200 "You came to $uri";
            }
            location /test2 {
                return 500 "Error from API";
            }
        }
    }
    

    API 测试的结果是

    $ curl localhost/test
    You came to /test
    
    $ curl localhost/test2
    Error from API
    

    现在如果我将 return 200 "auth passed"; 更改为 return 500 "auth failed"; 以模拟身份验证 500 错误,我会得到

    $ curl localhost/test
    Auth returned 500
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-12
      相关资源
      最近更新 更多