【问题标题】:Docker-compose: nginx does not work with django and gunicornDocker-compose:nginx 不适用于 django 和 gunicorn
【发布时间】:2015-06-29 21:32:02
【问题描述】:

我一直在尝试在 docker-compose 中设置一个环境,其中有几个容器:

  • 姜戈
  • Nginx
  • Postgres
  • 数据库数据
  • 存储

我使用了以下配置:

 app:
  restart: always
  build: src
  expose:
    - "8000"
  links:
    - postgres:postgres
  volumes_from:
    - storage_files_1
  env_file: .env
  command: gunicorn barbell.wsgi:application \
            -b 0.0.0.0:8000 -w 4

nginx:
  restart: always
  build: nginx
  ports:
    - "80:80"
    - "443:443"
  volumes_from:
    - storage_files_1
  links:
    - app:app

postgres:
  restart: always
  image: postgres:latest
  volumes_from:
    - storage_data_1
  ports:
    - "5432:5432"

我的 nginx 站点启用配置文件如下所示:

server {

    listen 80;
    server_name localhost;
    error_log /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;

    location /static {
        alias /static/;
        autoindex on;
    }

    location / {
         proxy_pass http://app:8000;
         proxy_set_header X-Forwarded-Host $server_name;
         proxy_set_header X-Real-IP $remote_addr;
         add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
    }
}

而且它不起作用 - nginx 总是返回 502,但完美地提供静态文件。我也尝试了与 uwsgi 相同的设置,但没有运气。 但是,当我将 Django 与 nginx 结合起来并从同一个容器中提供所有内容时,一切正常(同样,在 uwsgi 和 gunicorn 上)。

知道我错过了什么吗?

更新

这里是 nginx 日志:

*1 connect() failed (111: Connection refused) while connecting to upstream,
client: 172.17.42.1, server: 0.0.0.0, request: "GET / HTTP/1.1", upstream:   
"http://172.17.1.75:8000/", host: "localhost"

【问题讨论】:

  • error_log 在 nginx 中是什么样子的?
  • 今天晚些时候我会添加日志。

标签: nginx docker uwsgi gunicorn docker-compose


【解决方案1】:

事实证明,Gunicorn 是罪魁祸首。将其配置放入文件解决了这个问题。

gunicorn_config.py 与 manage.py 放在同一文件夹中:

bind = "0.0.0.0:8000"
loglevel = "INFO"
workers = "4"
reload = True

errorlog = "/var/log/gunicorn/error.log"
accesslog = "/var/log/gunicorn/access.log"

还有 docker-compose.yml 的一些变化:

app:
  restart: always
  build: src
  expose:
    - "8000"
  links:
    - postgres:postgres
  volumes_from:
    - storage_files_1
  env_file: .env
  command: gunicorn --config=gunicorn_config.py barbell.wsgi:application

现在它可以正常工作了。

【讨论】:

    【解决方案2】:

    因此,我没有看到您对错误日志以及您可能会或可能不会发生的事情的任何进一步反馈;但是,我已将您的示例简化为最简单的示例,以演示 Docker+Django+NGINX 的工作原理:

    见:docker-django-test

    注意:这是在我的一些使用 autodock 的基础架构上运行的

    autodock:
        image: prologic/autodock
        ports:
            - "1338:1338/udp"
            - "1338:1338/tcp"
        volumes:
            - /var/run/docker.sock:/var/run/docker.sock
    
    autodockhipache:
        image: prologic/autodock-hipache
        links:
            - autodock
            - hipache:redis
    
    hipache:
        image: hipache
        ports:
            - 80:80
            - 443:443
    

    见:A Docker-based mini-PaaS

    【讨论】:

    • 这并不是问题的真正答案,因为它从基础架构中删除了 gunicorn/uwsgi。虽然它通常有效,但 django runserver should not 可用于此类部署。
    • 我真的不认为这很重要。 gunicorn 和 uwsgi 都将为 nginx 创建 TCP 侦听器以进行代理传递。 TCP 是 TCP 是 TCP。
    猜你喜欢
    • 2018-03-09
    • 2021-08-29
    • 2020-11-25
    • 2017-10-31
    • 2019-03-22
    • 2013-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多