【问题标题】:Docker+Nginx allow upstream gunicorn from outside dockerDocker+Nginx 允许来自外部 docker 的上游 gunicorn
【发布时间】:2020-07-17 12:09:42
【问题描述】:

我有一个运行 Nginx 的 docker-compose 容器,我想将它与在同一台机器上运行的 Flask + Gunicorn 一起使用。当我在 Docker 内部运行时,一切都通过docker-compose.ymllinks 运行良好,但是当我在Docker 之外运行Flask + Gunicorn 时,我得到一个我想要解决的错误。我该如何解决这个错误?

我得到的错误是

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 --bind 0.0.0.0:5000 wsgi:app 运行gunicorn

我的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

【问题讨论】:

    标签: docker nginx flask docker-compose gunicorn


    【解决方案1】:

    您可以使用HOST IP,例如上游在端口5000上运行,您只需将frontend指向HOST 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;
            }
        }
    
    

    【讨论】:

      猜你喜欢
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-17
      • 1970-01-01
      • 2016-12-07
      • 2021-02-02
      • 2019-01-04
      相关资源
      最近更新 更多