【问题标题】:"Welcome to Nginx!" - Docker-Compose, using uWSGI, Flask, nginx“欢迎来到 Nginx!” - Docker-Compose,使用 uWSGI、Flask、nginx
【发布时间】:2023-03-06 13:18:01
【问题描述】:

我的问题:

我正在使用 Ubuntu 18.04 和基于 docker-compose 的解决方案,其中包含两个 Docker 映像,一个用于处理 Python/uWSGI一个用于我的 NGINX 反向代理。无论我更改什么,WSGI 似乎总是无法检测到我的默认应用程序。每当我运行 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

(所有代码 sn-ps 已被简化以帮助隔离问题)

这是我的 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

【问题讨论】:

    标签: docker nginx


    【解决方案1】:

    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,果然,它已被删除。但是,我仍然收到“欢迎使用 Nginx!”当我尝试将 Web 浏览器导航到“localhost:5000”时飞溅。关于可能发生的事情还有其他建议吗?
    • @EthanHill 好吧,我在本地注意到的一件事是直接点击localhost/index.html 仍然会加载欢迎页面,因为try_files 会在它尝试转到您的 Flask 应用程序之前找到该文件。您也可以尝试删除静态索引文件,或将 app.conf 上的根目录更改为其他文件夹以避免冲突。您的 nginx 访问日志的输出也将有助于诊断。我复制了你的 nginx 容器来重现和解决问题,但我可能遗漏了一些东西。
    • 这个建议没有解决我的问题。但是,它可能可以解决其他人的类似问题,因此我将其标记为已解决。
    • @EthanHill 您能否以不同的方式解决您的问题?另一种想法是挂载你的 app.conf 来覆盖 /etc/nginx/nginx.conf 本身,以避免处理覆盖提供的默认配置。
    • 我扔掉了我的整个样板文件,并用 tiangolo/uwsgi-nginx-docker 映像替换了它——考虑到时间限制,这似乎是最简单的解决方案。
    猜你喜欢
    • 2022-11-13
    • 2012-08-02
    • 2019-02-26
    • 2018-11-15
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    相关资源
    最近更新 更多