keepcalculator

06 Numbers and Powers

Nginx Server Blocks for Docker: Setup Guide

Card No. 06·73762 · Updated · Standard formula, shown below

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

  1. Identify your container ports. Run docker ps to 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).
  2. Create Nginx server block files. For each domain, create a config file in /etc/nginx/sites-available/. For example, app1.example.com and app2.example.com.
  3. Write the server block configuration. For each domain, add a server block 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.com and 3000 with your actual domain and port. Repeat for each container.

  4. Enable the server blocks. Create symbolic links from sites-available to sites-enabled using sudo ln -s /etc/nginx/sites-available/app1.example.com /etc/nginx/sites-enabled/. Do this for each site.
  5. Test the Nginx configuration. Run sudo nginx -t to check for syntax errors. If it reports success, proceed; if not, fix the errors before continuing.
  6. Reload Nginx. Use sudo systemctl reload nginx (or sudo service nginx reload) to apply the changes without dropping connections.
  7. 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.com to /etc/hosts.
  8. Access your containers. Visit http://app1.example.com in 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-available does nothing until you symlink it to sites-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 -t first.
  • 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.yml to 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 in proxy_pass is 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 Host header 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 -t to 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 --nginx to 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

The whole Numbers and Powers drawer →