Nginx as a reverse proxy: a minimal setup for self-hosters
5 min readInfrastructure · VPS · Self-hosting · Linux
A VPS has one public IP and, for web traffic, two ports that matter: 80 and 443. The moment you want to run a second app, you hit a wall. Both apps cannot listen on the same port. You could give each one an odd port like 3001 or 8081 and tell people to type it, but nobody wants a URL like that, and you cannot get a normal HTTPS certificate story out of it.
The standard fix is a reverse proxy. One program listens on ports 80 and 443, looks at which domain each request is for, and passes it to the right app running privately on the same machine. Nginx is the most common tool for this job. This post is the smallest setup I trust: routing, HTTPS and the handful of settings that cause most of the pain.
What a reverse proxy does
A normal proxy sits in front of clients and speaks for them. A reverse proxy sits in front of servers and speaks for them. Visitors only ever talk to Nginx. Nginx talks to your apps over a private address such as 127.0.0.1:3000, collects the response and sends it back.
- One entry point - many apps share one IP and one pair of ports.
- HTTPS in one place - Nginx holds the certificate, so your apps can speak plain HTTP locally.
- Routing by name -
app.example.comandapi.example.comgo to different backends. - A single place for rules - size limits, timeouts, compression, caching and basic access control.
- Hiding the backend - your app ports stay closed to the internet.
Install and the file layout
On Ubuntu or Debian, install from the package manager. Site configs usually live in /etc/nginx/sites-available/, and you enable one by linking it into /etc/nginx/sites-enabled/. Other distributions arrange files a little differently, but the idea of one small file per site is the same.
sudo apt update
sudo apt install nginx
sudo nano /etc/nginx/sites-available/app.example.com
sudo ln -s /etc/nginx/sites-available/app.example.com /etc/nginx/sites-enabled/The minimal config
Assume you have an app listening on port 3000 on the same server, and the DNS record for app.example.com already points at the VPS. This server block is the whole idea in a dozen lines.
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-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}server_name is how Nginx decides which block handles a request. proxy_pass is where it forwards to. The four proxy_set_header lines matter more than they look: without them your app sees every visitor as coming from 127.0.0.1 and thinks the request arrived over plain HTTP, which breaks logging, rate limits and redirect logic.
Always test before reloading. A syntax error in a reload that is accepted blindly can take every site on the box down.
sudo nginx -t && sudo systemctl reload nginxAdding HTTPS with Certbot
Once the HTTP version works, add a certificate. Certbot, the client for Let's Encrypt, can edit the Nginx config for you, add the 443 block, install the certificate and set up a redirect from HTTP to HTTPS. I covered how those certificates work in my SSL post; here the practical part is that renewal must keep working.
sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d app.example.com
sudo certbot renew --dry-runThe --dry-run line is the one people skip. It proves renewal will work before the certificate is about to expire. Renewal needs port 80 reachable from the internet and the domain pointing at this server, so a firewall rule added later can silently break it.
Several apps, one server
Adding another app is another file with a different server_name and a different local port. Nginx matches the domain from the request and picks the block. If nothing matches, it falls back to the first block or a default one, which is why I add an explicit catch-all that returns an error instead of accidentally serving the wrong app.
server {
listen 80 default_server;
server_name _;
return 444;
}WebSockets and long requests
Apps that use WebSockets, such as live dashboards, chat and some admin panels, need two extra headers so the connection upgrade is passed through. Forget them and the page loads but the live part never connects, which is a very confusing symptom the first time.
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_read_timeout 120s;
client_max_body_size 20m;
}Two related defaults trip people up. The default upload limit is small, so large uploads fail with a 413 until you raise client_max_body_size. And the default read timeout is short for slow backends, so a long-running request can end in a 504.
Where it bites
- 502 Bad Gateway - Nginx is fine but the app behind it is down, on a different port or listening on the wrong address. Check the app first.
- Open app ports - if your app also listens on
0.0.0.0:3000and the firewall allows it, people can bypass the proxy. Bind to127.0.0.1or close the port. - Docker confusion - inside a container,
127.0.0.1is the container itself. Use the service name on a shared network, or the host gateway address. - Trusting forwarded headers -
X-Forwarded-Forcan be faked by a client. Only trust it when the request really came through your proxy. - Config sprawl - after a dozen sites, copy-pasted blocks drift apart. Keep shared settings in an include file.
- Auto-config tools - if you run Coolify or Dokploy they manage their own proxy. Do not fight them by editing a second one on the same ports.
The failure drill
When something breaks, go in this order. Is the app up? Test it locally with curl http://127.0.0.1:3000. Does sudo nginx -t pass? Read the last lines of /var/log/nginx/error.log, which usually name the problem outright. Does the domain resolve to this server? Is the firewall allowing 80 and 443? Four checks, and a very large share of problems are found before you touch a config.
Should you use Nginx or something else? Caddy gets HTTPS right with almost no configuration and is a fine choice if you value that. Traefik suits setups where containers come and go. I stay with Nginx because it is everywhere, the documentation and answers are plentiful, and the config, while plain, is easy to read.
A reverse proxy is a front door with a guest list: one entrance, many rooms, and nobody wanders into the back.
Docs: nginx.org/en/docs

Written by
Shihab Hossain — Full-Stack Developer
I build thoughtful digital products from Bangladesh — fast web experiences with Next.js, Android apps with Flutter, and the infrastructure behind them.
Work with me