This guide shows you how to configure Nginx server blocks (virtual hosts) to route traffic to multiple Docker containers on a single host. You’ll set up separate domain names or subdomains, each pointing to a different container, using Nginx as a reverse proxy. By the end, you’ll have a working setup where app1.example.com serves one container and app2.example.com serves another, all managed by Nginx on the host machine.
What You’ll Need
- A Linux server (Ubuntu/Debian assumed) with Nginx installed (
sudo apt install nginx). - Docker and Docker Compose installed and running.
- At least two Docker containers running, each exposing a port (e.g., 3000, 5000) on localhost or a specific interface.
- Domain names or subdomains pointing to your server’s public IP (for DNS resolution). For local testing, you can edit
/etc/hosts. - Basic familiarity with the command line and editing files with
sudo.
Step-by-Step
- Identify your container ports. Run
docker psto see which ports your containers are mapped to. Note the host port (e.g., 0.0.0.0:3000->80/tcp means the container’s port 80 is available on host port 3000). - Create Nginx server block files. For each domain, create a config file in
/etc/nginx/sites-available/. For example,app1.example.comandapp2.example.com. - Write the server block configuration. For each domain, add a
serverblock that listens on port 80 (and optionally 443 for SSL) and proxies requests to the container’s host port. A minimal HTTP config looks like:server { listen 80; server_name app1.example.com; location / { proxy_pass http://127.0.0.1:3000; 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; } }Replace
app1.example.comand3000with your actual domain and port. Repeat for each container. - Enable the server blocks. Create symbolic links from
sites-availabletosites-enabledusingsudo ln -s /etc/nginx/sites-available/app1.example.com /etc/nginx/sites-enabled/. Do this for each site. - Test the Nginx configuration. Run
sudo nginx -tto check for syntax errors. If it reports success, proceed; if not, fix the errors before continuing. - Reload Nginx. Use
sudo systemctl reload nginx(orsudo service nginx reload) to apply the changes without dropping connections. - Update DNS or hosts file. For production, ensure your domains resolve to your server’s IP. For local testing, add lines like
127.0.0.1 app1.example.comto/etc/hosts. - Access your containers. Visit
http://app1.example.comin a browser. You should see the content from the corresponding container. Repeat for other domains.
Common Mistakes
- Forgetting to enable the site – Creating a file in
sites-availabledoes nothing until you symlink it tosites-enabled. - Wrong port in proxy_pass – Use the host port, not the container’s internal port. If your container maps 8080:80, you must proxy to 8080.
- Missing proxy headers – Without
proxy_set_header Host $host, some apps may misbehave because they see the proxy’s address instead of the original host. - Not testing config – A typo in the config can cause Nginx to fail to restart. Always run
nginx -tfirst. - SSL not configured – If you’re using HTTPS, you need to add SSL certificate paths and listen on 443 ssl. Don’t forget to redirect HTTP to HTTPS.
Tips
- Use Docker Compose for consistent port mapping – Define fixed host ports in your
docker-compose.ymlto avoid surprises when containers restart. - Separate configs per site – Keeping each server block in its own file makes it easier to manage and disable sites.
- Enable gzip compression – Add
gzip on;and related directives to your Nginx config to speed up responses. - Set client_max_body_size – If your app handles file uploads, adjust this value (e.g., 10m) to avoid 413 errors.
- Use variables for upstreams – For more complex setups, define upstream blocks to handle load balancing or multiple replicas.
Troubleshooting
- 502 Bad Gateway – Nginx can’t reach your container. Check that the container is running (
docker ps) and that the port inproxy_passis correct. Also verify the container’s IP if you’re not using localhost. - 404 Not Found – The proxy is working, but the app can’t find the route. Check the app’s logs and ensure the
Hostheader is passed correctly. - Connection refused – The container isn’t listening on that port. Inspect the container’s port mapping with
docker port <container>. - Nginx won’t start – Run
nginx -tto see the error. Common issues are missing semicolons, duplicate server_names, or invalid paths. - SSL certificate errors – If you added SSL but the certificate files are missing or wrong, Nginx will fail. Use
sudo certbot --nginxto get a free Let’s Encrypt certificate.
Frequently Asked Questions
Can I use port 80 for multiple containers?
No, only one service can bind to port 80 on the host. That’s why Nginx acts as a reverse proxy: it listens on 80 and forwards requests based on the domain name to different internal ports.
What if I don’t have a domain name?
You can use localhost with different ports (e.g., localhost:8080) or edit your hosts file to simulate domains. For external access, you need a real domain.
How do I add SSL to my server blocks?
Use Certbot: sudo certbot --nginx -d app1.example.com. It will automatically modify your config and set up renewal. For manual setup, add listen 443 ssl; and specify ssl_certificate and ssl_certificate_key paths.
Can I use Docker networks instead of localhost ports?
Yes, you can place containers on a custom Docker network and use the container name as the hostname in proxy_pass (e.g., http://app1:80). This avoids exposing ports to the host.
Why is my static content not loading?
Ensure that your location block handles static files correctly. You may need to add try_files $uri $uri/ /index.html; for single-page apps or set the root for static assets.
How do I restart Nginx after changes?
Use sudo systemctl reload nginx (graceful) or sudo systemctl restart nginx (full restart). Reload is preferred for production to avoid downtime.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- How to Choose an Amplifier for Magico A3 Speakers
- How to Ban a Player from Your FiveM Server
- Pokemon Go Friend Codes: How to Add & Use Them33.33 XP/day
- Google Algorithm Change: Original Reporting Boost
- Create Folder for Selected Item on Mac: Full Guide
- Data Center Location Questions for AI Workloads