我正在开发一个带有PostgreSQL数据库的Django应用程序,并且我正在使用带有Docker的NGINX + Gunicorn。
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"]
这是我的要求.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似乎也在工作!
答案 0 :(得分:0)
您需要将proxy_pass用于上游。
if (!-f $request_filename) {
proxy_pass http://myapp_server;
break;
}