Back to articles
Cloud VPS HostingAvertHostNovember 25, 20268 min read

Nginx Reverse Proxy on a VPS: Host Multiple Apps with HTTPS

Set up an nginx reverse proxy on an Ubuntu VPS: proxy several apps, add Let's Encrypt HTTPS, handle WebSockets and timeouts, and fix 502/504 errors.

An nginx reverse proxy is an Nginx server that accepts requests on ports 80 and 443 and forwards them to applications running on other local ports, so one VPS can serve several apps on separate domains with HTTPS. You install Nginx, define one server block per domain with proxy_pass, add a Let's Encrypt certificate and keep the application ports closed to the internet. This guide shows the full setup on Ubuntu 22.04 or 24.04, including the headers, WebSocket and timeout settings that most tutorials skip.

Start with a hardened server. If yours is new, follow the first 30 minutes of VPS setup first, so you have a non-root user, SSH keys and a firewall.

What a reverse proxy does and why use one on a VPS

A forward proxy hides clients from servers. A reverse proxy does the opposite: it sits in front of your applications and hides them from clients. Visitors talk only to Nginx; Nginx talks to your Node.js, Python, Java or Docker apps on 127.0.0.1.

  • One public entry point. Ports 80 and 443 serve every domain, while apps listen on 3000, 8000, 8069 and so on, reachable only from the server itself.
  • TLS in one place. Certificates and renewals are handled by Nginx and Certbot, not by each application.
  • Isolation and control. You can add rate limits, size limits, basic authentication or IP allow-lists without changing application code.
  • Zero-downtime changes. You can restart or replace an app and only the proxy path is affected.

The cost is one more component to patch and monitor. For a single static site, plain Nginx without proxying is simpler. A reverse proxy earns its place when you run two or more apps, or when an app framework should not face the internet directly.

Before you start: prerequisites

  • An Ubuntu VPS with a public IP address. A cloud VPS with 1 to 2 vCPU and 2 GB RAM is enough for Nginx plus a few small apps; Nginx itself uses very little memory.
  • A domain or sub-domain with an A record pointing to the VPS IP (for example app1.example.in and app2.example.in). DNS must resolve before you request certificates.
  • An application listening on a local port. For testing, you can run python3 -m http.server 8000 --bind 127.0.0.1.
  • Firewall rules allowing 80 and 443 (shown below).

Step 1: Install Nginx and open the right ports

sudo apt update
sudo apt install -y nginx
sudo ufw allow OpenSSH
sudo ufw allow 'Nginx Full'
sudo ufw enable
sudo ufw status

'Nginx Full' opens 80 and 443. Do not open your application ports. Browse to the server IP and you should see the default Nginx page. If you run Docker, remember that published ports can bypass ufw; bind containers to loopback with 127.0.0.1:8000:8000, as explained in our Docker VPS guide.

Step 2: Create a server block that proxies to your app

Create one file per domain in /etc/nginx/sites-available/:

