Setting Up Nginx as a Reverse Proxy with SSL on a VPS in 2026
Running your Node.js, Python, or PHP application directly exposed on port 80 is insecure and inflexible. Nginx as a reverse proxy sits in front of your application, handles SSL termination, and provides a clean, maintainable setup.
The Architecture
User Browser → Nginx (port 443, SSL) → Your App (port 3000, localhost only)
Your application never needs to handle SSL certificates. Nginx does it once and proxies plaintext internally.
Installation
sudo apt install nginx certbot python3-certbot-nginx
Nginx Virtual Host Configuration
Create a config file for your domain:
sudo nano /etc/nginx/sites-available/myapp
server {
listen 80;
server_name myapp.yourdomain.com;
# Certbot challenge (needed for initial certificate)
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
# Redirect HTTP to HTTPS
location / {
return 301 https://$server_name$request_uri;
}
}
server {
listen 443 ssl http2;
server_name myapp.yourdomain.com;
ssl_certificate /etc/letsencrypt/live/myapp.yourdomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/myapp.yourdomain.com/privkey.pem;
# Security headers
add_header X-Frame-Options DENY;
add_header X-Content-Type-Options nosniff;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
location / {
proxy_pass http://127.0.0.1:3000; # Your app's port
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
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/myapp /etc/nginx/sites-enabled/
sudo nginx -t # Validate config
sudo systemctl reload nginx
Let's Encrypt SSL Certificate
sudo certbot --nginx -d myapp.yourdomain.com
Certbot modifies your Nginx config automatically to add SSL certificate paths. Certificates auto-renew via a cron job installed by certbot.
Multiple Apps Behind One Nginx
Each application gets its own server_name block. They can all run on different local ports:
# In sites-available/apps.conf:
server { server_name app1.yourdomain.com; location / { proxy_pass http://127.0.0.1:3001; } }
server { server_name app2.yourdomain.com; location / { proxy_pass http://127.0.0.1:3002; } }
server { server_name api.yourdomain.com; location / { proxy_pass http://127.0.0.1:4000; } }