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_set_header Host $host stops backends from seeing 127.0.0.1

Nginx
~/tips/tip-nginx-proxy-set-header-host

proxy_set_header Host $host; proxy_set_header X-Forwarded-Proto $scheme;

Forwards the original host name and scheme to the application behind the proxy.

Apps behind a proxy that build absolute links or pick a virtual host by name need the original host name.

location / {
    proxy_pass http://127.0.0.1:8080;
    proxy_set_header Host $host; proxy_set_header X-Forwarded-Proto $scheme;
}

By default nginx sends Host: $proxy_host, which is the address written in proxy_pass, so the backend sees 127.0.0.1:8080. $host forwards the name the client asked for, and $http_host also keeps the port. X-Forwarded-Proto tells the app whether the visitor used HTTP or HTTPS, which prevents redirect loops. The app has to be configured to trust these headers from the proxy.

Further reading: ngx_http_proxy_module: proxy_set_header

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; proxy_redirect http://localhost:3000/ /app/; All Nginx tips (50) All topics

Link