server {
    listen 80;
    listen [::]:80;
    server_name app1.example.in;

    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 and reload:

sudo ln -s /etc/nginx/sites-available/app1.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

Always run nginx -t before reloading; a syntax error then leaves the running configuration untouched. The four proxy_set_header lines matter. Without Host, the app sees 127.0.0.1 as the hostname. Without X-Forwarded-For, logs and rate limiters see only the proxy's address. Without X-Forwarded-Proto, apps behind HTTPS may generate http:// links or redirect in a loop.

Step 3: Add HTTPS with Let's Encrypt

sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d app1.example.in --redirect --agree-tos -m you@example.in
sudo certbot renew --dry-run

Certbot edits the server block to add port 443, the certificate paths and an HTTP-to-HTTPS redirect, and installs a timer for renewal. The dry run confirms renewal will work. The usual failure is DNS not pointing at the server yet, or port 80 blocked by a firewall. See the Certbot documentation for other options such as wildcard certificates. If you want a paid certificate with organisation validation, that is a separate decision from the proxy itself; the proxy configuration is the same.

Step 4: Host several apps on one VPS

Repeat step 2 with another file and another server_name. Nginx chooses the block by the Host header, so each domain reaches its own app:

DomainLocal appproxy_pass
app1.example.inNode.js APIhttp://127.0.0.1:3000
erp.example.inOdoohttp://127.0.0.1:8069
flows.example.inn8n in Dockerhttp://127.0.0.1:5678

You can also split one domain by path, for example location /api/ { proxy_pass http://127.0.0.1:3000/; }. Mind the trailing slash: with a URI in proxy_pass, Nginx replaces the matched prefix; without it, the original path is passed through unchanged. Getting this wrong is the most common cause of 404 errors behind a proxy.

To put an unmatched or default domain to good use, add a catch-all block with listen 80 default_server; return 444; so requests for unknown hostnames are dropped instead of landing on your first app.

Step 5: WebSockets, timeouts and upload limits

Apps such as n8n, chat servers and dashboards use WebSockets. Add this inside the location block:

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 120s;
client_max_body_size 50m;
  • Upgrade headers are required for the WebSocket handshake; without them you see connections that open and immediately close.
  • proxy_read_timeout defaults to 60 seconds. Long reports or exports that exceed it return a 504 Gateway Timeout.
  • client_max_body_size defaults to 1 MB. Uploads larger than that fail with 413 Request Entity Too Large. Set it to what the application really needs, not to unlimited.

For a pool of identical app instances, define an upstream block with several server 127.0.0.1:300x; lines and point proxy_pass at its name; Nginx will round-robin between them.

Step 6: Harden the proxy

  • Hide the version: set server_tokens off; in /etc/nginx/nginx.conf.
  • Rate-limit login or API paths: define limit_req_zone $binary_remote_addr zone=login:10m rate=5r/m; in the http block and use limit_req zone=login burst=10 nodelay; in the location.
  • Bind apps to loopback. Verify with ss -tlnp that your apps show 127.0.0.1:PORT, not 0.0.0.0:PORT.
  • Protect admin tools with an allow/deny list or basic authentication, in addition to the application's own login.
  • Keep logs. The default access and error logs under /var/log/nginx/ are rotated by logrotate; include them in your audit routine. Our VPS security checklist covers the wider server.

If your applications process personal data of people in India, the DPDP Act puts obligations on how that data is protected. Encrypting traffic with TLS and limiting which services are exposed are sensible technical measures, but the Act is broader than any proxy setting; read the MeitY publications or take legal advice for your own obligations.

Troubleshooting common errors

SymptomLikely causeCheck
502 Bad GatewayApp is down or on a different portss -tlnp, curl -I http://127.0.0.1:3000, systemctl status of the app
504 Gateway TimeoutApp slower than the read timeoutRaise proxy_read_timeout, or fix the slow request
413 Request Entity Too LargeUpload bigger than the body limitRaise client_max_body_size
Redirect loopApp unaware it is behind HTTPSSend X-Forwarded-Proto; enable the app's "trust proxy" setting
Wrong client IP in the appForwarded headers not usedConfigure the app to read X-Forwarded-For from the proxy only
502 with SELinux enabled (RHEL-family)Nginx not allowed to connect outsetsebool -P httpd_can_network_connect 1

The first place to look is always sudo tail -f /var/log/nginx/error.log while you repeat the request. After any change, run nginx -t, reload, and re-test with curl -I https://app1.example.in. If the problem started right after a reboot, check that both Nginx and the app are enabled with systemctl enable.

How AvertHost can help

AvertHost runs Linux and Windows cloud VPS in a Tier-IV data centre in Delhi, so traffic from Indian users stays in-country, with a 99.9% uptime SLA, INR billing and GST invoices. You get a server you control, so any of the Nginx setups above work on a Linux VPS. See the services overview for the other cloud options, and our guide to deploying Nginx, PHP and Node with HTTPS if you prefer serving apps directly. Take a snapshot or backup before editing production configuration; the cloud server backup guide explains how.

FAQ

Is Nginx or Apache better as a reverse proxy?

Both can do it. Nginx is the common choice because its event-driven design handles many connections with low memory and its proxy configuration is short. If you already run Apache with mod_proxy and are comfortable with it, there is no need to switch.

Can I run a reverse proxy and a WordPress site on the same VPS?

Yes. Give WordPress its own server block that passes PHP to PHP-FPM, and give the other apps proxy blocks. Each is selected by server_name. Watch total RAM, since PHP-FPM, databases and Node apps share the same memory.

Do I need a reverse proxy for Docker containers?

Not strictly, but it is the clean way to expose several containers on ports 80 and 443 with TLS. Publish container ports only on 127.0.0.1 and let Nginx reach them.

How do I renew certificates automatically?

Certbot installs a systemd timer or cron job when you install it. Verify with sudo certbot renew --dry-run and check systemctl list-timers. Renewals fail if port 80 is blocked or the DNS record has changed.

Why do I get 502 only after the server restarts?

Usually the application did not start at boot, so Nginx has nothing to forward to. Run the app as a systemd service or with a Docker restart policy, and enable it so it starts automatically.