[英]Can't connect Ngxin reverse proxy to Gunicorn on Django container (Docker)
I've being trying to make a stack with a Nginx container receiving requests as a reverse proxy to my Django container with Gunicorn (not worrying about static files or DB for now), but I keep getting a 502 Bad Gateway error on Nginx.我一直在尝试使用 Nginx 容器创建一个堆栈,该容器接收请求作为我的 Django 容器与 Gunicorn 的反向代理(现在不担心静态文件或数据库),但我一直在 Nginx 上收到502 Bad Gateway错误。
Nginx logs: Nginx 日志:
2017/04/28 12:10:10 [notice] 1#1: using the "epoll" event method
2017/04/28 12:10:10 [notice] 1#1: nginx/1.12.0
2017/04/28 12:10:10 [notice] 1#1: built by gcc 6.3.0 20170205 (Debian 6.3.0-6)
2017/04/28 12:10:10 [notice] 1#1: OS: Linux 4.4.0-63-generic
2017/04/28 12:10:10 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2017/04/28 12:10:10 [notice] 1#1: start worker processes
2017/04/28 12:10:10 [notice] 1#1: start worker process 7
2017/04/28 12:10:10 [notice] 1#1: start worker process 8
2017/04/28 12:10:10 [notice] 1#1: start worker process 9
172.19.0.1 - - [28/Apr/2017:12:10:17 +0000] "GET / HTTP/1.1" 502 575 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36"
2017/04/28 12:10:17 [error] 7#7: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 172.19.0.1, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.53.53:8000/", host: "localhost"
2017/04/28 12:10:28 [info] 7#7: *1 client closed connection while waiting for request, client: 172.19.0.1, server: 0.0.0.0:80
2017/04/28 12:10:28 [info] 7#7: *4 client closed connection while waiting for request, client: 172.19.0.1, server: 0.0.0.0:80
2017/04/28 12:10:28 [info] 7#7: *3 client closed connection while waiting for request, client: 172.19.0.1, server: 0.0.0.0:80
Gunicorn:独角兽:
[2017-04-28 12:09:02 +0000] [1] [INFO] Starting gunicorn 19.7.1
[2017-04-28 12:09:02 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
[2017-04-28 12:09:02 +0000] [1] [INFO] Using worker: sync
[2017-04-28 12:09:02 +0000] [9] [INFO] Booting worker with pid: 9
[2017-04-28 12:09:02 +0000] [11] [INFO] Booting worker with pid: 11
[2017-04-28 12:09:02 +0000] [13] [INFO] Booting worker with pid: 13
Dockerfile for nginx:用于 nginx 的 Dockerfile:
FROM nginx:stable
COPY nginx.conf /etc/nginx/nginx.conf
nginx.conf file: nginx.conf 文件:
worker_processes 3;
events {
worker_connections 1024;
}
error_log /var/log/nginx/error.log debug;
http {
sendfile on;
upstream app_servers {
server app:8000;
}
server {
listen 80;
location / {
proxy_pass http://app_servers;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
}
}
}
Dockerfile for django: django 的 Dockerfile:
FROM python:3.5-slim
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD . /code
EXPOSE 8000
RUN pip install -r requirements.txt
RUN python manage.py collectstatic --noinput
CMD ["gunicorn", "marcos.wsgi:application", "--bind", "0.0.0.0:8000", "--workers", "3"]
I've tried to create a network manually and placing both containers in it running the following commands.我尝试手动创建一个网络并将两个容器放入其中运行以下命令。
docker run --network marcos_net -h app marcos
docker run --network marcos_net -h nginx -p 80:80 nginx
And with a docker-compose.yml file:并使用 docker-compose.yml 文件:
version: '3'
services:
app:
image: myrepo/app
nginx:
image: myrepo/nginx
ports:
- "80:80"
depends_on:
- app
I've also tried to access the containers to ping one another like it says in the docs ( https://docs.docker.com/engine/tutorials/networkingcontainers/ ) and they are in fact in the same network.我还尝试访问容器以 ping 彼此,就像文档( https://docs.docker.com/engine/tutorials/networkingcontainers/ )中所说的那样,它们实际上在同一个网络中。 Also checked with the docker network inspect
command.还使用docker network inspect
命令进行了docker network inspect
。
Both ways log the same error printed above.两种方式都会记录上面打印的相同错误。
I've seen some similar questions, but they are from different docker-compose file versions, and they didn't solve the problem for me, so I don't think it is the same problem.我见过一些类似的问题,但它们来自不同的 docker-compose 文件版本,它们没有为我解决问题,所以我认为这不是同一个问题。
Looks like your nginx isn't connected to gunicorn properly.看起来您的 nginx 没有正确连接到 gunicorn。 Try putting port parameter in your docker compose file.尝试将端口参数放入 docker compose 文件中。
services:
app:
image: myrepo/app
ports:
- "8000:8000"
Also, here is an example of how you may set it up.此外,这里有一个示例,说明如何设置它。
https://github.com/wiamsuri/django-gunicorn-nginx-docker https://github.com/wiamsuri/django-gunicorn-nginx-docker
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.