Skip to content
VDS · Websites and web servers

Reverse proxy: multiple applications on one address

How to share one public VDS address among several applications by domains and paths, pass headers correctly, and not break WebSocket.

Updated August 23, 2026

A machine has one public IPv4 address, but usually runs several applications. A reverse proxy accepts all requests on ports 80 and 443 and routes them to internal services by domain name or path. It also holds the single certificate and the shared logs.

Layout

Each application listens on its own local port and only on 127.0.0.1:

  • the frontend site — 127.0.0.1:3000;
  • the API — 127.0.0.1:8000;
  • the monitoring dashboard — 127.0.0.1:9090.

Only Nginx is exposed to the outside. To check that an application isn't exposed directly:

ss -tulpn | grep LISTEN

Lines like 0.0.0.0:3000 mean the service is reachable from the internet, bypassing the proxy and without encryption.

By domain

server {
    listen 80;
    server_name app.example.ru;
    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;
    }
}

server {
    listen 80;
    server_name api.example.ru;
    location / {
        proxy_pass http://127.0.0.1:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Without X-Forwarded-For, the application sees every request as coming from 127.0.0.1, and any IP-based restrictions stop working.

By path

location /api/ {
    proxy_pass http://127.0.0.1:8000/;
}

The trailing slash in proxy_pass matters here: with it, the /api prefix is stripped and the application receives /users; without it, it receives /api/users. Half of all “mysterious 404s” on a proxy come from exactly this.

WebSocket and long responses

location /ws/ {
    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_read_timeout 3600s;
}

Without the Upgrade/Connection pair, the connection drops right after the handshake; without an increased timeout, it drops after a minute of silence.

Common mistakes

SymptomCause
502 Bad GatewayThe application isn't running or listens on a different port
504 Gateway Time-outThe application takes longer to respond than proxy_read_timeout
Infinite redirectThe application doesn't know about HTTPS — pass X-Forwarded-Proto
File uploads breakclient_max_body_size is too small; the default is 1 MB
The same IP in the application logsX-Forwarded-For isn't passed

Next: multiple sites, certificate, Caddy with automatic HTTPS.

One certificate on the proxy covers all applications behind it. Internal connections over 127.0.0.1 don't need encryption — they never leave the machine.