How to Proxy to Docker Container in Nginx: Simple Guide
To proxy to a Docker container in
nginx, use the proxy_pass directive pointing to the container's IP address or service name and port. Ensure Docker networking allows access and configure nginx to forward requests to the container's exposed port.Syntax
The basic syntax to proxy requests in nginx to a Docker container uses the proxy_pass directive inside a location block. You specify the target URL, which is usually the container's IP or service name with port.
- proxy_pass: URL of the Docker container service (e.g.,
http://container_name:portorhttp://127.0.0.1:port). - location: URL path to match incoming requests.
- listen: Port on which
nginxlistens for requests.
nginx
server {
listen 80;
location / {
proxy_pass http://container_name:8080;
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;
}
}Example
This example shows how to configure nginx to proxy HTTP requests to a Docker container named myapp running on port 5000. It assumes both nginx and the container are on the same Docker network, so myapp can be used as the hostname.
nginx
server {
listen 80;
location / {
proxy_pass http://myapp:5000;
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;
}
}Output
When you visit http://your-nginx-server/, requests are forwarded to the Docker container 'myapp' on port 5000, serving the container's response transparently.
Common Pitfalls
- Using localhost or 127.0.0.1 incorrectly: If
nginxruns outside Docker,localhostpoints to the host, not the container. Use the container IP or Docker network alias instead. - Docker network not shared: Ensure
nginxand the container are on the same Docker network for hostname resolution. - Missing proxy headers: Not setting headers like
HostorX-Forwarded-Forcan cause issues with backend apps. - Port mismatch: Proxy to the container's exposed port, not the internal Docker port.
nginx
## Wrong way (using localhost from host machine when nginx is outside Docker) server { listen 80; location / { proxy_pass http://127.0.0.1:5000; # This fails if container is inside Docker } } ## Right way (using Docker network alias) server { listen 80; location / { proxy_pass http://myapp:5000; # Works if nginx and container share network } }
Quick Reference
Remember these tips when proxying to Docker containers with nginx:
- Use Docker network aliases or container IPs, not
localhost, whennginxruns outside the container. - Set proxy headers to preserve client info.
- Match the container's exposed port in
proxy_pass. - Test connectivity with
curlorpinginside thenginxcontainer.
Key Takeaways
Use the proxy_pass directive with the Docker container's network name and port inside nginx.
Ensure nginx and the Docker container share a Docker network for hostname resolution.
Set proxy headers like Host and X-Forwarded-For to forward client information correctly.
Avoid using localhost in proxy_pass if nginx runs outside Docker; use container IP or network alias.
Verify connectivity between nginx and the container before deploying the proxy configuration.