Tom*_*wer 5 redirect oauth nginx
我有一个 NGINX 反向代理,也使用以下配置在 / 上提供静态内容
location / {
auth_request /authn;
proxy_intercept_errors on;
recursive_error_pages on;
error_page 301 302 303 307 308 = @handle_redirect;
gzip_static on;
index index.html;
root /usr/share/nginx/html;
try_files $uri $uri/ @index;
}
location /authn {
set $target http://gateway:8030/authn;
proxy_pass http://gateway:8030/authn;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Original-URI $request_uri;
proxy_pass_request_headers on;
proxy_intercept_errors on;
recursive_error_pages on;
error_page 301 302 303 307 308 @handle_redirect;
}
location @handle_redirect {
proxy_set_header Host $host:$server_port;
set $redirect_url $upstream_http_location;
proxy_pass $redirect_url;
}
Run Code Online (Sandbox Code Playgroud)
目标是通过对 /authn 端点的子请求进行身份验证来检查用户,如果用户不是,它将返回 302 和 Location 标头。但是客户端从 NGINX 获得 500 错误日志,例如
auth request unexpected status: 500 while sending to client
Run Code Online (Sandbox Code Playgroud)
我还有一个 /root 端点,它直接通过 poxy 传递到 /authn 网关,该网关正确重定向到登录页面并验证客户端。我已经尝试过使用设置而不是处理子请求中的重定向代理传递请求到此端点
location /root {
set $target http://top-gateway:8030/authn;
proxy_pass http://top-gateway:8030/authn;
}
location /authn {
...
error_page 301 302 303 307 308 /root;
Run Code Online (Sandbox Code Playgroud)
但在这种情况下,我得到 500 和
auth request unexpected status: 302 while sending to client
Run Code Online (Sandbox Code Playgroud)
在 NGINX 错误日志中
为什么 NGINX 无法使用此设置正确处理重定向以及如何正确解决此问题?
也许这对您有用,以便了解它为什么失败,也许我迟到了,但我发现这个线程可以做类似的事情。
该链接显示了 Nginx 开发人员和有动机编写补丁的用户之间的对话。看来Nginx只支持auth_request的2xx和4xx代码。但是,如果您按照该链接中所示控制应用程序,则可以执行类似的操作。
location / {
auth_request /auth;
auth_request_set $auth_redirect $upstream_http_location;
error_page 401 = /auth_redirect;
}
location /auth {
proxy_pass http://auth_backend;
...
}
location /auth_redirect {
return 302 $auth_redirect;
}
Run Code Online (Sandbox Code Playgroud)
另外,读这篇文章对我很有用(不一样,但提供了一些想法的线索):
此链接显示了有关 auth_request_set 的简单介绍(您可以在文档中找到它)。
auth_request_set $x_upstreamhost $upstream_http_x_upstreamhost;
Run Code Online (Sandbox Code Playgroud)
另外,这里是 auth_request 的代码实现的深层次。
https://www.nginx.com/resources/wiki/extending/examples/auth_request/
此链接显示了 auth_request 实现的代码分解。我不能把它包括在这里。
干杯,