You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Docker Compose部署Azure Web App仅显示Nginx首页求助

Azure Web App Docker Compose部署:Nginx无法代理Flask应用排查

我的Azure Web App通过Docker Compose(预览版)部署Flask+Nginx应用,镜像来自Docker Hub,但网站始终显示Nginx默认首页,无法代理到Flask应用:

Nginx默认首页

现有配置文件

docker-compose.yml

version: "3"

services:
  app:
    build:
      context: ./
    image: tracrux/irelandwindfarms
    command: gunicorn wsgi:app --bind 0.0.0.0:5000
    volumes:
      - ${WEBAPP_STORAGE_HOME}:/flaskapp/
      - ${WEBAPP_STORAGE_HOME}static:/flaskapp/static
    ports:
      - "5000"


  nginx:
    image: nginx:latest
    volumes:
      - ${WEBAPP_STORAGE_HOME}nginx.conf:/etc/nginx/nginx.conf:ro
      - ${WEBAPP_STORAGE_HOME}static:/flaskapp/static
    ports:
      - "80:80"
    depends_on:
      - app

volumes:
  static:

Dockerfile

FROM python:3.12 # set the working directory 
WORKDIR /flaskapp # install dependencies 
COPY ./requirements.txt /flaskapp 
RUN pip install --no-cache-dir --upgrade -r requirements.txt # Copy your Flask application code 
COPY . . 
EXPOSE 5000

nginx.conf

events {
    worker_connections 1000;
}

upstream my_site_proxy {
  server app:5000;
}

http {
    server {
    listen 80;
    listen [::]:80;
    server_name  https://irelandwindfarms.azurewebsites.net/;
        location / {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_pass http://my_site_proxy/;
            proxy_redirect off;
        }

        location /static/ {
            alias /flaskapp/static/;
        }
    }
}

部署日志(显示正常启动)

2024-03-15T18:00:11.445Z INFO - Starting multi-container app..
2024-03-15T18:00:12.038Z INFO - Pulling image: tracrux/irelandwindfarms
2024-03-15T18:00:13.417Z INFO - latest Pulling from tracrux/irelandwindfarms
2024-03-15T18:00:13.427Z INFO - Digest: sha256:dbb401c5936d150717e74541d1fc53419dcdd330929599eccbb4c8e00dd321a4
2024-03-15T18:00:13.429Z INFO - Status: Image is up to date for tracrux/irelandwindfarms:latest
2024-03-15T18:00:13.439Z INFO - Pull Image successful, Time taken: 1 Seconds
2024-03-15T18:00:13.488Z INFO - Starting container for site
2024-03-15T18:00:13.490Z INFO - docker run -d --name irelandwindfarms_app_0_cad2ad5c -e WEBSITES_ENABLE_APP_SERVICE_STORAGE=true -e WEBSITE_SITE_NAME=irelandwindfarms -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=irelandwindfarms.azurewebsites.net -e WEBSITE_INSTANCE_ID=11af5b4880bcafd0e33c899dd84747a89c767943adf0db58295f85ef997b95d4 tracrux/irelandwindfarms gunicorn wsgi:app --bind 0.0.0.0:5000
2024-03-15T18:00:13.504Z INFO - Logging is not enabled for this container.
Please use https://aka.ms/linux-diagnostics to enable logging to see container logs here.
2024-03-15T18:00:13.928Z INFO - Pulling image: nginx:latest
2024-03-15T18:00:15.054Z INFO - latest Pulling from library/nginx
2024-03-15T18:00:15.101Z INFO - Digest: sha256:6db391d1c0cfb30588ba0bf72ea999404f2764febf0f1f196acd5867ac7efa7e
2024-03-15T18:00:15.105Z INFO - Status: Image is up to date for nginx:latest
2024-03-15T18:00:15.120Z INFO - Pull Image successful, Time taken: 1 Seconds
2024-03-15T18:00:15.154Z INFO - Starting container for site
2024-03-15T18:00:15.155Z INFO - docker run -d -p 8362:80 --name irelandwindfarms_nginx_0_cad2ad5c -e WEBSITES_ENABLE_APP_SERVICE_STORAGE=true -e WEBSITE_SITE_NAME=irelandwindfarms -e WEBSITE_AUTH_ENABLED=False -e WEBSITE_ROLE_INSTANCE_ID=0 -e WEBSITE_HOSTNAME=irelandwindfarms.azurewebsites.net -e WEBSITE_INSTANCE_ID=11af5b4880bcafd0e33c899dd84747a89c767943adf0db58295f85ef997b95d4 nginx:latest
2024-03-15T18:00:15.157Z INFO - Logging is not enabled for this container.
Please use https://aka.ms/linux-diagnostics to enable logging to see container logs here.
2024-03-15T18:00:22.966Z INFO - Started multi-container app
2024-03-15T18:00:22.972Z INFO - Initiating warmup request to container irelandwindfarms_nginx_0_cad2ad5c for site irelandwindfarms
2024-03-15T18:00:22.974Z INFO - Container irelandwindfarms_nginx_0_cad2ad5c for site irelandwindfarms initialized successfully and is ready to serve requests.

