Nginx as a Reverse Proxy in Front of an App
Nginx is often placed in front of an application server such as Node.js, Gunicorn, or a Java service. It accepts client connections, terminates TLS, serves static files, and forwards dynamic requests to the upstream app.
A minimal server block looks like this:
bash sudo nano /etc/nginx/sites-available/app.conf
nginx server { listen 80; servername example.com;
location / { proxypass http://127.0.0.1:3000; proxysetheader Host $host; proxysetheader X-Real-IP $remoteaddr; proxysetheader X-Forwarded-For $proxyaddxforwardedfor; } }
After writing the file, enable it and reload safely:
bash sudo ln -s /etc/nginx/sites-available/app.conf /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl reload nginx
Run nginx -t before every reload. It parses the configuration and reports errors without affecting the running server.
Useful directives to know:
- proxyreadtimeout controls how long Nginx waits for the upstream response. - clientmaxbodysize limits upload size, and the default of 1m often needs raising. - proxysetheader Upgrade $httpupgrade plus Connection 'upgrade' is required for WebSocket traffic.
When something fails, the error log at /var/log/nginx/error.log usually explains why, and sudo tail -f on it is the fastest way to watch requests while you test.
If you change a configuration file often, sudo nginx -T prints the full effective configuration, including every included file, so you can confirm which server block actually handles a request.