在Azure Web APP上运行FastAPI - 网关超时

问题描述 投票:0回答:1

我正在尝试在 Azure Web App 上运行 FastAPI。但出现以下错误:

天蓝色 504.0 网关超时

当我查看日志时,出现以下错误:

enter image description here

有什么建议会导致这种情况吗?

name: Build and deploy Python app to Azure Web App - segato-fast-api

on:
  push:
    branches:
      - dev
  workflow_dispatch:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - name: Set up Python version
        uses: actions/setup-python@v5
        with:
          python-version: '3.12'

      - name: Create and start virtual environment
        run: |
          python -m venv venv
          source venv/bin/activate
      
      - name: Install dependencies
        run: pip install -r requirements.txt
        
      # Optional: Add step to run tests here (PyTest, Django test suites, etc.)

      - name: Zip artifact for deployment
        run: zip release.zip ./* -r

      - name: Upload artifact for deployment jobs
        uses: actions/upload-artifact@v4
        with:
          name: python-app
          path: |
            release.zip
            !venv/
  deploy:
    runs-on: ubuntu-latest
    needs: build
    environment:
      name: 'Production'
      url: ${{ steps.deploy-to-webapp.outputs.webapp-url }}
    permissions:
      id-token: write #This is required for requesting the JWT

    steps:
      - name: Download artifact from build job
        uses: actions/download-artifact@v4
        with:
          name: python-app

      - name: Unzip artifact for deployment
        run: unzip release.zip

      
      - name: Login to Azure
        uses: azure/login@v2
        with:
          client-id: ${{ secrets.AZUREAPPSERVICE_CLIENTID_B7A634A93F634F01A9CEAF7F209FCFD4 }}
          tenant-id: ${{ secrets.AZUREAPPSERVICE_TENANTID_085412552C7445619A1BD2878A692F63 }}
          subscription-id: ${{ secrets.AZUREAPPSERVICE_SUBSCRIPTIONID_B0970C502F86483D9551067BC403A7CA }}

      - name: 'Deploy to Azure Web App'
        uses: azure/webapps-deploy@v3
        id: deploy-to-webapp
        with:
          app-name: 'segato-fast-api'
          slot-name: 'Production'
          
Add or update the Azure App Service build and deployment workflow config · hafnia-dna/dna-port-api@561d351 
from fastapi import FastAPI,Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi_azure_auth import SingleTenantAzureAuthorizationCodeBearer
import uvicorn
from fastapi import FastAPI, Security

from settings import Settings


from contextlib import asynccontextmanager
from typing import AsyncGenerator

from enums import settings

from routers.router1 import router1_router
from routers.docs import tags_metadata

from utils.azure.azure_auth import AzureAuth
from utils.azure.azure_logging import azure_logger 

azure_auth = AzureAuth()
@asynccontextmanager 
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]: 
    await azure_auth.load_openid_config() 
    yield


app = FastAPI(
    openapi_tags=tags_metadata,
    swagger_ui_oauth2_redirect_url='/oauth2-redirect',
    swagger_ui_init_oauth={
        'usePkceWithAuthorizationCodeGrant': True,
        'clientId': settings.OPENAPI_CLIENT_ID,
        'scopes': settings.SCOPE_NAME,
    },
)

app.include_router(router1_router)

if settings.BACKEND_CORS_ORIGINS:
    app.add_middleware(
        CORSMiddleware,
        allow_origins=[str(origin) for origin in settings.BACKEND_CORS_ORIGINS],
        allow_credentials=True,
        allow_methods=['*'],
        allow_headers=['*'],
)

if __name__ == '__main__':
        uvicorn.run('main:app', reload=True)

配置:

enter image description here

azure azure-web-app-service fastapi
1个回答
0
投票

这些504错误大多意味着容器没有启动。可能的代码在启动时崩溃了。我的建议是检索比您在

log stream
中看到的更多的错误日志。我通常做的是通过
VS code
azure app service
扩展连接到应用程序服务,然后你会在这里看到详细的容器日志文件。它通常会告诉您最初的崩溃异常。您应该能够使用 kudo 控制台看到相同的日志。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.