docker - Docker 上的 Worker Tiemout Upstream Gunicorn (Bad Gateway 502)
问题描述
我正在尝试从我的 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 容器无法连接到 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
完毕!:)
推荐阅读
- javascript - 使用 addEventListener 添加元素并在下一次迭代中获取
- python-3.x - 带有 pyrouge 的 UnicodeDecodeError
- bash - 格式化整个项目,保留git历史
- python - 如何在 Django 中使用循环创建表单?
- python - 向新列添加 1 个值并与其他列一起对其执行操作
- javascript - Karma 测试:测量未测试代码的覆盖率
- java - IntelliJ IDEA 测试覆盖率窗口不显示
- java - 如何在不遇到 SecurityException 的情况下从 Scala 脚本引用 java.sql?
- if-statement - 在 Flutter 中使用 if 语句和图标
- c# - C# MVC 和 MS Graph 问题