Sub topic 12
Putting an Application Behind a Server Block
This chapter assumes that Nginx is installed and running, that you can edit files with sudo, and that you understand basic HTTP requests and DNS. It also assumes that an application is already listening on 127.0.0.1:3000.
A reverse proxy is a server that accepts a client request and forwards it to another application. Nginx handles the public HTTP connection, while the application listens privately on the server. This keeps the application port out of direct Internet access and lets Nginx manage host names, logs, TLS, and redirects.
Build the reverse-proxy server block
Create /etc/nginx/sites-available/app.example.net:
server {
listen 80;
listen [::]:80;
server_name app.example.net;
access_log /var/log/nginx/app.example.net.access.log;
error_log /var/log/nginx/app.example.net.error.log;
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 proxy_pass directive selects the upstream application. An upstream is the service that receives the forwarded request. The application receives requests such as /login and /api/items through the same paths that the client requested.
The proxy_set_header directives preserve information that would otherwise be changed by the proxy:
Hosttells the application which public host name the client used.X-Real-IPcontains the direct client address seen by Nginx.X-Forwarded-Forkeeps a chain of client and proxy addresses.X-Forwarded-Prototells the application whether the original request used HTTP or HTTPS.
Configure the application to trust forwarded headers only when requests come from your trusted Nginx proxy. Otherwise, a client could send forged header values directly to the application.
Enable and test the site
Enable the configuration, validate all Nginx files, and reload only after validation succeeds:
sudo ln -s /etc/nginx/sites-available/app.example.net \
/etc/nginx/sites-enabled/app.example.net
sudo nginx -t && sudo systemctl reload nginxA reload lets existing connections finish while Nginx starts workers with the new configuration. If nginx -t reports an error, inspect the file and do not reload the failed configuration.
The request path through this setup is:
Common proxy errors
A 502 Bad Gateway usually means that Nginx cannot connect to the upstream. Check that the application is running and listening on the expected address and port:
ss -ltnp | grep ':3000'
sudo tail -n 50 /var/log/nginx/app.example.net.error.logA common mistake is using the wrong address. An application bound only to 127.0.0.1 accepts connections from the same server, which is usually appropriate when Nginx runs there. An application bound to a Unix socket requires a different proxy_pass value.
The trailing slash in proxy_pass changes path handling when a URI is included. Keeping proxy_pass http://127.0.0.1:3000; without a path passes the original request path unchanged.
Exercise
Nginx returns 502 for app.example.net. What should you check first?
Show answer
Confirm that the application is running and listening on 127.0.0.1:3000, then read the per-site Nginx error log for the connection failure.