简体   繁体   中英

nginx proxy_pass and docker errors when container not up

What am I trying to achieve:

I want to start an nginx container and an API container. Group them under the same network and proxy_pass from the nginx to the API container (using the API container name in the nginx conf file).

What I did so far:

Created 2 docker-compose files (because they are not dependent in one another). If the API container is started before the nginx everything works fine. When starting the nginx while the API container is not running I get an error.

Error I got:

nginx    | 2019/09/29 07:28:19 [emerg] 1#1: host not found in upstream "api_server" in /etc/nginx/conf.d/default.conf:8
nginx    | nginx: [emerg] host not found in upstream "api_server" in /etc/nginx/conf.d/default.conf:8
nginx exited with code 1

Another issue is when the API container is crashing for some reason and restarted, the nginx also needs to be restarted before it comes back to work.

Workaround for now:

Start the API container before the nginx.

My (partially) nginx conf file is:

location /api_server {
        proxy_pass http://api_server:2300;
    }

Any ideas?

I recommend taking a look at jwilder/nginx-proxy as it covers what you are trying to achieve. He created an application that listens for container lifecycle action and updates /etc/nginx/conf.d/default.conf file accordingly. Also make sure that nginx-proxy container is on same docker network container that will be proxied into.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM