为 Azure Web App 配置应用程序网关时出现 502 错误

Application Gateway 502 error when configuring it for Azure Web App

场景:

配置 Azure 应用程序网关以将流量路由到不同的 Azure Web 应用程序以利用 Web 应用程序防火墙 (WAF)。

除了浏览器中的最终 URL 是 {mysite}.azurewebsites.net 之外,初始配置工作正常。我希望这是自定义域名 {subdomain}.{mysite}.com。这是通过将后端池绑定到应用程序服务来配置的。将其更改为 FQDN 不起作用。屏幕截图如下。

CNAME 已经指向网关。

想要的结果

当有人点击 {subdomain}.{mysite}.com(已配置为指向网关)时,我应该得到 {subdomain}.{mysite}.com 而不是 {mysite}。azurewebsites.net

当前设置:

网关配置了一个多站点侦听器,其规则指向后端池并绑定到 http 设置。

监听器正在监听 {subdomain}.{mysite}.com

后端池配置

探测器配置

HTTP 设置配置

以下链接提供了部分 answers/configurations 但其中 none 对我有用。

https://docs.microsoft.com/en-us/azure/application-gateway/troubleshoot-app-service-redirection-app-service-url

https://docs.microsoft.com/en-us/azure/application-gateway/create-web-app

更新

以下是使用 azure 门户 UI.

使用 Azure 应用程序网关和 WAF(Web 应用程序防火墙)后面的自定义域配置 azure 应用程序服务的步骤列表。
  1. 配置目标指向您的应用服务的后端池。
  2. 使用以下设置配置 Health Probe

    • 协议:HTTP 或 HTTPS
    • 主机:{appservice}。azurewebsites.net
    • 路径:/
  3. 配置 Http 设置

    • 未选中“使用应用服务”。
    • 使用自定义探测器选中
    • Pick host name from backend address 未选中(您可能需要选中此项以配置规则,然后取消选中)。
  4. 配置监听器

    • 主机名:{subdomain}.{domain}.com(您的域)
  5. 配置基本规则

    • 将其绑定到适当的 http 设置、后端池和侦听器
    • 警告:如果没有选中从后端选择主机名的 http 设置,您可能需要更改它,附加它,然后再次删除该标志。

您可能会收到 502 错误,因为您在后端池的目标中更改为 FQDN subdomain.mysite.com。但是,在这种情况下,您没有更改 Web 应用程序服务中的自定义域,您只能通过默认域 mysite.azurewebsites.net 访问该网站。建议改回targets中的App服务

Initial configuration worked fine except that the final URL in the browser is the {mysite}.azurewebsites.net.

请检查您是否在 Custom domains 网络应用服务中设置了 HTTPS Only 设置 ON。这会将所有 Http 重定向到 Https。对我来说,通常,它会导致在浏览器中最后的 URL 是 mysite.azurewebsites.net.

更新

如果您没有开启HTTPS Only,出现此问题的主要原因可能如下:

  • You have redirection configured on your App Service. Redirection can be as simple as adding a trailing slash to the request.
  • You have Azure AD authentication which causes the redirection.
  • You have enabled “Pick Host Name from Backend Address” switch in the HTTP settings of Application Gateway.
  • You don’t have your custom domain registered with your App Service.

此问题可以通过在应用程序端不进行重定向来解决,但是,如果这不可能,您可以按照此 solution 来解决它。

  1. 将域 subdomain.mysite.com 注册到应用服务的自定义域列表。有关详细信息,请参阅 Map an existing custom DNS name to Azure App Service.
  2. 一旦完成,您的应用服务就可以接受主机名 subdomain.mysite.com。现在更改 DNS 中的 CNAME 条目以将其指向应用程序网关的 FQDN。例如,appgw.eastus.cloudapp.azure.com.
  3. 取消选中探测设置中的复选框 Pick Hostname from Backend HTTP Settings。在探测的主机名字段中,输入应用服务的 FQDN mysite.azurewebsites.net,因为从应用程序网关发送的探测请求将在主机 header.
  4. 中携带它
  5. 取消选中应用程序网关的 HTTP 设置中的复选框 Pick Hostname from Backend Address
  6. 将自定义探测关联回后端 HTTP 设置并验证后端运行状况是否良好。

完成此操作后,应用程序网关现在应该将相同的主机名 subdomain.mysite.com 转发到应用服务,并且重定向将在相同的主机名上发生。