brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: nginx proxy_redirect fixes redirects that point to the backend's own address

Nginx
~/tips/tip-nginx-proxy-redirect-location

proxy_redirect http://localhost:3000/ /app/;

Rewrites Location headers from a backend so redirects stay on your public domain.

You log in through the proxy, and the app sends you to http://localhost:3000/dashboard, which does not exist from outside. The backend thinks it lives on its own port. nginx can rewrite that header on the way out.

location /app/ {
    proxy_pass http://127.0.0.1:8081/;
    proxy_redirect http://localhost:3000/ /app/;
}

When the backend answers with a Location or Refresh header that starts with the first value, nginx replaces that part with the second. A redirect to http://localhost:3000/dashboard comes out pointing at /app/dashboard on the proxy’s own host name.

nginx already does this for the address in proxy_pass itself. A redirect to http://127.0.0.1:8081/home was fixed without any extra line, because that is the default setting. You only need your own line when the app builds redirects from another name, like a localhost:3000 from its config. You can list several proxy_redirect lines for different patterns.

This only touches headers. Links inside the HTML or a redirect done with JavaScript still point to the wrong place, and for those the real fix is the app’s base URL setting, often together with proxy_set_header Host $host and X-Forwarded-Proto. I’d look for that setting first and keep proxy_redirect for apps that have none. proxy_redirect off turns the rewriting off completely.

Further reading: proxy_redirect

Going deeper: Guide: fix a 502 Bad Gateway in Nginx Proxy Manager, with a checklist that finds the cause

More Nginx tips sudo certbot --nginx -d example.com -d www.example.com /data/nginx/custom/server_redirect.conf map $http_upgrade $connection_upgrade { proxy_intercept_errors on; sub_filter 'http://127.0.0.1:8081/' 'https://example.com/app/'; All Nginx tips (50) All topics

Link