NGINX Reverse Proxy failing with Linked Docker Containers

允我心安 提交于 2019-12-18 15:57:11

问题


I have the following docker-compose.yml:

node1:
    build: ./node
    links:
        - redis
    ports:
        - "8080"
node2:
    build: ./node
    links:
        - redis
    ports:
        - "8080"
service1:
    build: ./service
    links:
        - redis
    ports:
        - "8383"
redis:
    image: redis
    ports:
        - "6379"
nginx:
    build: ./nginx
    links:
        - node1:node1
        - node2:node2
        - service1:service1
    ports:
        - "80:80"

After executing this and running docker ps I get the following:

080d9d7dc2e0        dockerworkflow_nginx:latest      "nginx -g 'daemon of   5 minutes ago       Up 5 minutes        0.0.0.0:80->80/tcp, 443/tcp   dockerworkflow_nginx_1
8c25bfdb9d00        dockerworkflow_node1:latest      "nodemon /src/index.   6 minutes ago       Up 6 minutes        0.0.0.0:33023->8080/tcp       dockerworkflow_node1_1
4ae817be2a63        dockerworkflow_service1:latest   "nodemon /src/index.   6 minutes ago       Up 6 minutes        0.0.0.0:33022->8383/tcp       dockerworkflow_service1_1
91ff238fe3f6        dockerworkflow_node2:latest      "nodemon /src/index.   6 minutes ago       Up 6 minutes        0.0.0.0:33021->8080/tcp       dockerworkflow_node2_1
fe0c7e02c860        redis:latest                     "/entrypoint.sh redi   6 minutes ago       Up 6 minutes        0.0.0.0:33020->6379/tcp       dockerworkflow_redis_1

Everything seems to be good so far.

The nginx.conf I am using looks like the following:

worker_processes 4;
events { worker_connections 1024; }

http {    
    server {
          listen 80;

          location / {
            proxy_pass http://node1;
          }

          location /a/ {
            proxy_pass http://node2;
          }

          location /b/ {
            proxy_pass http://service1;
          }
    }
}

All this should really be doing is the following:

If I enter http://{host-ip}/ then the node1 container is forwarded the request.

If I enter http://{host-ip}/a/ then the node2 container is forwarded the request.

If I enter http://{host-ip}/b/ then the service1 container is forwarded the request.

Right now, I am getting 502 Bad Gateway if I try anything.


回答1:


I was able to figure out the solution and it turned out to be something stupid that didn't show up in any logs and was difficult for me to come across.

Below is the updated nginx.conf file.

worker_processes 4;
events { worker_connections 1024; }

http {    

    upstream node_app {
        server node1:8080;
    }

    upstream service_app {
        server service1:8383;
    }

        server {
                listen 80;

            location / {
                proxy_pass http://node_app/;
                include /etc/nginx/proxy_params;
            }

             location /a/ {
                proxy_pass http://node_app/;
                include /etc/nginx/proxy_params;
             }

             location /b/ {
                proxy_pass http://service_app/;
                include /etc/nginx/proxy_params;
            }
        }
}

Not sure if the include is necessary at this point, but the trailing / at the end of the proxy_pass directive seem to do the trick at the end of the day.




回答2:


I think you should specify the ports within every proxy_pass.

worker_processes 4;
events { worker_connections 1024; }

http {    
    server {
          listen 80;

          location / {
            proxy_pass http://node1:8080;
          }

          location /a/ {
            proxy_pass http://node2:8080;
          }

          location /b/ {
            proxy_pass http://service1:8383;
          }
    }
}


来源:https://stackoverflow.com/questions/31331760/nginx-reverse-proxy-failing-with-linked-docker-containers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!