Skip to content

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

@davideuler

Description

@davideuler

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;
   }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions