Docker nginx无法连接到在单独容器中运行的上游gunicorn

时间:2017-03-05 13:08:59

标签: nginx docker docker-compose gunicorn

我尝试了各种选择,例如expose的{​​{1}},bridgenetworks选项,但无法使用与在单独容器中运行的上游gunicorn的nginx连接,我收到{{1}来自nginx的错误。我不确定我到底错过了什么。以下是我的docker-compose文件:

502 Bad Gateway

nginx conf:

docker-compose

Gunicorn配置:

version: "3"

services:
  web:
    build: .
    container_name: web
    command: bash -c "/start_web.sh"
    restart: always
    depends_on:
      - worker
    ports:
      - "80:80"
      - "443:443"

  worker:
    build: .
    container_name: worker
    command: bash -c "/start_worker.sh"
    restart: always
    ports:
      - "8000:8000"

Circus ini文件:

web.ini

upstream worker {
    server 127.0.0.1:8000;
}

server {
    listen 80 default_server;

    location / {
        proxy_http_version 1.1;
        proxy_buffering off;
        proxy_set_header Host $http_host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Url-Scheme $scheme;
        proxy_redirect off;
        # Mitigate httpoxy attack
        proxy_set_header Proxy "";

        proxy_pass http://worker;
    }
}

worker.ini

import multiprocessing
import os

bind = '127.0.0.1:8000'

default_workers = multiprocessing.cpu_count() * 2 + 1
workers = os.getenv('GUNICORN_WORKERS', os.getenv('WEB_CONCURRENCY', default_workers))
worker_class = 'tornado'

# This is to fix issues with compressor package: broken offline manifest for
# custom domain. It randomly breaks, I think because of global variable inside.
preload_app = True

timeout = 200
graceful_timeout = 60
max_requests = 250
max_requests_jitter = max_requests
accesslog = '/tmp/gunicorn_access.log'
errorlog = '/tmp/gunicorn_error.log'

整个代码在github和存储库docker_test上都可用,以便于测试。

1 个答案:

答案 0 :(得分:3)

Gunicon配置:

bind = '127.0.0.1:8000'

这将绑定到loopback接口(仅限localhost),将其更改为0.0.0.0以绑定到容器中的每个可用接口。这将使它可以从nginx访问。

Nginx配置:

upstream worker {
    server 127.0.0.1:8000;
}

您需要将loopback ip更改为工作容器的DNSname/IP。我建议创建一个用户定义的网络,然后放入该网络中相关的所有容器,并通过DNS名称调用它们。您在默认桥接网络中没有内部DNS,因此遵循nginx配置不会工作。

upstream worker {
    server worker:8000;
}