【发布时间】:2018-10-30 01:27:50
【问题描述】:
我正在开发一个带有 PostgreSQL 数据库的 Django 应用程序,并且我正在使用 NGINX + Gunicorn 和 Docker。
PostgreSQL、NGINX 和 Gunicorn 位于与网络通信的不同容器上。我可以使用docker-compose build 构建我的应用程序,但是当我使用docker-compose up 执行它并在浏览器中查看我的应用程序时,我得到的只是502 Bad Gateway 错误,而在终端中我看到的是:
nginx_1 | 127.0.0.1 - - [20/May/2018:01:53:01 +0000] "GET /home HTTP/1.0" 502 174 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0" "172.23.0.1"
nginx_1 | 172.23.0.1 - - [20/May/2018:01:53:01 +0000] "GET /home HTTP/1.1" 502 174 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/60.0" "-"
我的 docker-compose 看起来像这样:
version: '3'
services:
# Database container
db:
image: postgres:10
volumes:
- db_volume:/var/lib/postgresql/data
env_file:
- ./.env
networks:
- db_network
# Web app container with gunicorn
webapp:
build: .
env_file: ./.env
volumes:
- .:/opt/services/webapp/src
- static:/opt/services/webapp/static
- media:/opt/services/webapp/media
networks:
- db_network
- nginx_network
depends_on:
- db
# NGINX (Reverse proxy) container
nginx:
image: nginx:1.13
ports:
- 8000:80
volumes:
- ./config/nginx/conf.d:/etc/nginx/conf.d
- static:/opt/services/webapp/static
- media:/opt/services/webapp/media
networks:
- nginx_network
depends_on:
- webapp
networks:
db_network:
driver: bridge
nginx_network:
driver: bridge
volumes:
db_volume:
static:
media:
这是我的 Dockerfile:
# Start with an official Python image
FROM python:3.6
ENV PYTHONUNBUFFERED 1
RUN mkdir -p /opt/services/webapp/src
WORKDIR /opt/services/webapp/src
# Install dependencies
ADD requirements.txt /opt/services/webapp/src
RUN pip install -r requirements.txt
COPY . /opt/services/webapp/src
# Expose port 8000
EXPOSE 8000
# Default command to run when starting the container
CMD ["gunicorn", "-c", "config/gunicorn/conf.py", "--bind", ":8000", "--chdir", "myapp", "myapp.wsgi:application"]
这是我的 requirements.txt:
bcrypt==3.1.4
cffi==1.11.5
Django==2.0.4
Pillow==5.1.0
psycopg2==2.7.4
psycopg2-binary==2.7.4
pycparser==2.18
pytz==2018.4
six==1.11.0
django-phonenumber-field==2.0.0
gunicorn==19.8.1
gevent==1.3.1
还有我的 NGINX 配置:
# Upstream server
upstream myapp_server {
server webapp:8000;
}
# Main server
server {
listen 80;
server_name localhost;
location /static/ {
alias /opt/services/webapp/static/;
}
location /media/ {
alias /opt/services/webapp/media/;
}
location / {
proxy_pass http://myapp_server;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://127.0.0.1;
break;
}
}
}
我不确定是什么导致了这个问题,但看起来 gunicorn 没有正确检测到我的应用程序,NGINX 正在运行,而 PostgreSQL 似乎也在运行!
【问题讨论】:
-
我也遇到了类似的问题,你有解决办法吗?
标签: django docker nginx docker-compose gunicorn