I have a NGINX reverse proxy also serving static content on / with following config
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;
}
The goal is to check the user in authenticated by subrequest to /authn endpoint, which will return 302 and Location header if user is not. But client gets 500 from NGINX with error log like
auth request unexpected status: 500 while sending to client
I also have a /root endpoint which poxy-passes directly to /authn gateway which correctly redirects to login page and authenticates client. I've tried instead of handling redirects in subrequests proxy-pass requests to this endpoint with setting
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;
But in this case I get 500 and
auth request unexpected status: 302 while sending to client
in NGINX error.log
Why NGINX doesn't handle redirects correctly with this settings and how to correctly solve this?