Search code examples
nginxhttp-redirectreverse-proxy

Nginx does not redirect proxied responses correct


I have a running nginx instance on server A which passes requests to an Apache webserver on server B.

I have tested several(!) settings... but the following problem still remains:

A request like https://example.com/myapp gets translated to http://example.com/ and passed to server B. Server B sends a http://example.com/login response to nginx/server A. But nginx does not translate that response to https://example.com/myapp/login: what I receive if I use wget https://example.com/myapp is https://example.com/login (which lacks of the /myapp part).

That's my current config:

server {
  listen 443 ssl;
    
  server_name example.com;
  location /myapp/ {
    proxy_pass http://192.168.0.100/;
  }
}

I have read many threads and played with many parameters like proxy_redirect, rewrite and return. But that did not worked for me...


Solution

  • You probably need to reconfigure server B rather than reconfiguring Nginx. When server B issues a redirect, it needs to include the directory. You can pass headers to the backend to let it know that it is behind a reverse proxy and then change the backend to redirect differently when it detects those headers:

    proxy_set_header X-Forwarded-Host $host:$server_port;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    

    As far as I know, Nginx has no equivalent of Apache's proxypass_reverse directive which munges the redirect headers from the backend server. So another solution would be to switch from Nginx to Apache for the front end which has the functionality to change the redirect.