Sub topic 5
Forwarding Host, Client IP and Protocol to the Backend
This chapter assumes that you know basic HTTP headers, TCP ports, and Nginx server and location blocks. It also assumes that an application is listening on a local or private port, such as 127.0.0.1:3000.
Why forwarded headers matter
A reverse proxy receives the client request and creates a second request to the backend. Without configuration, the backend may see Nginx as the client. It may also see the wrong hostname or believe that an HTTPS request was plain HTTP.
Forwarded headers preserve information from the original request:
| Header | Value | Common backend use |
|---|---|---|
| `Host` | Requested hostname | Virtual hosts, absolute URLs |
| `X-Real-IP` | Client IP address | Simple client identification |
| `X-Forwarded-For` | Chain of proxy and client IPs | Logging and audit trails |
| `X-Forwarded-Proto` | `http` or `https` | Secure redirects and cookies |
An application must be configured to trust these headers only when requests come through your Nginx proxy. A client can send headers with these names directly, so accepting them from an untrusted network can enable incorrect logs, redirects, or access decisions.
Configure the basic headers
A typical proxied site sets the original host, client address, proxy chain, and protocol explicitly:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:3000;
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;
}
}$host contains the hostname selected by Nginx. Use $http_host when the backend must receive the original Host value, including its port. $remote_addr is the address of the client connected directly to Nginx.
$proxy_add_x_forwarded_for preserves an existing X-Forwarded-For chain and appends the current client address. This is useful when another trusted proxy already sits in front of Nginx. If Nginx is the public edge and clients connect directly to it, overwrite the value instead:
proxy_set_header X-Forwarded-For $remote_addr;That prevents a client from placing a misleading address at the start of the chain.
$scheme is the protocol used by the client to connect to Nginx. If Nginx terminates TLS, it is https, even when Nginx forwards the request to the backend over plain HTTP.
Understand the request path
The client-facing request and the backend request are separate connections:
The backend should use the forwarded protocol when generating redirects or secure URLs. Many web frameworks require a setting such as “trust proxy” before they honor X-Forwarded-Proto and X-Forwarded-For.
Validate the configuration
Always test before reloading. A syntax error should not replace a working configuration:
sudo nginx -t && sudo systemctl reload nginxThen inspect the application logs or create a temporary endpoint that displays the received headers. Confirm that the hostname, client address, and protocol match the original request.
Exercise
Which header should an application use to determine whether the original client used HTTPS, and what must be configured before trusting it?
Show answer
It should use X-Forwarded-Proto. The application must trust only the known Nginx proxy, because clients can forge this header when they connect directly.