gpt4 book ai didi

Nginx - 如何在 auth_request 返回 401 后重定向到登录页面

转载 作者:行者123 更新时间:2023-12-01 21:26:05 27 4
gpt4 key购买 nike

我做 auth_request/auth;configuring-subrequest-authentication/ 中所述

如果/auth 返回 401 或 403,我如何重定向到登录页面?

我试过

error_page 401 403 = @error401;

location = ^/securedUrl {
add_header dbg-header dbg_ws_3;
auth_request /auth;
proxy_pass http://auth-module:8080;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass_header x-remaining-session-time;
}

location @error401 {
return 302 /login-module/login;
}

location = /auth {
internal;
rewrite ^/(.*) /is-authorized break;
proxy_pass http://auth-module:8080;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Original-URI $request_uri;
}

但是它不起作用,我在浏览器中得到 401

最佳答案

当使用代理请求时,你应该设置“proxy_intercept_errors on;”在该位置。像这样:


location = ^/securedUrl {
add_header dbg-header dbg_ws_3;
auth_request /auth;
proxy_pass http://auth-module:8080;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_pass_header x-remaining-session-time;

}

location @error401 {
return 302 /login-module/login;
}


location = /auth {
internal;
rewrite ^/(.*) /is-authorized break;
proxy_pass http://auth-module:8080;
proxy_pass_request_body off;
proxy_set_header Content-Length "";
proxy_set_header X-Original-URI $request_uri;
proxy_intercept_errors on;
error_page 401 = @error401;
}

关于Nginx - 如何在 auth_request 返回 401 后重定向到登录页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63173650/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com