首页 > 解决方案 > Docker+Nginx 允许来自外部 docker 的上游 gunicorn

问题描述

我有一个正在运行的 docker-compose 容器Nginx,我想在同一台机器上与Flask+Gunicorn在 docker 外部运行时一起使用。当我在Docker 内部运行时,一切都docker-compose.yml links通过. 我该如何解决这个错误?FlaskGunicornDocker

我得到的错误是

2020/07/17 03:24:49 [error] 38#38: *1 connect() failed (111: Connection refused) while connecting to upstream, client: *.*.*.*, server: website.com, request: "GET / HTTP/2.0", upstream: "http://127.0.0.1:5000/", host: "website.com"

我正在gunicorn运行gunicorn --bind 0.0.0.0:5000 wsgi:app

我的 nginxwebsite.conf

upstream hello_server {
    #server 127.0.0.1:5000;
    server 0.0.0.0:5000;
}

    server {
        listen 80;
        # ...

        location ^~ /static/ {
            # Path of your static files
            root /var/www;
        }

        location / {
            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_pass http://frontends;
        }
    }
}

我的docker-compose.yml

version: '3'

services:

  web:
    image: nginx:latest
    ports:
      - 80:80/tcp
      - 443:443/tcp

标签: dockernginxflaskdocker-composegunicorn

解决方案


您可以使用HOSTIP,例如,上游在端口上运行5000,您只需要指向frontendHOST IP即可192.168.0.1

 server {
        listen 80;
        location ^~ /static/ {
            # Path of your static files
            root /var/www;
        }

        location / {
            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_pass http://192.168.0.1:5000;
        }
    }

或带有上游变量

upstream gunicornapp {
    server 192.168.43.84:3000;
}

    server {
        listen 80;
        location ^~ /static/ {
            # Path of your static files
            root /var/www;
        }

        location / {
            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_pass http://gunicornapp;
        }
    }


推荐阅读