nginx http auth 请求模块只会 return 默认错误页面

nginx http auth request module will only return default error pages

我使用 nginx 作为整个解决方案的单一入口。

我的目标是在继续之前发送一些 url 进行身份验证。

我找到了一个名为:ngx_http_auth_request_module 的模块,它非常适合解决我的问题。

http://nginx.org/en/docs/http/ngx_http_auth_request_module.html

我正在这样编译我的 nginx:./configure --with-http_auth_request_module

我的代码如下所示:

http {

    server {
        listen       8080 default_server;

        server_name _;

        location /api {
            auth_request /auth;
            proxy_pass  http://localhost:8000;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }

        location = /auth {
            proxy_pass http://localhost:8000/auth/user;
            proxy_pass_request_body off;
            proxy_set_header Content-Length "";
            proxy_set_header X-Original-URI $request_uri;
        }

    }
}

我的问题是,如果我的服务器 return 出现错误,例如 401,则会显示默认页面。我想要的是能够 return 从我的身份验证服务中 return 编辑的 json。没有这个就没用了。只显示通用 401 页面有什么意义?我想 return 我的代理 json 回复。

请帮忙。

auth_request 仅用于身份验证。这个小技巧应该适合你

error_page 401 /auth;

验证错误后,它将再次转到 /auth 位置,这次是作为普通请求。