【发布时间】:2021-02-22 18:29:39
【问题描述】:
我有两个 docker 容器:nginx_server 和 django_server(带有 UWSGI)和一个 posgres 带有 postgresql。现在问题是,我的nginx 设置似乎有点不对劲,因为如果我在curl 0.0.0.0:8000 (在主机上和容器内)任何时候都会给我一个502 Bad Gateway。
django_server 在 UWSGI 上运行,配置如下:
uwsgi --http-socket 0.0.0.0:80 \
--master \
--module label_it.wsgi \
--static-map /static=/app/label_it/front/static \
我能够在容器内外连接到它并获得正确的结果。
在 django_server 容器内,我可以 ping 和 curl nginx_server 得到肯定结果和 502 Bad Gateway。
curl django_server 在nginx_server 容器输出中:
<html lang="en">
<head>
<title>Bad Request (400)</title>
</head>
<body>
<h1>Bad Request (400)</h1><p></p>
</body>
</html>
(当curl localhost:80 而不是curl 0.0.0.0:80 / curl 127.0.0.1:80 时,我在django_server 容器中收到上述输出)
在我看来,nginx_server 没有正确地从 uwsgi 请求数据。
nginx_server配置:
upstream django {
server django_server:8000;
}
# configuration of the server
server {
# the port your site will be served on
listen 0.0.0.0:8000;
# the domain name it will serve for
server_name label_it.com; # substitute your machine's IP address or FQDN
charset utf-8;
# max upload size
client_max_body_size 75M; # adjust to taste
location /static {
alias /vol/static; # your Django project's static files - amend as required
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /etc/nginx/uwsgi_params; # the uwsgi_params file you installed
}
}
我研究了连接必须设置为0.0.0.0 才能从外部访问容器,并且已经完成。
另一件事,我在nginx_server 中调用django_server 通过它的容器名称,这应该允许mi 连接到docker network,因为我是docker-compose up。
主要问题是:
django_server 内容(请求uwsgi)无法从其他容器访问,但可以从主机访问。
另外(netstat -tlnp 这两个可用):
tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN -
tcp 0 0 0.0.0.0:8001 0.0.0.0:* LISTEN -
docker-compose.prod.yaml:
version: "3.8"
services:
db:
image: postgres
container_name: postgres
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
volumes:
- ./private/posgtesql/data/data:/var/lib/postgresql/data
app:
build: .
container_name: django_server
environment:
- DJANGO_SECRET_KEY=***
- DJANGO_DEBUG=False
- PATH=/app/scripts:${PATH}
- DJANGO_SETTINGS_MODULE=label_it.settings
volumes:
- .:/app
- static_data:/vol/static
ports:
- "8001:80"
depends_on:
- db
nginx:
container_name: nginx_server
build:
context: ./nginx
volumes:
- static_data:/vol/static
- ./nginx/conf.d:/etc/nginx/conf.d
- ./nginx/uwsgi_params:/etc/nginx/uwsgi_params
ports:
- "8000:80"
depends_on:
- app
volumes:
static_data:
编辑:
根据 David Maze 的说法,我已将端口映射更改为更简单且更易于跟踪。
django_server` is now `port 8001:8001
nginx_server` is now `port 8000:8000
分别:
django_server uwsgi: --http-socket 0.0.0.0:8001
nginx_server: listen 0.0.0.0:8000 and server django_server:8001
即使应用了上述更改,问题仍然存在。
【问题讨论】:
标签: python django docker networking docker-compose