Self-Hosting & VPS

How to Run Multiple Apps on One VPS Using Docker Compose and Nginx Reverse Proxy

Running only one service on a 2 GB or 4 GB RAM VPS leaves capacity unused, yet exposing multiple Docker containers directly on random ports like :3000 or :5678 breaks HTTPS and creates security risks. The clean production pattern is binding every container strictly to 127.0.0.1 and placing Nginx in front as a TLS reverse proxy.

Architecture Overview

Each subdomain points to your VPS public IP (port 80/443). Nginx terminates HTTPS via Let’s Encrypt and forwards traffic internally over localhost to the matching container port:

  • app1.yourdomain.com → Nginx (:443) → 127.0.0.1:8081
  • app2.yourdomain.com → Nginx (:443) → 127.0.0.1:5678

Step 1: Bind Docker Ports Strictly to Localhost

In Docker Compose, writing ports: ["8081:80"] binds to 0.0.0.0, which bypasses UFW firewall rules on Linux! Always prefix with 127.0.0.1 so only Nginx on the same host can reach the container:

services:
  uptime-kuma:
    image: louislam/uptime-kuma:1
    restart: unless-stopped
    ports:
      - "127.0.0.1:3001:3001"
    volumes:
      - ./kuma-data:/app/data

Launch the stack and confirm the port listens only on loopback:

docker compose up -d
ss -tulpn | grep 3001

Step 2: Create an Nginx Reverse Proxy Virtual Host

Create a dedicated Nginx file for each app subdomain under /etc/nginx/sites-available/:

sudo nano /etc/nginx/sites-available/status.yourdomain.com
server {
    listen 80;
    listen [::]:80;
    server_name status.yourdomain.com;

    location / {
        proxy_pass http://127.0.0.1:3001;
        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;

        # Required for real-time dashboards & WebSockets
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

Enable the site, verify syntax, and reload Nginx:

sudo ln -s /etc/nginx/sites-available/status.yourdomain.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

Step 3: Add Free HTTPS SSL for the Subdomain

Once your subdomain’s DNS A record points to your VPS IP, issue a dedicated TLS certificate:

sudo certbot --nginx -d status.yourdomain.com --redirect

Troubleshooting Common Reverse Proxy Errors

1. 502 Bad Gateway

Nginx reached the server block, but nothing responded on the upstream port. Verify the container is running (docker compose ps), confirm the host port matches proxy_pass, and check container logs (docker compose logs --tail=50).

2. WebSockets Fail or UI Keeps Disconnecting

Real-time dashboards, n8n editors, and Uptime Kuma require proxy_http_version 1.1 plus the Upgrade and Connection headers shown above.

3. 413 Request Entity Too Large

When uploading files larger than 1 MB through a reverse proxy, add client_max_body_size 50M; inside the Nginx server block and reload Nginx.

Memory Budgeting Tip

Always verify available RAM with free -h before adding another container, and configure a 2 GB swap file on smaller VPS plans so temporary memory spikes never trigger Linux OOM kills.

Leave a Reply

Your email address will not be published. Required fields are marked *