NGINX反向代理与链接Docker容器失败

时间:2015-07-10 03:02:23

标签: nginx docker reverse-proxy docker-compose

我有以下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"

执行此操作并运行docker ps后,我得到以下内容:

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

到目前为止,一切似乎都很好。

我使用的nginx.conf如下所示:

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;
          }
    }
}

这一切应该真正做到的是:

如果我输入http://{host-ip}/,则node1容器会转发请求。

如果我输入http://{host-ip}/a/,则node2容器会转发请求。

如果我输入http://{host-ip}/b/,则service1容器会转发请求。

现在,如果我尝试任何事情,我将获得502 Bad Gateway

2 个答案:

答案 0 :(得分:6)

我能够找出解决方案,结果证明这是一个愚蠢的东西,没有出现在任何日志中,我很难遇到。

以下是更新的nginx.conf文件。

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;
            }
        }
}

此时不确定是否需要包含,但/指令末尾的尾随proxy_pass似乎可以在当天结束时执行此操作。

答案 1 :(得分:-2)

我认为你应该在每个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;
          }
    }
}