NEWUse coupon code SMX20 to get 20% OFF your first billing cycle on any game server or VPS!Explore Store

TUTORIALS & GUIDES

How to Configure Nginx as a Reverse Proxy for Multiple Applications

Serve several apps from one VPS with Nginx: one server block per domain, path-based routing, the right proxy headers, WebSockets, HTTPS for every site, timeouts, upload limits and debugging 502s.

One VPS can comfortably host several applications: a Node.js site, a Python API, a WordPress blog and an admin dashboard, for example. Only one program can listen on port 443, though, so you need something that receives every request and routes it to the right app. That is the job of a reverse proxy, and Nginx is the most common choice: it is fast, uses little memory and also handles HTTPS, compression, caching and upload limits for all your apps in one place.

How Routing Works

                 ┌─ shop.example.com ─▶ 127.0.0.1:3000  (Next.js)
Internet ─▶ Nginx┼─ api.example.com  ─▶ 127.0.0.1:8000  (FastAPI)
  :80/:443       └─ example.com/blog ─▶ 127.0.0.1:8080  (WordPress container)

Nginx chooses a server block by matching the request's Host header against each block's server_name. Inside a block, location rules route by URL path. Each app listens only on 127.0.0.1, so nothing except Nginx can reach it directly.

Step 1: Run Each App on Its Own Local Port

Give every application a unique port and bind it to localhost. Examples:

AppHow to bind
Node.js / Next.jsPORT=3000 HOSTNAME=127.0.0.1 npm start, under PM2 or systemd
FastAPI / Uvicornuvicorn main:app --host 127.0.0.1 --port 8000
Django / Gunicorngunicorn mysite.wsgi --bind 127.0.0.1:8001
Docker containerports: ["127.0.0.1:8080:80"]

Check what is listening with sudo ss -tlnp. Anything bound to 0.0.0.0 or * is reachable from outside unless your firewall blocks it. Docker-published ports bypass UFW entirely.

Step 2: Point the Domains at the VPS

Create an A record for each hostname (shop.example.com, api.example.com and so on) with the VPS IP. A wildcard record (*.example.com) is handy if you add subdomains often.

Step 3: One Server Block per Domain

Put shared proxy headers in a snippet so every site behaves the same. Create /etc/nginx/snippets/proxy.conf:

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 X-Forwarded-Host  $host;
proxy_read_timeout 60s;

Then one file per site, for example /etc/nginx/sites-available/shop.example.com:

server {
    listen 80;
    server_name shop.example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
        include snippets/proxy.conf;
    }
}

and /etc/nginx/sites-available/api.example.com:

server {
    listen 80;
    server_name api.example.com;
    client_max_body_size 25m;

    location / {
        proxy_pass http://127.0.0.1:8000;
        include snippets/proxy.conf;
    }
}
sudo ln -s /etc/nginx/sites-available/shop.example.com /etc/nginx/sites-enabled/
sudo ln -s /etc/nginx/sites-available/api.example.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

Why the headers matter: without Host, many frameworks generate wrong URLs; without X-Forwarded-For, every visitor appears to come from 127.0.0.1; without X-Forwarded-Proto, apps behind HTTPS think requests are plain HTTP and create redirect loops or insecure cookies. Each framework also needs to be told to trust these headers from 127.0.0.1 (Express app.set('trust proxy', 1), Django SECURE_PROXY_SSL_HEADER, Uvicorn --proxy-headers).

Step 4: Add a Catch-All Default Server

If a request arrives with an unknown Host header (for example, someone browsing your bare IP), Nginx serves it from the first server block it finds, which may expose an app on the wrong domain. Add an explicit default that closes the connection:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name _;
    return 444;
}

Remove the distribution's sites-enabled/default file so it does not conflict. For HTTPS, a catch-all needs a certificate; many people simply let the first real site act as the default for port 443.

Step 5: Path-Based Routing on One Domain

Sometimes you want several apps under one hostname. The trailing slash on proxy_pass decides whether the prefix is stripped:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;        # main site
        include snippets/proxy.conf;
    }

    # /api/users  ->  backend receives /users   (prefix stripped)
    location /api/ {
        proxy_pass http://127.0.0.1:8000/;
        include snippets/proxy.conf;
    }

    # /docs/intro ->  backend receives /docs/intro   (prefix kept)
    location /docs/ {
        proxy_pass http://127.0.0.1:4000;
        include snippets/proxy.conf;
    }
}

Path-based routing only works cleanly if the app knows its base path; otherwise links and assets point to / and break. Most frameworks have a setting for this (Next.js basePath, Django FORCE_SCRIPT_NAME, WordPress site URL). When in doubt, subdomains are simpler.

