Nginx as a reverse proxy
Intermediate
12 min read
Put a web server in front of your application to handle HTTPS, static files and forwarding.
Why it matters
Application servers are not built to face the internet directly. A reverse proxy terminates HTTPS, serves static files fast, and forwards the rest to your app — and it is where many deployment errors (502, 504) show up first.
Where it fits
What you will learn
- Explain what a reverse proxy does.
- Read a server block that forwards requests to an app.
- Find the cause of a 502 Bad Gateway.
Before you start
- HTTP and APIs✓ Learned
- Containers with Docker✓ Learned
The request path
- Browser
- Nginx :443 (HTTPS)
- App :3000
- Database
A minimal server block
/etc/nginx/conf.d/app.conf
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
Example
sudo nginx -t # check the configuration
sudo systemctl reload nginx # apply it without dropping connections
Tip: A 502 Bad Gateway almost always means Nginx is fine but the app behind it is not running, or listens on a different address or port. Check the app first, then the Nginx error log.
Install and set up
- Nginx Linux packages ↗ Official packages for the common Linux distributions.
Official documentation and references
Documentation
- Nginx documentation ↗ Official · nginx.org
Guides
- Nginx beginner's guide ↗ Official · nginx.org
Reference
- ngx_http_proxy_module ↗ Official · nginx.org — Every proxy_ directive explained.
Learn next
- HTTPS and certificates ›✓ Learned
Related topics
- Containers with Docker✓ Learned
- Logs, metrics and alerts✓ Learned
- HTTP and APIs✓ Learned
Done with this topic?
Reviewed 2026-10-23. Links go to the official documentation unless marked otherwise.