Apply for Free VPS

Use Nginx as a reverse proxy

Updated 27 Nov 2026 nginx proxy

Put Nginx in front of an application that listens on localhost.

Let your application listen on 127.0.0.1 and have Nginx handle connections from outside.

Configuration

/etc/nginx/sites-available/app:

server {
    listen 20002;                       # 80 or 443 on a dedicated address
    server_name app.example.com;
    client_max_body_size 10m;

    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;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;
    }
}

Add this once, in the http block (for example /etc/nginx/conf.d/upgrade.conf), for WebSockets:

map $http_upgrade $connection_upgrade { default upgrade; '' close; }

Enable

sudo ln -s /etc/nginx/sites-available/app /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

502 Bad Gateway

The application is not answering: check sudo ss -tlnp and journalctl -u your-app.

Trusting headers

Configure your application to trust forwarded headers only from 127.0.0.1.

Was this helpful?

Report outdated or incorrect information