Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to set up nginx proxy_pass to follow upstream 302 redirects with multiple redirects #25

Open
davideuler opened this issue Jul 22, 2022 · 0 comments

Comments

@davideuler
Copy link
Owner

davideuler commented Jul 22, 2022

The following config shows how to set up nginx proxy to follow upstream 302 redirects.
The upstream server only receive hostname as api.upstream.com, so proxy_set_header directive is necessary.
And the upstream has upstreams recursively, so need to enable recursive_error_pages.

Then the client would get normal 200 response instead of 302 from nginx.


server {
    listen               80 myserver.com;

     # redirect health status page without Host header:
     location /health.status {
       proxy_pass   http://api.upstream.com;
       proxy_redirect http://api.upstream.com/ /;
       proxy_intercept_errors on;
       recursive_error_pages on;
      error_page 301 302 307 = @handle_redirect;
    }


    location / {
        #proxy_pass   http://127.0.0.1:8080;
        proxy_pass   http://api.upstream.com;

        ## set Host to target server host name
        proxy_set_header Host "api.upstream.com";
        proxy_redirect http://api.upstream.com/ /;

        proxy_intercept_errors on;
        recursive_error_pages on;
        error_page 301 302 307 = @handle_redirect;

    }

   location @handle_redirect {
       set $original_uri $uri;
       set $orig_loc $upstream_http_location;

       # nginx goes to fetch the value from the upstream Location header
       proxy_pass $orig_loc;
   }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant