Guides ยท Oct 5, 2026
Nginx Reverse Proxy with Free HTTPS (Let's Encrypt) on a VPS
Put several apps behind one Nginx reverse proxy on a VPS: server blocks per domain, free Let's Encrypt certificates, WebSocket support and troubleshooting.
A reverse proxy lets one server host many websites and apps, all on ports 80 and 443, each with its own domain and its own free HTTPS certificate. Your apps listen on private local ports, and Nginx decides where each request goes. This is the standard way to run several services on a single VPS, whether they are Node.js apps, Docker containers or Python services.
What you need
- An Ubuntu or Debian VPS with root or sudo access.
- One or more domain names with A records pointing to your server's IP (and AAAA if you use IPv6). Wait for DNS to update; you can check with
dig +short app.example.com. - An app already running on a local port, such as
127.0.0.1:3000.
1. Install Nginx and open the firewall
sudo apt update
sudo apt install -y nginx
sudo ufw allow "Nginx Full"
systemctl status nginx --no-pager
Visit your server's IP in a browser; the default Nginx welcome page confirms it works. The UFW guide explains the firewall commands used here.
2. Create a server block for each app
Each domain gets its own file in /etc/nginx/sites-available/:
sudo nano /etc/nginx/sites-available/app.example.com
server {
listen 80;
listen [::]:80;
server_name app.example.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;
}
}
Enable it, test the syntax and reload:
sudo ln -s /etc/nginx/sites-available/app.example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Always run nginx -t before reloading. A syntax error is reported without taking your sites down.
Repeat with another file, another server_name and another proxy_pass port for each additional app. Nginx picks the right block from the domain the visitor used.
3. Get a free HTTPS certificate
sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d app.example.com
Certbot proves you control the domain, downloads a certificate, edits your server block to use it, and offers to redirect HTTP to HTTPS (choose yes). Certificates from Let's Encrypt last a limited time, and certbot renews them automatically with a systemd timer. Verify that renewal works:
sudo certbot renew --dry-run
systemctl list-timers | grep certbot
If certbot fails, the usual causes are DNS not yet pointing to this server, port 80 blocked by a firewall, or a typo in server_name.
4. WebSocket support
Apps using WebSockets (dashboards, chat, n8n, Uptime Kuma) need the upgrade headers. Add this inside location /:
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 300s;
Without it you will see apps that load but never update or keep reconnecting.
5. Add sensible security headers
Inside the HTTPS server block:
add_header X-Content-Type-Options nosniff always;
add_header X-Frame-Options SAMEORIGIN always;
add_header Referrer-Policy strict-origin-when-cross-origin always;
server_tokens off;
Be careful with stricter policies such as Content-Security-Policy and HSTS: they can break your app or lock browsers into HTTPS for a long time if misconfigured. Add them deliberately and test.
6. Upload limits and timeouts
By default Nginx rejects request bodies over 1 MB, which breaks file uploads with a 413 error. Raise it for the apps that need it:
client_max_body_size 50m;
A 504 Gateway Timeout means the app took longer than proxy_read_timeout to answer, so raise it or fix the slow endpoint.
7. Basic rate limiting
Slow down abusive clients on login or API routes. In /etc/nginx/nginx.conf inside the http block:
limit_req_zone $binary_remote_addr zone=login:10m rate=5r/s;
Then in the location you want to protect:
location /login {
limit_req zone=login burst=10 nodelay;
proxy_pass http://127.0.0.1:3000;
}
This helps against small bursts and scripted abuse. It does not replace network-level filtering for floods, which is what a DDoS protected VPS is for.
8. Keep apps private behind the proxy
Bind app ports to localhost (127.0.0.1:3000) so the only way in is through Nginx. For Docker, publish ports as 127.0.0.1:3000:3000 (see the Docker caveat in the UFW guide). Verify what is listening to the outside with sudo ss -tulpn.
9. Troubleshooting checklist
- 502 Bad Gateway: the app is not running or listens on a different port. Check
curl -I http://127.0.0.1:3000and the app's logs. - Default page shows instead of your app: the
server_namedoes not match or the symlink insites-enabledis missing. - Certificate errors: check
sudo certbot certificatesand that DNS points to the right server. - Check logs:
sudo tail -f /var/log/nginx/error.log. - Wrong client IP in your app: the app must trust the
X-Forwarded-Forheader from the proxy (in Express,app.set('trust proxy', 1)).
Where to go next
Before you add more apps, take a backup of /etc/nginx and your certificates folder /etc/letsencrypt, so that a broken edit or a rebuilt server never costs you your configuration. Keep one server block per file and name files after the domain, which makes it obvious later which app owns which site.
See the full app deployment example in deploying Node.js with PM2 and Nginx, run a workflow tool behind the proxy with n8n, and make sure your server basics are covered with the Ubuntu 26.04 setup checklist. If your visitors are mostly on the West Coast, hosting in Oregon keeps latency low.
Frequently asked questions
Can I host multiple websites on one VPS with Nginx?
Yes. Create one server block per domain, each with its own server_name and proxy_pass, and Nginx routes requests by domain.
Are Let's Encrypt certificates really free?
Yes. They are free, and certbot renews them automatically, so make sure the renewal timer is active.
Why do I get a 502 Bad Gateway?
Nginx cannot reach your app. Check that it is running and listening on the port in proxy_pass.
Do I need a reverse proxy for a single app?
Not strictly, but it gives you HTTPS, easy domain routing and the option to add more apps later without opening more ports.
Related guides
- How to Deploy a Node.js App with PM2, Nginx and Free SSL
- UFW Firewall on an Ubuntu VPS: Rules, Rate Limiting and Docker Caveats
- How to Self-Host n8n on a VPS with Docker and HTTPS
Need a server for this?
Every TidalVPS VPS comes with full root access, unmetered traffic, daily backups and DDoS protection.