The Short Answer
An application such as a Node.js or Python service listens on a local port, for example 127.0.0.1:3000. A web server such as Nginx listens on ports 80 and 443, handles HTTPS, and forwards requests to that local port. The application never faces the internet directly. HAProxy is another common reverse proxy, often chosen when load balancing is the main job.
Reverse Proxy vs Forward Proxy
| Forward proxy | Reverse proxy | |
|---|---|---|
| Acts for | Clients | Servers |
| Set up by | The client's network | The site operator |
| Hides | The client | The backends |
| Typical use | Filtering outbound traffic | HTTPS, routing, caching |
The traffic flows the same way; what differs is whose side the proxy is on.
What a Reverse Proxy Does
- TLS termination: it holds the certificate and handles HTTPS, so backends speak plain HTTP on a local or private address. See what is SSL/TLS.
- Routing: one public address serves many applications, chosen by host name or path, such as
/api/to one backend and everything else to another. - Load balancing: requests are spread across several backends; see what is a load balancer.
- Caching and compression: it can store responses and compress them, which takes load off the application.
- Buffering: it can read slow clients' requests completely before passing them on, so the backend is not held up.
- Limits: request size, rate limits, and access rules in one place.
A Minimal Example
server {
listen 443 ssl;
server_name example.com;
# ssl_certificate and ssl_certificate_key lines go here
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;
}
}
The full setup, with certificates and testing, is in Nginx reverse proxy.
The Real Client Address
To the backend, every request comes from the proxy, so its logs show 127.0.0.1 or the proxy's private address. The proxy passes the original details in headers:
X-Forwarded-For: the client address, plus any earlier proxies.X-Real-IP: the client address alone, a common Nginx convention.X-Forwarded-Proto: whether the client used http or https.Forwarded: the standardized header that combines these.
The backend must trust these headers only when they come from the proxy, because any client can send them. In Nginx on a backend, the realip module does this with set_real_ip_from naming the proxy's address and real_ip_header naming the header. A proxy that forwards raw TCP instead of HTTP can pass the address with the PROXY protocol.
On Your Server: See the Proxy at Work
sudo ss -tlnp
curl -I https://example.com
curl -I http://127.0.0.1:3000
- Listening sockets: Nginx should appear on
0.0.0.0:443and the application on127.0.0.1:3000. If the application shows0.0.0.0:3000, it is reachable around the proxy; bind it to127.0.0.1. - Headers: compare the two
curl -Iresults. Headers that appear only in the first were added by the proxy. - Logs: follow
/var/log/nginx/access.logand the application's log side by side while you load a page; see Nginx logs.
Common Problems
- 502 Bad Gateway: the backend is down or on another port; see 502 Bad Gateway in Nginx.
- 504 Gateway Timeout: the backend answered too slowly; see 504 Gateway Timeout.
- Redirect loop: the application thinks the request was plain HTTP because it ignores
X-Forwarded-Proto; see too many redirects. - Every visitor has the same address: the headers are not set or not trusted on the backend.
- One server, one point of failure: if the proxy stops, every site behind it stops. Monitor it and test each change with
sudo nginx -tbefore reloading.