代码之家  ›  专栏  ›  技术社区  ›  user_78361084

nginx反向代理到主机中的应用程序

  •  0
  • user_78361084  · 技术社区  · 6 年前

    我有一个应用程序在5000端口的Docker外运行。我试图通过docker compose在nginx中运行反向代理,但无法与主机的端口5000通信。在docker-compose.yml文件中,我有:

    ports:
      - 80:80
      - 443:443
      - 5000:5000
    

    当我尝试运行时,我得到:

    ERROR: for nginx  Cannot start service nginx: driver failed programming external connectivity on endpoint nginx (374026a0d34c8b6b789dcd82d6aee6c4684b3201258cfbd3fb18623c4101): Error starting userland proxy: listen tcp 0.0.0.0:5000: bind: address already in use
    

    如果我发表评论 - 5000:5000 我得到:

    [error] 6#6: *1 connect() failed (111: Connection refused) while connecting to upstream
    

    如何从Docker nginx容器连接到主机中已经运行的应用程序?

    编辑:

    我的nginx.conf文件

    user www-data;
    worker_processes auto;
    pid /run/nginx.pid;
    
    events {
        worker_connections 768;
    }
    
    http {
        upstream mysite {
            server 0.0.0.0:5000;
        }
    
        server {
            listen 80;
            server_name localhost;
    
            location / {
            proxy_pass http://mysite;
            }
        }
    }
    

    当我尝试curl localhost时的响应是 502 Bad Gateway .应用程序本身和curl 127.0.0.1:5000在主机上响应良好。

    编辑2: 我也试过 solution found here 但我明白了 nginx: [emerg] host not found in upstream "docker" .Docker是我主机的主机名。

    编辑3: 我的docker-compose.yml

    version: '3'
    services:
      simple:
        build: ./simple
        container_name: simple
        ports:
          - 80:80
          - 443:443
    

    我的文件:

    FROM nginx
    COPY nginx.conf /etc/nginx/nginx.conf
    
    EXPOSE 80 443
    CMD ["nginx", "-g", "daemon off;", "-c", "/etc/nginx/nginx.conf"]
    

    编辑:

    我正在通过Linux中的“hostname”命令获取计算机主机。

    1 回复  |  直到 6 年前
        1
  •  3
  •   rdRahul    6 年前


    1. 你需要给它一个它能到达的地址。解决它需要你
    2. 找到你的IP地址。 通过打开应用程序
    3. 在nginx配置文件中