Apache ProxyPassReverse in NGINX: 5 Easy Fixes
The Apache ProxyPassReverse directive fixes the headers that a backend sends back through a reverse proxy, mainly the Location of a redirect. When you move the same site to nginx, the old answer on this blog said that nginx has no equivalent and that you only need to add a few request headers. That is not right: nginx has proxy_redirect and proxy_cookie_path, and its default already does most of the job. This rewrite tests both servers with a small backend that redirects, on Apache httpd 2.4.69 and nginx 1.30.5 in podman.
Contents
What Apache ProxyPassReverse really does
A reverse proxy forwards the request to a backend such as http://myapp:8080/. When the backend answers with a redirect, it writes its own address into the Location header, and the browser would then try to open an internal host name. The Apache ProxyPassReverse directive rewrites that header so the redirect points back to the proxy. The original post used this virtual host:
<VirtualHost myhost:8888>
ServerName myhost
DocumentRoot /path/to/myapp/public
ProxyPass / http://myapp:8080/
ProxyPassReverse / http://myapp:8080/
</VirtualHost>
The lab backend is a 30-line Python server. Its /login page answers 302 with Location: http://myapp:8080/dashboard and a cookie with Path=/. Called directly, it returns exactly that. Behind Apache with only ProxyPass "/shop/" "http://myapp:8080/", the redirect leaks unchanged to the client; adding Apache ProxyPassReverse with the same two arguments produced Location: http://localhost:18081/shop/dashboard.

The nginx equivalent of Apache ProxyPassReverse
In nginx the directive is proxy_redirect, and it is on by default in the form proxy_redirect default. The ngx_http_proxy_module documentation says that the default value is built from the location and the proxy_pass address. The test confirms it. With this block and no other redirect setting:
location /n1/ { proxy_pass http://myapp:8080/; }
the same login redirect came back as Location: http://localhost:18090/n1/dashboard, the address the client used, with the /n1/ prefix added. With proxy_redirect off; in the block, the backend address http://myapp:8080/dashboard leaked, which is the behaviour of Apache without the directive. So the claim "nginx has no ProxyPassReverse" was wrong in practice: you get the same result unless something turns the feature off.
If the default does not match, because the backend uses another host name or a different scheme, set the rewrite yourself:
location /n4/ {
proxy_redirect http://myapp:8080/ /n4/;
proxy_cookie_path / /n4/;
proxy_pass http://myapp:8080/;
}
This block returned Location: http://localhost:18090/n4/dashboard and Set-Cookie: sid=abc; Path=/n4/. The Apache counterpart for the cookie is ProxyPassReverseCookiePath "/" "/shop/"; with it the test showed Path=/shop/, and without it the cookie kept Path=/ for both servers.
Host and X-Forwarded headers: what the backend sees
The old nginx block set four headers. They do not replace the Apache ProxyPassReverse behaviour (they change the request, not the response), but they decide what the backend knows about the original request. The test endpoint /whoami printed these values:
- Apache with default settings:
Host=myapp:8080andX-Forwarded-Host=localhost:18081; Apache adds the X-Forwarded headers by itself. - Apache with
ProxyPreserveHost On:Host=localhost:18083, the host the client asked for. - nginx with only
proxy_pass:Host=myapp:8080and no X-Forwarded headers at all. - nginx with the old post's headers:
Host=localhost,X-Forwarded-Forset andX-Forwarded-Host=localhost:18090.
Notice the last line: proxy_set_header Host $host; dropped the port, so the backend saw localhost instead of localhost:18090. An app that builds absolute links from the Host header will then generate wrong URLs on a non-standard port. Use $http_host if the port must be kept. See the mod_proxy documentation for the Apache side of these directives.
Move an Apache virtual host to nginx step by step
Use this order when you convert a site that relies on Apache ProxyPassReverse, and check the redirect after each step with curl -si:
- Replace
ProxyPass "/shop/" "http://myapp:8080/"withlocation /shop/ { proxy_pass http://myapp:8080/; }. Keep the trailing slash on both sides. - Request a page that redirects, such as the login page. If the
Locationline shows the public address, the defaultproxy_redirectis enough and the Apache ProxyPassReverse line needs no copy. - If the backend still answers with another host name, add an explicit
proxy_redirect http://myapp:8080/ /shop/;. - Replace
ProxyPassReverseCookiePath "/" "/shop/"withproxy_cookie_path / /shop/;and compare theSet-Cookieline. - Replace
ProxyPreserveHost Onwithproxy_set_header Host $host;, or with$http_hostwhen the port matters. The second form was not run in the lab; only$hostwas tested.
The whole lab is reproducible: one container with the Python backend, one with httpd 2.4 and one with nginx on the same podman network, and a curl -si to each virtual host or location. Nothing in the results depends on the host system, so the numbers above will match your own run apart from the container addresses.
Common mistakes when you replace Apache ProxyPassReverse with nginx
- Relative redirects. A backend that answers
Location: /dashboardwas passed through unchanged by both servers (/shop/oldand/n1/oldboth returned/dashboard). Behind a sub path the browser then leaves the prefix. Fix the application's base path; the proxy cannot guess it. - Leaving proxy_redirect off after copying a snippet, which gives the same leak as Apache without ProxyPassReverse.
- Forgetting the cookie path for a sub path. The session cookie keeps
Path=/and is sent to every other application on the host. - Trailing slash mismatch. In the test,
proxy_pass http://myapp:8080/;(with a URI) strips the location prefix, whileproxy_pass http://myapp:8080;passes the path as it is; keep the slashes oflocationandproxy_passconsistent.
Test your configuration
Check the syntax with nginx -t (the lab printed "syntax is ok" and "test is successful") and httpd -t ("Syntax OK"), reload the service, and then test the redirect itself, not only the home page: curl -si http://localhost:18090/n1/login and read the Location and Set-Cookie lines. To serve the site over HTTPS first, see Nginx redirect all HTTP to HTTPS and 7 tips for securing nginx with TLS and Fail2Ban.
FAQ
Do I need a replacement for Apache ProxyPassReverse in nginx? Usually no; the default proxy_redirect rewrites the Location of redirects that start with the address in proxy_pass. Add your own rule only when the backend uses a different address.
Is the old location /home and location /app example still valid? The idea is, but the original IP placeholders and Tomcat port were never tested; use the tested blocks above and put in your own backend address.
Comments
Post a Comment