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

Nginx-如何在auth_请求返回401后重定向到登录页面,nginx,nginx-reverse-proxy,Nginx,Nginx Reverse Proxy,我做auth\u请求/auth 如中所述 如果/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_ve

我做
auth\u请求/auth
如中所述

如果/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\u intercept\u 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;
}

使用代理请求时,应在位置中设置“proxy\u intercept\u 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;
}