Setting up nginx as a reverse proxy with HTTPS
Updated: 18 November 2026 · Guide · VPS · Linux
A reverse proxy accepts requests from the internet on ports 80 and 443 and passes them on to an app that only listens locally. In nginx that's essentially one line, proxy_pass http://127.0.0.1:3000; plus a few headers so the app sees the real domain and the real visitor IP, and two more for WebSockets. certbot then takes care of HTTPS.
This assumes nginx on the server and a domain whose A record points to the server. How to set up and check both beforehand is in the guide on hosting your own website (link below). This one is only about the part that comes on top when passing requests to an app.
First: make the app listen locally only
The point of the proxy is that the app itself isn't reachable from the internet. So it should listen on 127.0.0.1, not on 0.0.0.0. With Node.js this is usually the host in the configuration or in the call to listen(). With Docker you specify the address with the port, otherwise the port is open despite ufw:
docker run -d -p 127.0.0.1:3000:3000 <image> # or in compose.yaml ports: - "127.0.0.1:3000:3000"
Check that the app responds and really only listens locally: curl must return a response, and ss must show 127.0.0.1:3000, not 0.0.0.0:3000.
curl -I http://127.0.0.1:3000 sudo ss -tlpn | grep 3000
The server block
Create a file /etc/nginx/sites-available/app.your-domain.com. This is the minimal version for an app on port 3000:
server {
listen 80;
listen [::]:80;
server_name app.your-domain.com;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
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;
}
}sudo ln -s /etc/nginx/sites-available/app.your-domain.com /etc/nginx/sites-enabled/ sudo nginx -t sudo systemctl reload nginx
What the headers do
Without an explicit setting nginx passes on the name from proxy_pass as the Host, i.e. 127.0.0.1:3000. That's the default value given in the nginx documentation ($proxy_host). But many apps build links and redirects from the Host and then end up at 127.0.0.1. proxy_set_header Host $host passes through the domain the visitor requested.
The app sees only nginx as the sender of every request, i.e. 127.0.0.1. X-Real-IP carries the visitor's address. X-Forwarded-For appends it to any existing header of the same name; that's how nginx describes the variable $proxy_add_x_forwarded_for. X-Forwarded-Proto tells the app whether the visitor came via http or https. Many frameworks only evaluate these headers once you explicitly allow it; the setting is named differently depending on the framework, often “trust proxy” or “trusted proxies”. Only trust them if the app really is reachable only via the proxy, otherwise any visitor can set any IP they like.
proxy_http_version 1.1 is only the default from nginx 1.29.7; before that it was 1.0. nginx -v shows which version is running. The line does no harm in newer versions and is needed for WebSockets.
Passing WebSockets through
Real-time apps with Socket.IO, dashboards with live data or a chat need WebSockets. The Upgrade header only applies between two directly connected hops, so nginx doesn't pass it on by itself. The nginx documentation solves this with a map, which must be in the http context. On Debian and Ubuntu nginx.conf includes the files under /etc/nginx/conf.d/ there, so create a file /etc/nginx/conf.d/websocket.conf:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}Then add two lines in the location block:
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;A side effect that often causes confusion: nginx closes a connection if the app sends nothing for 60 seconds (proxy_read_timeout, default 60s). Quiet WebSocket connections then drop every minute. The fix is either a larger value for proxy_read_timeout in this location block or ping messages from the app at shorter intervals; the nginx documentation names both.
Paths: the trailing slash in proxy_pass
If the app should run not under the whole domain but under a subpath, a detail in proxy_pass decides. Without a path after the address nginx passes the request on unchanged. With a path nginx replaces the part that matches the location:
# /app/login -> app receives /app/login
location /app/ {
proxy_pass http://127.0.0.1:3000;
}
# /app/login -> app receives /login
location /app/ {
proxy_pass http://127.0.0.1:3000/;
}Which variant is right depends on whether the app itself knows it lives under /app/. If it doesn't, its links and images will then point to the root of the domain. A separate subdomain per app avoids this problem entirely.
HTTPS with certbot
certbot's nginx plugin fetches the certificate and adds the HTTPS part to the server block itself. According to the Certbot documentation it's worth backing up the nginx configuration first.
sudo cp -a /etc/nginx /root/nginx-backup sudo apt install certbot python3-certbot-nginx sudo certbot --nginx -d app.your-domain.com sudo certbot renew --dry-run
The --dry-run test checks renewal against Let's Encrypt's staging environment without saving a real certificate. The headers above remain valid; certbot only changes listen, the certificate paths and the redirect. From now on X-Forwarded-Proto tells the app https.
Firewall: only 80 and 443 open
Only nginx's ports are opened. Port 3000 stays closed, since the app is only reachable locally. If ufw still has an old rule for the app port, remove it:
sudo ufw allow 'Nginx Full' sudo ufw status numbered
Troubleshooting
If the app behind the proxy doesn't respond, nginx returns an error page instead of the app, usually 502 Bad Gateway. Then first run curl -I http://127.0.0.1:3000 on the server itself: if nothing comes back there, it's the app, not nginx. If the app is running, nginx's error log shows why the handover fails:
sudo tail -n 50 /var/log/nginx/error.log sudo nginx -t
If the app redirects to http instead of https after login, or to 127.0.0.1 instead of the domain, Host or X-Forwarded-Proto is almost always missing, or the app doesn't trust the headers yet.
- First: nginx, domain and DNSCheck the A record, static site, permissions
- Installing DockerAnd why ports belong on 127.0.0.1
- Running a Node.js app permanentlyUsing a Discord bot with systemd as an example
- SSH and firewallThe foundation on every new VPS
- See VPS plansAll tiers with real figures