NGINX 反向代理因链接的 Docker 容器而失败 [英] NGINX Reverse Proxy failing with Linked Docker Containers

查看:57
本文介绍了NGINX 反向代理因链接的 Docker 容器而失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 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 后,我得到以下信息:

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.

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

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:

如果我输入 http://{host-ip}/ 那么 node1 容器会转发请求.

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

如果我输入 http://{host-ip}/a/ 然后 node2 容器转发请求.

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

如果我输入 http://{host-ip}/b/ 然后 service1 容器转发请求.

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

现在,如果我尝试任何操作,我会收到 502 Bad Gateway.

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

推荐答案

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

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.

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

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

不确定此时是否需要包含,但是 proxy_pass 指令末尾的尾随 / 似乎在一天结束时起到了作用.

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.

这篇关于NGINX 反向代理因链接的 Docker 容器而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