“欢迎来到 Nginx!” - Docker-Compose,使用uWSGI、Flask、nginx

Eth*_*ill 4 nginx docker

我的问题:

欢迎来到 Nginx! 溅

我正在使用 Ubuntu 18.04 和基于 docker-compose 的解决方案,其中包含两个 Docker 映像,一个用于处理 Python/uWSGI,另一个用于我的 NGINX 反向代理。无论我进行什么更改,WSGI 似乎始终无法检测到我的默认应用程序。每当我运行docker-compose up并导航到 时,localhost:5000我都会得到上面的默认启动画面。

完整的程序似乎可以在我们的 CentOS 7 机器上运行。然而,当我尝试在 Ubuntu 测试机上执行它时,我只能得到“Welcome to NGINX!” 页。

目录结构:

 /app
  - app.conf
  - app.ini
  - app.py
  - docker-compose.py
  - Dockerfile-flask
  - Dockerfile-nginx
  - requirements.txt
  /templates
Run Code Online (Sandbox Code Playgroud)

(所有代码片段均已简化,以帮助隔离问题)

这是我的 docker 回溯的示例:

时钟烧瓶_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)
Run Code Online (Sandbox Code Playgroud)

时钟_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
Run Code Online (Sandbox Code Playgroud)

这是我的 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
Run Code Online (Sandbox Code Playgroud)

Dockerfile 烧瓶:

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" ]
Run Code Online (Sandbox Code Playgroud)

Dockerfile-nginx

FROM nginx:latest
EXPOSE 80
COPY app.conf /etc/nginx/conf.d
Run Code Online (Sandbox Code Playgroud)

应用程序配置文件

server {
    listen 80;
    root /usr/share/nginx/html;
    location / { try_files $uri @app; }
    location @app {
        include uwsgi_params;
        uwsgi_pass flask:5000;
    }
}
Run Code Online (Sandbox Code Playgroud)

应用程序.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')
Run Code Online (Sandbox Code Playgroud)

应用程序.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
Run Code Online (Sandbox Code Playgroud)

Rob*_*bel 6

nginx 映像附带一个主配置文件 ,/etc/nginx/nginx.conf它加载conf.d文件夹中的每个 conf 文件 - 包括本例中的敌人,即 stock /etc/nginx/conf.d/default.conf。其内容如下(为了简洁而进行了一些修剪):

server {
  listen 80;
  server_name localhost;

  location / {
    root /usr/share/nginx/html;
    index index.html index.htm;
  }
}
Run Code Online (Sandbox Code Playgroud)

因此,您的app.conf配置和此配置均处于活动状态。不过,这个默认选项获胜的原因是因为server_name它具有(而您的缺少)指令——当您点击 时localhost:5000,nginx 会根据主机名进行匹配并将您的请求发送到那里。

要轻松解决此问题,您只需删除 Dockerfile-nginx 中的该文件即可:

RUN rm /etc/nginx/conf.d/default.conf
Run Code Online (Sandbox Code Playgroud)