簡體   English   中英

nginx 在部署到 Azure App Service 時不起作用

[英]nginx not working when deployed to Azure App Service

我有一個程序,由 Fast API、Celery、Flower 和 nginx 組成,寫在 Python 中。我使用 docker 組合構建圖像並將它們作為多容器應用程序部署到 Azure App Service。

我的問題是部署到 Azure App Service 時無法訪問 Flower。 在本地,它工作正常。

我的 docker-compose-build.yml 用於構建圖像,然后將其推送到 ACR:

version: '3.4'

services:

  fast_api:
    container_name: fast_api
    build:
      context: .
      dockerfile: ./Dockerfile
    volumes:
      - .:/app
    ports:
      - 8080:8080
    depends_on:
      - redis

  celery_worker:
    container_name: celery_worker
    build: .
    command: celery -A app.celery.worker worker --loglevel=warning --pool=eventlet --concurrency=1000 -O fair 
    volumes:
      - .:/app
    environment:
      - CELERY_BROKER_URL=redis://redis:6379/0
      - CELERY_RESULT_BACKEND=redis://redis:6379/0
    depends_on:
      - fast_api
      - redis

  redis:
    container_name: redis
    image: redis:6-alpine 
    ports: 
      - 6379:6379

  flower:
    container_name: flower
    build: .
    command: celery -A app.celery.worker flower --port=5555 --url_prefix=flower
    ports:
      - 5555:5555
    environment:
      - CELERY_BROKER_URL=redis://redis:6379/0
      - CELERY_RESULT_BACKEND=redis://redis:6379/0
    depends_on:
      - fast_api
      - redis
      - celery_worker

  nginx:
    container_name: nginx
    build:
      context: .
      dockerfile: ./Dockerfile.nginx
    ports:
      - 80:80
    depends_on:
      - flower
      - fast_api

我的 docker-compose.yml 被 Azure 應用服務使用:

version: '3.4'

services:
  fast_api:
    container_name: fast_api
    image: name.azurecr.io/name_fast_api:latest
    volumes:
      - ${WEBAPP_STORAGE_HOME}/app
    ports:
      - 8080:8080
    depends_on:
      - redis

  celery_worker:
    container_name: celery_worker
    image: name.azurecr.io/name_celery_worker:latest
    command: celery -A app.celery.worker worker --loglevel=warning --pool=eventlet --concurrency=1000 -O fair 
    volumes:
      - ${WEBAPP_STORAGE_HOME}/app
    environment:
      - CELERY_BROKER_URL=redis://redis:6379/0
      - CELERY_RESULT_BACKEND=redis://redis:6379/0
    depends_on:
      - fast_api
      - redis

  redis:
    container_name: redis
    image: name.azurecr.io/redis:6-alpine 
    ports: 
      - 6379:6379

  flower:
    container_name: flower
    image: name.azurecr.io/name_flower:latest
    command: celery -A app.celery.worker flower --port=5555 --url_prefix=flower
    ports:
      - 5555:5555
    environment:
      - CELERY_BROKER_URL=redis://redis:6379/0
      - CELERY_RESULT_BACKEND=redis://redis:6379/0
    depends_on:
      - fast_api
      - redis
      - celery_worker

  nginx:
    container_name: nginx
    image: name.azurecr.io/name_nginx:latest
    # volumes:
    #   - ${WEBAPP_STORAGE_HOME}/etc/nginx/nginx.conf #   Storage:/etc/nginx/nginx.conf #/etc/nginx/ #/usr/share/nginx/html ##/etc/nginx/nginx.conf
    ports:
      - 80:80
    depends_on:
      - flower
      - fast_api

最初,在 docker-compose.yml 中,我直接從 Docker Hub 中拉取鏡像,然后將 nginx.conf 文件存儲在我掛載到 App Service 的 Azure File Share 中。 我懷疑 nginx.conf 文件未被 nginx 使用。因此,我通過創建 Dockerfile.nginx 來構建自定義 nginx 圖像,我將其復制到 nginx.conf 文件中。

FROM nginx:stable
COPY nginx.conf /etc/nginx/nginx.conf

我的 nginx.conf 文件:

events {
  worker_connections  4096;  ## Default: 1024
}

http {

server {
    listen 80 default_server;
    #listen [::]:80;
    server_name _; #app-name.azurewebsites.net;

    location / {
        proxy_pass http://fast_api:8080/;
        proxy_set_header Host $host;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
        location /test {
        proxy_pass http://fast_api:8080/;
        proxy_set_header Host $host;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
    location /flower {
        proxy_pass http://flower:5555/flower;
        proxy_set_header Host $host;
        proxy_redirect off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}
}

當我然后 go 到https://app-name.azurewebsites.net/flower我得到:

{“細節”:“未找到”}

我可以毫無問題地訪問https://app-name.azurewebsites.net/docs並且 API 完美運行。

有誰知道為什么部署到 Azure 后我無法訪問 Flower?

感謝您提供任何幫助和想法,因為我已經沒有什么可以嘗試的了!

問題已解決。 問題是所有 docker 服務的端口都暴露在外部,並且該端口 8080 被 fast_api 服務使用,這可能與 App Service 正在偵聽端口 8080 或 80 ( https://learn.microsoft. com/en-us/azure/app-service/configure-custom-container?pivots=container-linux#configure-port-number ) 解決方案是只公開帶有 port 參數的 nginx 服務的端口,所有其他服務應該只能通過 expose 參數在內部公開。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM