How to follow HTTP redirects inside nginx?
Solution 1:
I believe you want the variable $upstream_http_location.
Variables that begin with $proxy* control what goes from nginx to the upstream. The $upstream* series of variables contains information about the response that nginx itself receives. You can get any aribitrary HTTP header received from an upstream server with $upstream_http_headername.
Note that these $upstream variables cannot be anything but null until the response is received from the upstream server, so there are some limitations on their use.
Solution 2:
Here is the full example of what worked for me:
server {
...
location / {
proxy_pass http://backend;
# You may need to uncomment the following line if your redirects are relative, e.g. /foo/bar
#proxy_redirect / /;
proxy_intercept_errors on;
error_page 301 302 307 = @handle_redirect;
}
location @handle_redirect {
set $saved_redirect_location '$upstream_http_location';
proxy_pass $saved_redirect_location;
}
}