Step 6: WebSockets and Server-Sent Events

WebSockets need the HTTP/1.1 upgrade headers passed through. Define a map once in /etc/nginx/conf.d/websocket.conf:

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

and use it in the app's location:

location / {
    proxy_pass http://127.0.0.1:3000;
    include snippets/proxy.conf;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_read_timeout 3600s;
}

For Server-Sent Events, disable buffering on that location with proxy_buffering off; so events reach the browser immediately.

Step 7: HTTPS for Every Site

sudo ufw allow 'Nginx Full'
sudo certbot --nginx -d shop.example.com -d api.example.com -d example.com
sudo certbot renew --dry-run

Certbot edits each matching server block, adds the certificate and an HTTP-to-HTTPS redirect. You can issue one certificate covering several names (as above) or run Certbot once per site; separate certificates are easier to manage when sites belong to different clients. Full details are in our Let's Encrypt guide.

Useful Per-App Settings

NeedDirective
Larger uploadsclient_max_body_size 50m;
Long-running requestsproxy_read_timeout 300s; (better: move work to a background job)
Serve static files directlylocation /static/ { alias /srv/app/static/; }
Rate-limit a login endpointlimit_req_zone in http, then limit_req zone=login burst=10;
Compressiongzip on; gzip_types text/css application/javascript application/json;
Restrict an admin appallow 198.51.100.25; deny all; inside its location

ServoMix Plans for This Workload

Nginx itself needs very little; the applications behind it set the requirement. Two or three small apps fit 2 vCores / 8 GB. Hosting many client sites or several databases on one server is where the 4–8 vCore tiers make sense.

Intel Xeon Platinum 8269CY VPS

KVM VPS on Intel Xeon Platinum 8269CY (up to 3.80 GHz Turbo) with DDR4 ECC memory, NVMe SSD, full root access and an unmetered 1 Gbps port.

PlanvCoresRAMStorageNetworkPriceGood forBuy
SMX vCore XS (Intel Xeon)28 GB DDR4 ECC40 GB NVMe SSD1 Gbps, unmetered₹270/moBusiness websites, small apps, scheduled jobs and botsOrder
SMX vCore S (Intel Xeon)416 GB DDR4 ECC80 GB NVMe SSD1 Gbps, unmetered₹1,169/monthBusiness software, mid-size databases, several sites on one serverCheck stock
SMX vCore M (Intel Xeon)832 GB DDR4 ECC150 GB NVMe SSD1 Gbps, unmetered₹2,249/monthERP/CRM stacks, heavier databases, multiple production appsCheck stock
SMX vCore L (Intel Xeon)1664 GB DDR4 ECC250 GB NVMe SSD1 Gbps, unmetered₹4,248/monthHigh-memory workloads and consolidated production serversCheck stock

AMD EPYC 7C13 VPS

Dedicated KVM vCores on AMD EPYC 7C13 (up to 3.70 GHz) with DDR4 ECC memory, NVMe SSD and an unmetered 1 Gbps port.

PlanvCoresRAMStorageNetworkPriceGood forBuy
SMX vPro XS (AMD EPYC)28 GB DDR4 ECC40 GB NVMe SSD1 Gbps, unmetered₹420/moWebsites, small APIs, bots, staging and VPN serversOrder
SMX vPro S (AMD EPYC)416 GB DDR4 ECC80 GB NVMe SSD1 Gbps, unmetered₹560/moWooCommerce, Node.js/Python backends, Docker Compose stacks, CI runnersOrder
SMX vPro M (AMD EPYC)832 GB DDR4 ECC150 GB NVMe SSD1 Gbps, unmetered₹840/moProduction databases, high-traffic sites, multi-container appsOrder
SMX vPro L (AMD EPYC)1664 GB DDR4 ECC250 GB NVMe SSD1 Gbps, unmetered₹1,120/moLarge production workloads, SaaS platforms, analytics, in-memory cachesOrder

Prices are monthly in INR as listed on the ServoMix store (verified 2026-11-14). Stock and final pricing are shown on the product page at checkout.

