【问题标题】:Worker Tiemout Upstream Gunicorn on Docker (Bad Gateway 502)Docker 上的 Worker Timeout Upstream Gunicorn (Bad Gateway 502)
【发布时间】:2021-03-07 17:55:34
【问题描述】:

我正在尝试从我的 Ubuntu 服务器上运行基于 Django、Docker、Nginx 和 Gunicorn 构建的平台。

在问你之前,我正在阅读我的问题,我在我的 nginx.conf 上做了:

location / {
    proxy_read_timeout 300s;
    proxy_connect_timeout 75s;
    ...
}

然后,在我的 Gunicorn 设置中:

CMD ["gunicorn", "--bind", "0.0.0.0:8000", "-t 90", "config.wsgi:application"]

问题仍然存在,服务器总是返回:502 Bad Gateway。当我尝试访问:

http://34.69.240.210:8000/admin/

从浏览器,服务器重定向到

http://34.69.240.210:8000/admin/login/?next=/admin/

但显示错误:

我的Dockerfile

FROM python:3.8
  
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

RUN mkdir /code
WORKDIR /code
COPY . /code/

RUN pip install -r requirements.txt

CMD ["gunicorn", "--bind", "0.0.0.0:8000", "-t 90", "config.wsgi:application"]

我的 docker-compose.yml

version: "3.8"
  
services:
  django_app:
    build: .
    volumes:
      - static:/code/static
      - .:/code

  nginx:
    image: nginx:1.15
    ports:
      - 8000:8000
    volumes:
      - ./config/nginx/conf.d:/etc/nginx/conf.d
      - static:/code/static
    depends_on:
      - django_app

volumes:
  .:
  static:

我的 Nginx 文件:

upstream django_server {
    server django_app:8000 fail_timeout=0;
}

server {
    listen 8000;
    server_name 34.69.240.210;

    keepalive_timeout 5;
    client_max_body_size 4G;

    location / {
        proxy_read_timeout 300s;
        proxy_connect_timeout 75s;
        proxy_pass http://django_server;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    }
}

知道我能做些什么来解决它吗?

谢谢。

【问题讨论】:

    标签: docker ubuntu nginx gunicorn


    【解决方案1】:

    好吧,记录一下。

    我的问题是数据库连接。我的 Docker 容器无法连接到 potsgres 本地数据库。

    所以,我在 postgresql.conf 中添加了这一行:

    listen_addresses = '*'
    

    然后,我将这一行添加到 pg_hba.conf 中

    host all all 0.0.0.0/0 md5
    

    重启 Postgres:

    sudo service postgresql restart
    

    我的主机postgres ip:

    172.17.0.1
    

    从 docker 内部测试:

    psql -U myuser -d databasename -h 172.17.0.1 -W
    

    完成! :)

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 2014-04-30
      • 1970-01-01
      • 2016-04-20
      • 2020-09-01
      • 2021-04-22
      • 2018-10-30
      • 2020-12-14
      • 2018-08-03
      相关资源
      最近更新 更多