Sub topic 9
Reverse Proxying to an Application Server
Assume you know basic Linux commands, HTTP requests, DNS names, TCP ports, and how to edit files with sudo. This chapter uses an application that listens on 127.0.0.1:3000.
A reverse proxy accepts a client request and sends it to another server. Nginx becomes the public HTTP endpoint, while the application server remains private. The client sees Nginx, not the application process.
The proxy location
Add a location block inside the appropriate server block. This example forwards every request beginning with /api/:
server {
listen 80;
server_name example.com;
location /api/ {
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;
}
}proxy_pass defines the upstream address. The proxy_set_header directives preserve request information that would otherwise describe Nginx rather than the original client.
The application can read these headers as follows:
Hostis the requested domain.X-Real-IPis the direct client address seen by Nginx.X-Forwarded-Forcontains the client address and earlier proxy addresses.X-Forwarded-Protosays whether the client used HTTP or HTTPS.
The application must trust forwarded headers only when the request came through a trusted proxy. Otherwise, a direct client could forge them.
Choosing the forwarded path
The URI behavior of proxy_pass depends on whether it includes a path. Compare these locations:
location /api/ {
proxy_pass http://127.0.0.1:3000;
}A request for /api/users is normally sent upstream as /api/users.
location /api/ {
proxy_pass http://127.0.0.1:3000/;
}Here, Nginx replaces the matching /api/ prefix. The same request is sent upstream as /users. This difference often causes unexpected 404 responses.
Use the first form when the application expects the /api/ prefix. Use the second form when the application routes requests from /.
Test the proxy safely
Check that the application responds directly before testing Nginx:
curl -i http://127.0.0.1:3000/api/healthThen test the public route:
curl -i -H 'Host: example.com' http://127.0.0.1/api/healthValidate the Nginx configuration before reloading it:
sudo nginx -t && sudo systemctl reload nginxA reload starts workers with the new configuration while existing requests finish. If the syntax check fails, Nginx keeps using the current configuration.
Common failures
A 502 Bad Gateway usually means Nginx cannot connect to the upstream. Check that the application is running, listening on the expected address and port, and allowed by local security rules.
A 404 Not Found can come from either Nginx or the application. Inspect the access and error logs, then compare the path sent by each proxy_pass form.
A request may reach the wrong server block when its Host header does not match server_name. Test with the real domain or provide the expected Host header.
Exercise
An application expects /users, but clients request /api/users. Which proxy_pass form removes the /api/ prefix?
Show answer
Use proxy_pass http://127.0.0.1:3000/; inside location /api/. The trailing slash causes Nginx to replace the matching prefix.