使用 ASP.NET Core 8 Web API 的 Azure 应用服务 (linux) 会导致 http 403 错误

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

对于一个学校项目,我正在使用 bicep 和 github 操作制作 CI/CD 管道。

二头肌文件非常基本,创建 3 个资源。

  • 应用程序服务
  • 服务器场
  • 应用洞察
@description('Specifies the location for resources.')
param location string = resourceGroup().location

var envResourceNamePrefix = 'staging-testwebsite'

@description('Deployment name id.')
param deploymentNameId string = '0000000000'

// create azure insights
resource appInsights 'Microsoft.Insights/components@2020-02-02' = {
  name: '${envResourceNamePrefix}-app-insights'
  location: location
  kind: 'web'
  properties: {
    Application_Type: 'web'
    publicNetworkAccessForIngestion: 'Enabled'
    publicNetworkAccessForQuery: 'Enabled'
  }
}

var appInsightsKey = appInsights.properties.InstrumentationKey

resource appServicePlan 'Microsoft.Web/serverfarms@2023-01-01' = {
  name: '${envResourceNamePrefix}-app-sp'
  location: location
  kind: 'linux'
  sku: {
    name: 'B1'
  }
  properties: {
    reserved: true
  }
}

resource webApp 'Microsoft.Web/sites@2023-01-01' = {
  name: '${envResourceNamePrefix}-app'
  location: location
  kind: 'web'
  identity: {
    type: 'SystemAssigned'
  }
  properties: {
    httpsOnly: true
    serverFarmId: appServicePlan.id
    clientAffinityEnabled: true
    reserved: true
    siteConfig: {
        // alwaysOn: true cant be set for free tier
        linuxFxVersion: 'dotnet|3.1'
    }
  }  
}

output deploymentNameId string = deploymentNameId
output appInsightsInstrumentionKey string = appInsightsKey
output webAppName string = webApp.name

然后使用 github 操作工作流程,将我的 Web 应用程序发布并部署到 Azure。

这一切都成功完成,管道完成且没有错误,并且代码已部署到我的应用程序服务。

但是,每当我访问该网站时,它都会返回

403 forbidden nginx/1.24.0

奇怪的事情很少见,它确实有效并且实际上让我可以调用端点。

我有一个名为

website-url/test
的端点,它只返回
test
。我有过这样的例子,它实际上返回了这个。但大多数时候,当我在浏览器中访问该端点时,它会给我一个 404。

在 azure 中,应用程序服务配置为网络:

  • 公网访问 已启用,无访问限制

因为它应该是公开的,所以我不明白为什么我会收到http 403禁止错误。

我发布的项目是 ASP.NET Core 8 Web API。

编辑: 应用服务日志流的日志文件:

2024-03-08T09:52:24.661209363Z: [INFO]  Note: Any data outside '/home' is not persisted
2024-03-08T09:52:27.264971290Z: [INFO]  Starting OpenBSD Secure Shell server: sshd.
2024-03-08T09:52:27.303758390Z: [INFO]  Running oryx create-script -appPath /home/site/wwwroot -output /opt/startup/startup.sh     -bindPort 8080 -startupCommand 'php-fpm;'
2024-03-08T09:52:27.513169309Z: [INFO]  Could not find build manifest file at '/home/site/wwwroot/oryx-manifest.toml'
2024-03-08T09:52:27.513220809Z: [INFO]  Could not find operation ID in manifest. Generating an operation id...
2024-03-08T09:52:27.540329219Z: [INFO]  Build Operation ID: 7d8f3084-eed3-4166-a5ff-381919ea0532
2024-03-08T09:52:28.951354426Z: [INFO]  Writing output script to '/opt/startup/startup.sh'
2024-03-08T09:52:29.818185526Z: [INFO]  Starting nginx: nginx.
2024-03-08T09:52:30.993719813Z: [ERROR]  [08-Mar-2024 09:52:30] NOTICE: fpm is running, pid 82
2024-03-08T09:52:31.021318926Z: [ERROR]  [08-Mar-2024 09:52:31] NOTICE: ready to handle connections
azure azure-web-app-service http-status-code-403 .net-8.0 azure-bicep
1个回答
0
投票

通常,禁止错误发生在服务器收到你的请求但无法接受并发送响应并拒绝它时。这可能是由于以下原因造成的。

需要检查以下内容:

  • 我可以看到错误有

    "nginx/1.24.0"
    ,因此这个或任何
    NSG's
    可能会阻止或限制应用程序服务按预期运行。交叉验证是否有任何 NSG 规则、IP 地址或防火墙限制流量。

  • 如果您配置了链接到应用程序服务的任何专用端点,然后将启用公共网络访问,则会发生此类问题。

有关上述问题的更多相关详细信息,请参阅 Microsoft 问答

  • 检查您的

    Startup.cs
    文件是否有任何其他可能导致授权问题的配置。请参阅 @AjayKumar 关于同一问题的 SO

  • 您可以通过转到

    App service
    并检查
    Monitoring
    下的应用程序服务日志来清楚地检查所有上述信息,例如IP拦截器、网络限制,或者还可以检查
    Log Stream
    以检索有关错误的更多信息。此外,您还可以在链接到您的应用程序服务的应用程序见解下进行检查。

我尝试了与您相同的代码,并且能够成功执行部署,如下所示。

@description('Specifies the location for resources.')
param location string = resourceGroup().location

var envResourceNamePrefix = 'staging-testwebsite'

@description('Deployment name id.')
param deploymentNameId string = '0000000000'


resource appInsights 'Microsoft.Insights/components@2020-02-02' = {
  name: '${envResourceNamePrefix}-app-insights'
  location: location
  kind: 'web'
  properties: {
    Application_Type: 'web'
    publicNetworkAccessForIngestion: 'Enabled'
    publicNetworkAccessForQuery: 'Enabled'
  }
}

var appInsightsKey = appInsights.properties.InstrumentationKey

resource appServicePlan 'Microsoft.Web/serverfarms@2023-01-01' = {
  name: '${envResourceNamePrefix}-app-sp'
  location: location
  kind: 'linux'
  sku: {
    name: 'B1'
  }
  properties: {
    reserved: true
  }
}

resource webApp 'Microsoft.Web/sites@2023-01-01' = {
  name: '${envResourceNamePrefix}-app'
  location: location
  kind: 'web'
  identity: {
    type: 'SystemAssigned'
  }
  properties: {
    httpsOnly: true
    serverFarmId: appServicePlan.id
    clientAffinityEnabled: true
    reserved: true
    siteConfig: {
        // alwaysOn: true cant be set for free tier
        linuxFxVersion: 'dotnet|3.1'
    }
  }  
}

enter image description here

enter image description here

enter image description here

我还检查了我的应用程序 URL,它按预期工作。

enter image description here

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