【问题标题】:504 error with nginx and flask docker containersnginx 和烧瓶 docker 容器出现 504 错误
【发布时间】:2017-11-24 07:38:46
【问题描述】:

两天的工作,我仍然卡住了。我正在运行单独的 nginx 和应用程序容器。应用容器有一个烧瓶应用,在 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

如果需要,我会提供更多信息,以便在我正在努力解决的这个问题上获得一些帮助。

【问题讨论】:

  • 可以直接在localhost:8000访问gunicorn服务器吗?
  • 不,我试过没有成功。
  • 我可以使用暴露的端口和映射来访问它是有道理的,但我仍然不能。
  • 这里粘贴的 docker-compose 是您正在使用的吗?没有可以依赖的服务postgres。无论如何,我删除了那行,我无法重现。
  • 感谢时间,我添加了 docker-compose 文件的 postgres 部分。您使用了一个烧瓶应用程序并且能够通过localhost:80localhost:8000 访问它?

标签: python nginx docker flask http-status-code-504


【解决方案1】:

NGINX 响应 504 表示网关超时,因为 NGINX 无法连接后端服务器。因此,您可以在proxy_pass 目录中找到问题。

我猜NGINX无法解析web域名,有两种解决方案:

  1. 而不是IP

    位置/{

    proxy_pass http://<real_ip>:8000;
    

    }

  2. 使用上游

    上游网络{

    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;
    

    }

【讨论】:

    【解决方案2】:

    好吧,经过三天的猛烈抨击,我从头开始。重建应用程序容器并运行 gunicorn。

    从那里我能够确定 gunicorn 进程超时,因为数据库主机名不正确。失败并没有通过我的应用程序返回错误,而是静默了。

    我通过链接 postgres 容器和 web 容器解决了这个问题。在我的代码中,我能够使用“postgres”(容器的名称)作为 postgres 主机名。

    检查外部主机的地址。

    【讨论】:

      猜你喜欢
      • 2019-09-29
      • 1970-01-01
      • 2019-04-01
      • 1970-01-01
      • 2018-08-21
      • 2014-09-06
      • 2017-11-23
      • 1970-01-01
      • 2013-05-14
      相关资源
      最近更新 更多