Debugging 502, 504 and Redirect Loops

  • 502 Bad Gateway: Nginx cannot reach the app. Check the app is running and listening on the port in proxy_pass (curl -I http://127.0.0.1:3000, ss -tlnp). Read /var/log/nginx/error.log for "connection refused".
  • 504 Gateway Timeout: the app responded too slowly. Raise proxy_read_timeout for that location or make the endpoint faster.
  • Too many redirects: the app does not know the original request was HTTPS. Pass X-Forwarded-Proto and configure the framework to trust the proxy. With Cloudflare, use Full (strict) SSL mode.
  • Wrong site appears: server_name does not match the hostname, so the default server answered. Check with sudo nginx -T | grep server_name.
  • Assets 404 under a path: the app is not configured for its base path, or the trailing slash on proxy_pass strips a prefix the app expects.

Frequently Asked Questions

Can I host multiple websites on one VPS with Nginx?
Yes. Run each app on its own local port and create one Nginx server block per domain with proxy_pass to that port. Nginx routes requests by the Host header, so all sites share ports 80 and 443.
Subdomains or paths: which is better for multiple apps?
Subdomains (api.example.com) are simpler because each app keeps its own root path. Paths (example.com/api/) work but require each app to know its base path.
What does the trailing slash in proxy_pass do?
With a URI in proxy_pass (e.g. http://127.0.0.1:8000/), Nginx replaces the matched location prefix with that URI, effectively stripping it. Without one (http://127.0.0.1:8000), the full original path is passed unchanged.
Why does my app see 127.0.0.1 as every visitor's IP?
Pass X-Forwarded-For and X-Real-IP from Nginx and configure the framework to trust the proxy at 127.0.0.1. Then it reads the client address from those headers.
How do I proxy WebSockets with Nginx?
Use proxy_http_version 1.1 and pass Upgrade and Connection headers (via a map on $http_upgrade), and raise proxy_read_timeout for long-lived connections.
How do I get SSL for several domains on one server?
Run certbot --nginx with multiple -d flags or once per site. Certbot edits each server block and sets up automatic renewal for all certificates.
Do I need to open each app's port in the firewall?
No. Only 80 and 443 (plus SSH) should be open. The apps listen on 127.0.0.1 and are reached only through Nginx.
Nginx, Caddy or Traefik?
All three are good reverse proxies. Nginx is the most widely documented and very efficient; Caddy issues certificates automatically with a simpler config; Traefik discovers Docker containers automatically. Pick the one that fits how you deploy.
What causes a 502 Bad Gateway from Nginx?
Nginx could not get a response from the upstream app: it is stopped, crashed, listening on a different port or address, or the socket path is wrong. Check the app status and Nginx's error log.
Which ServoMix VPS should I use to host several apps?
Add up the memory each app and database needs. Two or three small apps fit the 2 vCore / 8 GB plan; agencies hosting many sites or several databases usually choose 4 vCores / 16 GB or 8 vCores / 32 GB.

Frequently Asked Questions

Can I host multiple websites on one VPS with Nginx?

Yes. Run each app on its own local port and create one Nginx server block per domain with proxy_pass to that port. Nginx routes requests by the Host header, so all sites share ports 80 and 443.

Subdomains or paths: which is better for multiple apps?

Subdomains ( api.example.com ) are simpler because each app keeps its own root path. Paths ( example.com/api/ ) work but require each app to know its base path.

What does the trailing slash in proxy_pass do?

With a URI in proxy_pass (e.g. http://127.0.0.1:8000/ ), Nginx replaces the matched location prefix with that URI, effectively stripping it. Without one ( http://127.0.0.1:8000 ), the full original path is passed unchanged.

Why does my app see 127.0.0.1 as every visitor's IP?

Pass X-Forwarded-For and X-Real-IP from Nginx and configure the framework to trust the proxy at 127.0.0.1. Then it reads the client address from those headers.

How do I proxy WebSockets with Nginx?

Use proxy_http_version 1.1 and pass Upgrade and Connection headers (via a map on $http_upgrade ), and raise proxy_read_timeout for long-lived connections.

How do I get SSL for several domains on one server?

Run certbot --nginx with multiple -d flags or once per site. Certbot edits each server block and sets up automatic renewal for all certificates.

Do I need to open each app's port in the firewall?

No. Only 80 and 443 (plus SSH) should be open. The apps listen on 127.0.0.1 and are reached only through Nginx.

Nginx, Caddy or Traefik?

All three are good reverse proxies. Nginx is the most widely documented and very efficient; Caddy issues certificates automatically with a simpler config; Traefik discovers Docker containers automatically. Pick the one that fits how you deploy.

What causes a 502 Bad Gateway from Nginx?

Nginx could not get a response from the upstream app: it is stopped, crashed, listening on a different port or address, or the socket path is wrong. Check the app status and Nginx's error log.

Which ServoMix VPS should I use to host several apps?

Add up the memory each app and database needs. Two or three small apps fit the 2 vCore / 8 GB plan; agencies hosting many sites or several databases usually choose 4 vCores / 16 GB or 8 vCores / 32 GB.

Was this article helpful?

Your feedback helps us continuously improve ServoMix guides.

Share this article

Know someone who may find this guide useful?

Nginx Reverse Proxy for Multiple Apps on One VPS | ServoMix