我真是茫然,出了什么问题。
我在单独的Docker容器中运行nginx和gunicorn。 Nginx在端口80上运行,并在8000上转发到gunicorn。我从nginx收到502错误。如果我打到8000端口,那么我的gunicorn服务器会收到200响应。
如果我卸下nginx泊坞窗并在笔记本电脑上启动nginx服务器,将流量转发到端口8000,则一切正常。 dockerized Nginx服务器设置会出错吗?
docker-compose.yml
version: '3.3'
services:
nginx:
build: ./nginx
ports:
- '1337:80'
depends_on:
- django-api
django-api:
build:
context: .
dockerfile: Dockerfile
command: gunicorn my_app.wsgi:application --log-level verbose --log-file=/var/log/guni --access-logfile=/var/log/gunia --bind 0.0.0.0:8000 --forwarded-allow-ips "*"
ports:
- '8000:8000'
depends_on:
- mysql
mysql:
image: mysql:5
volumes:
- type: bind
source: ./db/docker-entrypoint-initdb.d
target: /docker-entrypoint-initdb.d
- type: volume
source: mysql-data
target: /var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: toor
networks:
- backend
ports:
- '3306:3306'
volumes:
mysql-data:
static_volume:
networks:
backend:
gunicorn Dockerfile
FROM python:3
ENV PYTHONUNBUFFERED 1
ENV DJANGO_SETTINGS_MODULE dori.settings.dev
WORKDIR /app
COPY . /app
RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt
# Port to expose
EXPOSE 8000
nginx.conf
upstream hello_django {
server localhost:8000;
}
server {
listen 80;
location / {
proxy_pass http://hello_django;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
}
答案 0 :(得分:3)
问题出在您的nginx配置文件中。您正在尝试将来自nginx容器的请求转发到端口8000
上的容器本身,只需将您的nginx配置文件更改为此:
upstream hello_django {
server django-api:8000;
}
server {
listen 80;
location / {
proxy_pass http://hello_django;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
}
}
django-api 是docker-compose在网络中创建的容器的服务名称,当您尝试连接到其他服务时,您的其他服务可以在本地网络中对其进行解析。