Sub topic 12
Passing Host, Client IP, and Scheme to the App
Assume you know basic HTTP headers, Nginx locations, and how to run an application server such as Gunicorn. This chapter focuses on a reverse-proxy deployment where Nginx accepts the client connection and forwards the request to your application.
Why forwarded headers matter
When a client connects directly to an application, the application can inspect the connection for details such as the client IP address and HTTPS status. With Nginx in front, the application sees the connection from Nginx instead.
Without additional headers, the application may see:
Host: an internal hostname or unexpected value- Client IP:
127.0.0.1or the address of Nginx - Scheme:
http, even though the client used HTTPS
Nginx can copy these original values into request headers:
| Header | Meaning |
|---|---|
| `Host` | The hostname requested by the client |
| `X-Real-IP` | The client address seen by Nginx |
| `X-Forwarded-For` | A list of client and proxy addresses |
| `X-Forwarded-Proto` | The original scheme, such as `http` or `https` |
Configure Nginx as the trusted proxy
A typical reverse-proxy location looks like this:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://unix:/run/gunicorn/app.sock:;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}$remote_addr is the address of the immediate client connected to Nginx. If Nginx is the public edge server, this is usually the real client address.
$proxy_add_x_forwarded_for preserves an existing X-Forwarded-For value and appends $remote_addr. This supports multiple trusted proxies. For example:
203.0.113.24, 198.51.100.10The first address is the original client. Later addresses identify proxies.
If the original port matters, use proxy_set_header Host $http_host; instead of $host. The $host variable is normalized and usually omits the port.
Trust boundaries and application settings
Forwarded headers are claims made by a proxy. A client can send its own X-Forwarded-For header, so the application must trust these headers only when the request came through a proxy you control.
Configure the framework to trust exactly the expected number of proxies. For example, Flask uses ProxyFix, while Django uses settings such as SECURE_PROXY_SSL_HEADER. The correct setting depends on your proxy chain.
Trusting headers from arbitrary clients creates security problems:
- IP-based access rules can be bypassed.
- Audit logs can record false addresses.
- The application may generate HTTP redirects instead of HTTPS redirects.
- Host-based URL generation can use an attacker-controlled hostname.
How the values travel
The application should use the framework’s trusted-proxy mechanism rather than parsing these headers manually.
Verify the configuration
Check syntax and reload Nginx:
sudo nginx -t
sudo systemctl reload nginx
curl -I https://example.com/
sudo journalctl -u myapp --since "10 minutes ago"Log the values temporarily if you need to confirm that the application receives the expected host, scheme, and client address. Remove sensitive diagnostic logging after testing.
Exercise
Why should an application trust X-Forwarded-For only from a known proxy?
Show answer
Because a client can send that header itself. Trusting unverified values lets a client spoof its IP address and can bypass IP-based rules or corrupt audit logs.