我正在尝试使用Nginx容器创建一个堆栈,接收请求作为我的带有Gunicorn的Django容器的反向代理(现在不用担心静态文件或数据库),但我不断得到一个 Nginx上出现502 Bad Gateway 错误。
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
nginx的Dockerfile:
FROM nginx:stable
COPY nginx.conf /etc/nginx/nginx.conf
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;
}
}
}
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"]
我尝试手动创建网络并将两个容器放在其中,运行以下命令。
docker run --network marcos_net -h app marcos
docker run --network marcos_net -h nginx -p 80:80 nginx
使用docker-compose.yml文件:
version: '3'
services:
app:
image: myrepo/app
nginx:
image: myrepo/nginx
ports:
- "80:80"
depends_on:
- app
我还试图像文档(https://docs.docker.com/engine/tutorials/networkingcontainers/)中所说的那样访问容器以互相ping通,并且它们实际上位于同一网络中。还使用docker network inspect
命令进行检查。
两种方式都记录上面打印的相同错误。
我已经看到了一些类似的问题,但它们来自不同的docker-compose文件版本,并且它们并没有为我解决问题,所以我不认为这是同样的问题
答案 0 :(得分:1)
看起来你的nginx没有正确连接到gunicorn。尝试将端口参数放入docker compose文件中。
services:
app:
image: myrepo/app
ports:
- "8000:8000"
此外,这里有一个如何设置它的例子。