排查思路

1. 核心问题:Nginx未加载自定义配置

网站显示Nginx首页,说明Nginx使用默认配置而非你挂载的nginx.conf,原因可能是:

  • Azure环境变量路径不匹配:Azure中${WEBAPP_STORAGE_HOME}指向/var/aspnetcore/wwwroot,而非你本地设置的./,如果该路径下未上传nginx.conf,挂载会失效。
  • 挂载路径格式错误:${WEBAPP_STORAGE_HOME}nginx.conf会拼接成/var/aspnetcore/wwwrootnginx.conf,正确格式应为${WEBAPP_STORAGE_HOME}/nginx.conf(添加斜杠)。

2. 修正配置文件

docker-compose.yml

  • 移除build块:指定image后build会被忽略,属于冗余配置,直接删除。
  • 修正volumes路径:给挂载路径添加斜杠,确保路径正确:
    # app服务部分
    volumes:
      - ${WEBAPP_STORAGE_HOME}/:/flaskapp/
      - ${WEBAPP_STORAGE_HOME}/static:/flaskapp/static
    # nginx服务部分
    volumes:
      - ${WEBAPP_STORAGE_HOME}/nginx.conf:/etc/nginx/nginx.conf:ro
      - ${WEBAPP_STORAGE_HOME}/static:/flaskapp/static
    

nginx.conf

  • 修正server_name:去掉https://仅保留域名,同时添加default_server确保匹配所有请求:
    server {
      listen 80 default_server;
      listen [::]:80 default_server;
      server_name irelandwindfarms.azurewebsites.net;
      # 其余配置不变
    }
    

3. 上传配置文件到Azure存储

通过Azure门户进入Web App -> 高级工具 -> Kudu,导航到/var/aspnetcore/wwwroot目录,上传你的nginx.conf和static文件夹,确保挂载的文件存在。

4. 开启容器日志排查细节

部署日志提示未启用容器日志,无法查看Nginx和Flask的运行错误。开启日志后可确认:

  • Nginx是否成功加载自定义配置
  • Nginx连接Flask容器(app:5000)是否有报错
  • Gunicorn是否正常启动并绑定5000端口

5. 验证容器间网络连通性

进入Nginx容器内部,执行curl app:5000,确认能否访问Flask应用。如果无法访问,检查Flask容器是否正常运行或网络配置是否存在问题。

6. 简化部署测试

先去掉Nginx,直接部署Flask容器并设置端口为80,确认Flask应用能正常访问后,再逐步添加Nginx代理,缩小排查范围。

内容的提问来源于stack exchange,提问作者user101

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 20:14:58