Nginx upstream problems in docker environment
I have nginx as a proxy for a python application. To launch them I have a docker-compose.
services:
app:
...
nginx:
...
ports:
- "80:80"
- "443:443"
server {
...
location / {
proxy_pass http://app: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;
}
}
With this configuration I got two problems.
Do not name the service in docker-compose.yml as app
In nginx:alpine it resolves as 127.0.53.53. And if nginx is started before an app, it will always return 502 error. So I renamed it to backend.
The next error - [emerg] host not found in upstream
Now nginx fails when it’s starting before the backend, but it fails fast and it’s better. To solve this in local links or depends_on can be used.
In swarm mode these options are not supported, but it’s not a problem, because with a restart_policy it just continues to restart until the backend
is resolvable.