A reverse proxy sits in front of your web applications. It routes requests to different backend services based on domain, path, or port. This is useful when running multiple apps on one VPS.

This guide shows you how to configure Nginx as a reverse proxy.

What You'll Need

A VPS with Nginx installed and at least one backend service running (Node.js app, Python app, or other web server).

Basic Reverse Proxy Configuration

Edit your Nginx site configuration file (e.g., /etc/nginx/sites-available/example.com).

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

This proxies all requests to a service running on port 3000 (e.g., a Node.js app).

Common proxy_set_header Directives

  • proxy_set_header Host $host; – Passes original hostname.
  • proxy_set_header X-Real-IP $remote_addr; – Passes real client IP.
  • proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; – For logging real IPs.
  • proxy_set_header X-Forwarded-Proto $scheme; – Tells backend if request was HTTPS.

Proxying Based on Path

Route different paths to different backends:

server {
    listen 80;
    server_name example.com;

    location /app1/ {
        proxy_pass http://localhost:3001/;
    }

    location /app2/ {
        proxy_pass http://localhost:3002/;
    }
}

Proxying Based on Subdomain

Create separate server blocks for each subdomain:

server {
    listen 80;
    server_name api.example.com;
    location / {
        proxy_pass http://localhost:3000;
    }
}

server {
    listen 80;
    server_name app.example.com;
    location / {
        proxy_pass http://localhost:4000;
    }
}

Adding HTTPS with Let's Encrypt

Install Certbot and obtain a certificate:

sudo apt install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com

Certbot automatically updates your Nginx config to use HTTPS.

WebSocket Proxying

For apps using WebSockets (like live chat), add these headers:

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

Load Balancing with Upstream

Distribute traffic to multiple backend servers:

upstream backend {
    server 127.0.0.1:3001;
    server 127.0.0.1:3002;
    server 127.0.0.1:3003;
}

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://backend;
    }
}

Testing and Reloading

Test configuration and reload Nginx:

sudo nginx -t
sudo systemctl reload nginx

Next Steps

Reverse proxies are powerful. You can now run multiple applications on one VPS, each on different ports, all accessible via standard ports 80/443.

Need a VPS to practice on? Check our recommended VPS providers.