欢迎使用Nginx! - Docker-Compose,使用uWSGI、Flask和nginx

4

我的问题:

Welcome to Nginx! splash

我正在使用 Ubuntu 18.04 和基于 Docker Compose 的解决方案,包括两个 Docker 镜像,一个用于处理 Python/uWSGI,另一个用于我的 NGINX 反向代理。无论我如何更改,似乎 uWSGI 无法检测到我的默认应用程序。每次我运行 docker-compose up 并导航到 localhost:5000,我都会看到上述默认闪屏。

完整的程序似乎在我们的 CentOS 7 机器上工作正常。然而,当我尝试在我的 Ubuntu 测试机上执行它时,我只能得到“欢迎来到 NGINX!”页面。

目录结构:

 /app
  - app.conf
  - app.ini
  - app.py
  - docker-compose.py
  - Dockerfile-flask
  - Dockerfile-nginx
  - requirements.txt
  /templates

(所有代码段都已简化以帮助分离问题)

以下是我的Docker回溯示例:

clocker_flask_1

[uWSGI] getting INI configuration from app.ini
current working directory: /app
detected binary path: /usr/local/bin/uwsgi
uwsgi socket 0 bound to TCP address 0.0.0.0:5000 fd 3
*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***
*** Operational MODE: preforking+threaded ***
WSGI app 0 (mountpoint='') ready in 1 seconds on interpreter 0x558072010e70 pid: 1 (default app)

clocker_nginx_1

/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Configuration complete; ready for start up

这是我的docker-compose.yaml文件:

# docker-compose.yml
version: '3'
services:
  
  flask:
    image: webapp-flask
    build:
      context: .
      dockerfile: Dockerfile-flask
    volumes:
      - "./:/app:z"
      - "/etc/localtime:/etc/localtime:ro"
    environment:
      - "EXTERNAL_IP=${EXTERNAL_IP}"

  nginx:
    image: webapp-nginx
    build:
      context: .
      dockerfile: Dockerfile-nginx
    ports:
      - 5000:80
    depends_on:
      - flask

Dockerfile-flask:

FROM python:3
ENV APP /app
RUN mkdir $APP
WORKDIR $APP
EXPOSE 5000
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD [ "uwsgi", "--ini", "app.ini" ]

Dockerfile-nginx

FROM nginx:latest
EXPOSE 80
COPY app.conf /etc/nginx/conf.d

app.conf

server {
    listen 80;
    root /usr/share/nginx/html;
    location / { try_files $uri @app; }
    location @app {
        include uwsgi_params;
        uwsgi_pass flask:5000;
    }
}

app.py

# Home bit
@application.route('/')
@application.route('/home', methods=["GET", "POST"])
def home():
    return render_template(
        'index.html',
        er = er
    )

if __name__ == "__main__":
    application.run(host='0.0.0.0')

app.ini

[uwsgi]
protocol = uwsgi
module = app
callable = application
master = true
processes = 2
threads = 2
socket = 0.0.0.0:5000
vacuum = true   
die-on-term = true
max-requests = 1000
1个回答

6
nginx镜像自带一个主要的配置文件,/etc/nginx/nginx.conf,它会加载conf.d文件夹中的每个conf文件,包括这种情况下的你的敌人,一个默认的/etc/nginx/conf.d/default.conf。它读起来如下(为了简洁起见进行了修整):
server {
  listen 80;
  server_name localhost;

  location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
  }
}

因此,您的app.conf和此配置文件都是活动状态。然而默认的那个获胜了,原因是它有server_name指令(而你没有)- 当你访问localhost:5000时,nginx会根据主机名进行匹配并将请求发送到那里。
要轻松解决此问题,您可以在Dockerfile-nginx中删除该文件。
RUN rm /etc/nginx/conf.d/default.conf

我已经在我的Dockerfile-nginx中添加了以下内容: /
EXPOSE 80 / RUN rm /etc/nginx/conf.d/default.conf / COPY app.conf /etc/nginx/conf.d /
我还通过SSH登录到我的clocker_nginx_1 Docker镜像并安装了nano,结果它被删除了。然而,当我尝试在Web浏览器中导航到“localhost:5000”时,仍然会出现“欢迎使用Nginx!”的提示。您有其他建议吗?
- Ethan Hill
1
@EthanHill 嗯,我在本地注意到的一件事是,直接访问 localhost/index.html 仍然会加载欢迎页面,因为 try_files 会在尝试进入 Flask 应用程序之前找到该文件。您可以尝试删除静态索引文件,或将 app.conf 中的根更改为不同的文件夹以避免冲突。诊断问题时,nginx 访问日志的输出也会很有帮助。我复制了您的 nginx 容器以重现和解决问题,但可能会漏掉某些内容。 - Robert Nubel
这个建议并没有解决我的问题。但是,它有可能解决其他人类似的问题,所以我将其标记为已解决。 - Ethan Hill
@EthanHill,你能以其他方式解决你的问题吗?另一个想法是将你的app.conf挂载到/etc/nginx/nginx.conf上,以避免处理提供默认配置的覆盖。 - Robert Nubel
我放弃了整个样板文件,并用tiangolo/uwsgi-nginx-docker镜像替换它 - 鉴于时间限制,这似乎是最简单的解决方案。 - Ethan Hill

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接