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

Open
#25 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
38/100
Issue type
Documentation
Clarity
Mostly clear
Activity status
Stale
Tech stack
nginx
Domain
devops

Research direction

Start with the nginx configuration in the issue, especially proxy_set_header, proxy_intercept_errors, recursive_error_pages, error_page, and the @handle_redirect location. Check the repository's existing tip format and placement before documenting this example. Done means the setup is recorded clearly with the multi-redirect behavior and required directives explained.

Written by the indexing model from the issue text.

Description

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;
   }
}
Dominant language
Python
Stars
5
Forks
2
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from davideuler/programming-tips

All issues in davideuler/programming-tips

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.