nginx 和 flask docker 容器出现 504 错误
504 error with nginx and flask docker containers
两天的工作,我仍然卡住了。我是 运行 独立的 nginx 和应用程序容器。应用程序容器有一个 flask 应用程序,它在端口 8000 上运行一个 gunicorn 进程。
每次我导航到 localhost:8080
,这是映射到本地主机上的 nginx 端口 80,我得到一个加载屏幕和一个 nginx 504 错误。
这是我在终端上看到的:
docker-compose.yml
version: '2'
services:
web:
restart: always
build: ./web_app
expose:
- "8000"
ports:
- "8000:8000"
volumes:
- ./web_app:/data/web
command: /usr/local/bin/gunicorn web_interface:app -w 4 -t 90 --log-level=info -b :8000 --reload
depends_on:
- postgres
nginx:
restart: always
build: ./nginx
ports:
- "8080:80"
volumes_from:
- web
depends_on:
- web
postgres:
restart: always
image: postgres:latest
volumes_from:
- data
volumes:
- ./postgres/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
- ./backups/postgresql:/backup
expose:
- "5432"
data:
restart: always
image: alpine
volumes:
- /var/lib/postgresql
tty: true
nginx.conf
server {
listen 80;
server_name localhost;
charset utf-8;
location /static/ {
alias /data/web/crm/web_interface;
}
location = /favicon.ico {
alias /data/web/crm/web_interface/static/favicon.ico;
}
location / {
proxy_pass http://web:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
nginx Dockerfile
FROM nginx:latest
RUN rm /etc/nginx/conf.d/default.conf
COPY ./nginx.conf /etc/nginx/conf.d/nginx.conf
如果需要,我会提供更多信息以帮助解决我正在努力解决的这个问题。
NGINX 响应 504 表示网关超时,因为 NGINX 无法连接后端服务器。因此,您可以在 proxy_pass 目录中找到问题。
估计NGINX无法解析web
域名,有两种解决方法:
代替IP
位置/{
proxy_pass http://<real_ip>:8000;
}
使用上游
上游网络{
server <real_ip>;
}
位置/{
proxy_pass http://web:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
好的,经过三天的抨击我的头,我重新开始了。重建应用程序容器和 运行 gunicorn。
从那里我能够确定 gunicorn 进程超时是因为数据库主机名不正确。失败没有通过我的应用程序返回错误,而是静悄悄的。
我通过链接 postgres 容器和 web 容器修复了这个问题。在我的代码中,我能够使用 "postgres"(容器的名称)作为 postgres 主机名。
检查外部主机的地址。
两天的工作,我仍然卡住了。我是 运行 独立的 nginx 和应用程序容器。应用程序容器有一个 flask 应用程序,它在端口 8000 上运行一个 gunicorn 进程。
每次我导航到 localhost:8080
,这是映射到本地主机上的 nginx 端口 80,我得到一个加载屏幕和一个 nginx 504 错误。
这是我在终端上看到的:
docker-compose.yml
version: '2'
services:
web:
restart: always
build: ./web_app
expose:
- "8000"
ports:
- "8000:8000"
volumes:
- ./web_app:/data/web
command: /usr/local/bin/gunicorn web_interface:app -w 4 -t 90 --log-level=info -b :8000 --reload
depends_on:
- postgres
nginx:
restart: always
build: ./nginx
ports:
- "8080:80"
volumes_from:
- web
depends_on:
- web
postgres:
restart: always
image: postgres:latest
volumes_from:
- data
volumes:
- ./postgres/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
- ./backups/postgresql:/backup
expose:
- "5432"
data:
restart: always
image: alpine
volumes:
- /var/lib/postgresql
tty: true
nginx.conf
server {
listen 80;
server_name localhost;
charset utf-8;
location /static/ {
alias /data/web/crm/web_interface;
}
location = /favicon.ico {
alias /data/web/crm/web_interface/static/favicon.ico;
}
location / {
proxy_pass http://web:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
nginx Dockerfile
FROM nginx:latest
RUN rm /etc/nginx/conf.d/default.conf
COPY ./nginx.conf /etc/nginx/conf.d/nginx.conf
如果需要,我会提供更多信息以帮助解决我正在努力解决的这个问题。
NGINX 响应 504 表示网关超时,因为 NGINX 无法连接后端服务器。因此,您可以在 proxy_pass 目录中找到问题。
估计NGINX无法解析web
域名,有两种解决方法:
代替IP
位置/{
proxy_pass http://<real_ip>:8000;
}
使用上游
上游网络{
server <real_ip>;
}
位置/{
proxy_pass http://web:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
好的,经过三天的抨击我的头,我重新开始了。重建应用程序容器和 运行 gunicorn。
从那里我能够确定 gunicorn 进程超时是因为数据库主机名不正确。失败没有通过我的应用程序返回错误,而是静悄悄的。
我通过链接 postgres 容器和 web 容器修复了这个问题。在我的代码中,我能够使用 "postgres"(容器的名称)作为 postgres 主机名。
检查外部主机的地址。