How to Configure Nginx as a Reverse Proxy for Docker Containers

An nginx reverse proxy docker containers setup puts one Nginx server in front of your apps. Visitors reach Nginx on port 80 or 443, and Nginx forwards each request to the right container on your private Docker network. The containers never need a public port of their own, which keeps your host firewall rules short and your URLs clean.

This guide builds that setup from scratch: a shared network, two test containers, one Nginx config with two virtual hosts, HTTPS, and the header fixes that WebSocket apps and file uploads need. If Docker is not installed yet, start with the guide to install Docker Compose on Ubuntu 24.04 and come back here.

What a Reverse Proxy Does in a Docker Setup

A reverse proxy sits between visitors and your apps. It reads the Host header, matches it against a server_name, and passes the request to the container that serves that site. One public IP can serve many sites this way, because the domain name decides the destination, not the port.

The proxy also gives you one place to add HTTPS, rate limits, and access logs. Your app containers stay focused on running the app and do not need to know about certificates or public ports.

There is one rule to respect: Nginx must reach each container by container name, not by localhost. Inside the Nginx container, localhost means the Nginx container itself. Container names work only when Nginx and the app share a Docker network, which is the first step below.

Set Up the Shared Docker Network

Create a network called web and attach every proxied app to it. Nginx joins the same network, so it can resolve container names through Docker’s built-in DNS.

docker network create web
docker network ls

If you are unsure which network mode fits a given app, the explainer on Docker networking: bridge, host, and macvlan covers the trade-offs. For a reverse proxy, a user-defined bridge network like web is the normal choice: containers get DNS names, and ports stay private unless you publish them.

Do not publish app ports to the host in this setup. A container started with -p 3000:3000 is reachable directly, which defeats the point of the proxy. Let Nginx be the single public entry point.

Run Two Test Containers

Two small web servers make the routing easy to test. Each one joins the web network and serves a different page, so a wrong route shows up at once.

docker run -d --name site-a --network web nginxdemos/hello
docker run -d --name site-b --network web nginxdemos/hello

Check that both containers run:

docker ps --format '{{.Names}} {{.Status}}'
docker network inspect web --format '{{range .Containers}}{{.Name}} {{end}}'

A docker container reverse proxy setup fails most often at this stage, before Nginx is even involved. If a container is missing from the network, Nginx returns a 502 error for that site. Fix the network first, then look at the Nginx config.

Write the nginx reverse proxy docker containers Config

Now run Nginx itself on the same network, with ports 80 and 443 published and a config folder mounted in:

docker run -d --name nginx-proxy --network web 
  -p 80:80 -p 443:443 
  -v ./nginx/conf.d:/etc/nginx/conf.d 
  -v ./nginx/certs:/etc/nginx/certs 
  nginx:alpine

Create ./nginx/conf.d/sites.conf with one server block per site. Note the proxy_pass line: it points at the container name and the port the app listens on inside the container, not a published host port.

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

    location / {
        proxy_pass http://site-a:80;
        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 b.example.com;

    location / {
        proxy_pass http://site-b:80;
        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;
    }
}

The four proxy_set_header lines matter. Without them, the app sees Nginx’s IP as the visitor, and it cannot tell whether the original request used HTTP or HTTPS. Apps that build absolute URLs or log visitors need the real values, so keep these headers in every location block that proxies traffic.

This is the core of an nginx proxy_pass docker pattern: proxy_pass plus forwarded headers, repeated per site. Reload Nginx and test with curl against each domain, or add the domains to your local hosts file for a test before DNS points at the server.

docker exec nginx-proxy nginx -t
docker exec nginx-proxy nginx -s reload
curl -H 'Host: a.example.com' http://YOUR_SERVER_IP/
curl -H 'Host: b.example.com' http://YOUR_SERVER_IP/

Route Multiple Domains to Different Containers

Adding a third site means adding one more server block and one more container on the web network. Nothing else changes. Each app stays isolated in its own container, and routing lives in one readable config folder.

If you would rather manage routes through a web interface than hand-written config files, the comparison of Traefik versus Nginx Proxy Manager for Docker beginners shows where a GUI tool saves time and where raw Nginx gives you more control. The manual setup in this article is the better choice when you need custom headers, WebSocket upgrades, or per-location rules that a GUI hides.

For apps that need a path prefix instead of a subdomain, add a second location block inside the same server block:

location /app/ {
    proxy_pass http://site-b:80/;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-Proto $scheme;
}

Watch the trailing slash on proxy_pass. With the slash, Nginx strips the matched /app/ prefix before forwarding. Without it, the full original path goes to the container, and most apps return 404 for paths they do not serve.

Add HTTPS With Certbot

Plain HTTP works for testing, but public sites need HTTPS. The cleanest path on a Docker host is to run Certbot once per domain and let it write certificates into the folder Nginx mounts as /etc/nginx/certs. The walkthrough for free SSL certificates for Docker apps with Let’s Encrypt covers the Certbot commands and renewal in detail.

Once certificates exist, extend each server block with a TLS listener and a redirect from HTTP:

server {
    listen 80;
    server_name a.example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name a.example.com;

    ssl_certificate /etc/nginx/certs/a.example.com/fullchain.pem;
    ssl_certificate_key /etc/nginx/certs/a.example.com/privkey.pem;

    location / {
        proxy_pass http://site-a:80;
        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;
    }
}

Test renewal before you trust it. Certificates expire after about 90 days, so a renewal timer that never ran will take a site down quietly. Run the dry-run command from the SSL guide and check the timer with systemctl list-timers on the host.

Handle Headers, WebSockets, and Large Uploads

Two classes of app break behind a default proxy config: real-time apps and apps that accept uploads. Both need a few extra lines.

WebSocket apps need an upgrade handshake. Add these lines to the location block that serves the app, or to a dedicated /ws/ location if only one path uses sockets:

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 86400;

File uploads need a bigger body limit. Nginx defaults to 1 MB, so a 5 MB image upload fails with a 413 error. Set the limit in the server block to match what your app accepts:

client_max_body_size 50m;

An nginx docker compose reverse proxy setup makes these tweaks easier to keep. Put the Nginx service and the app services in one Compose file, attach them all to the same network, and keep the config files in the same project folder as the Compose file. The next person who opens the project sees the proxy config next to the apps it serves, instead of hunting through a host-wide config folder.

Test, Reload, and Fix Common Errors

Work through this checklist whenever a site misbehaves after a config change:

  • 502 Bad Gateway: the app container is down, not on the shared network, or listening on a different internal port than proxy_pass names. Check docker ps and the container logs first.
  • 404 from the app: the path forwarded to the container is wrong. Check the trailing slash rule on proxy_pass and compare the app’s access log with the requested URL.
  • Redirect loop: the app thinks the request is HTTP while Nginx serves HTTPS. Confirm X-Forwarded-Proto is set and the app trusts the proxy header.
  • 413 Request Entity Too Large: raise client_max_body_size in the right server block, then reload.
  • Wrong site served: no server_name matched, so Nginx used the default server. Check spelling of the domain and confirm DNS points at this host.

Always run nginx -t inside the container before reloading. A syntax error stops the reload and the old config keeps serving, which is confusing if you expect a change to take effect. The test output names the file and line number, so fix that line and test again.

With the network, the per-site blocks, HTTPS, and these fixes in place, you can extend the setup site by site without touching the apps themselves. Add a container, add a block, test, reload, and the new site is live behind the same Nginx front door.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.