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: tell Nginx which proxy to trust, so your apps see the real client IP

Nginx
~/tips/tip-nginxproxymanager-real-ip

set_real_ip_from 192.168.1.0/24;

Makes nginx read the real visitor address from a header when another proxy sits in front.

Logs that list the address of your proxy for every visitor are useless. The same goes for IP-based rules that suddenly apply to everyone.

When another proxy or tunnel sits in front of NPM, nginx’s real IP module can swap the address for the one in a header. Put it in http_top.conf in /data/nginx/custom/:

set_real_ip_from 192.168.1.0/24;
real_ip_header X-Forwarded-For;
real_ip_recursive on;

set_real_ip_from lists the addresses you trust to send a correct header. real_ip_header names the header to read. With real_ip_recursive on nginx picks the last address in it that is not trusted, which is the real client.

The caveat is the trust list. Anything you list can claim to be any visitor, so only add addresses you control. Leave out the whole internet. Also, the default for real_ip_header is X-Real-IP, and if the front proxy sets another header, nothing changes until you name it. The subnet above is a placeholder, so use your own.

You can check the effect in an app’s access log or its list of logins. Before the change it shows the address of your proxy for everyone. After it, the real client addresses appear. Nginx’s own log format uses the replaced address too, which helps when you read NPM’s logs.

Further reading: nginx.org: ngx_http_realip_module, Nginx Proxy Manager: Advanced Configuration, On this site: Nginx streams drop idle connections after ten minutes, On this site: nginx closes a quiet WebSocket after 60 seconds, On this site: Guide: put Nextcloud behind Nginx Proxy Manager with HTTPS and the right headers

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