使用 CLI 将应用服务与应用程序网关集成Integrate App Service with Application Gateway using CLI

此示例脚本将创建一个 Azure 应用服务 Web 应用、Azure 虚拟网络和应用程序网关。This sample script creates an Azure App Service web app, an Azure Virtual Network and an Application Gateway. 然后,它将 Web 应用的流量限制为只能源自应用程序网关子网。It then restricts the traffic for the web app to only originate from the Application Gateway subnet.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

如果选择在本地安装并使用 CLI,需要 Azure CLI 2.0.74 版或更高版本。If you choose to install and use the CLI locally, you need Azure CLI version 2.0.74 or later. 要查找版本,请运行 az --versionTo find the version, run az --version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

示例脚本Sample script

#/bin/bash

# This script requires minimum Azure CLI version 2.0.74

# Variables
resourceGroupName="yourResourceGroup"
appName="webappwithgateway$RANDOM"
location="chinaeast"

# Create a Resource Group 
az group create --name $resourceGroupName --location $location

# Create network resources
az network vnet create \
    --resource-group $resourceGroupName \
    --name myVNet \
    --location $location \
    --address-prefix 10.0.0.0/16 \
    --subnet-name myAGSubnet \
    --subnet-prefix 10.0.1.0/24

az network public-ip create \
    --resource-group $resourceGroupName --location $location \
    --name myAGPublicIPAddress --dns-name $appName --sku Standard

# Create an App Service Plan
az appservice plan create --resource-group $resourceGroupName \
    --name myAppServicePlan --location $location --sku S1

# Create a Web App
az webapp create --resource-group $resourceGroupName \
    --name $appName --plan myAppServicePlan

appFqdn=$(az webapp show --name $appName --resource-group $resourceGroupName --query defaultHostName -o tsv)

# Create an Application Gateway
az network application-gateway create \
    --resource-group $resourceGroupName \
    --name myAppGateway \
    --location $location \
    --vnet-name myVNet \
    --subnet myAGsubnet \
    --min-capacity 2 \
    --sku Standard_v2 \
    --http-settings-cookie-based-affinity Disabled \
    --frontend-port 80 \
    --http-settings-port 80 \
    --http-settings-protocol Http \
    --public-ip-address myAGPublicIPAddress \
    --servers $appFqdn

az network application-gateway http-settings update \
    --resource-group $resourceGroupName --gateway-name myAppGateway \
    --name appGatewayBackendHttpSettings \
    --host-name-from-backend-pool

# Apply Access Restriction to Web App
az webapp config access-restriction add \
    --resource-group $resourceGroupName --name $appName \
    --priority 200 --rule-name gateway-access \
    --subnet myAGSubnet --vnet-name myVNet

# Get the App Gateway Fqdn
az network public-ip show \
    --resource-group $resourceGroupName \
    --name myAGPublicIPAddress \
    --query {AppGatewayFqdn:dnsSettings.fqdn} \
    --output table

清理部署Clean up deployment

运行示例脚本后,可以使用以下命令删除资源组以及与其关联的所有资源。After the sample script has been run, the following command can be used to remove the resource group and all resources associated with it.

az group delete --name myResourceGroup

脚本说明Script explanation

此脚本使用以下命令创建资源组、应用服务应用、Cosmos DB 和所有相关资源。This script uses the following commands to create a resource group, App Service app, Cosmos DB, and all related resources. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

CommandCommand 说明Notes
az group create 创建用于存储所有资源的资源组。Creates a resource group in which all resources are stored.
az network vnet create 创建虚拟网络。Creates a virtual network.
az network public-ip create 创建公共 IP 地址。Creates a public IP address.
az network public-ip show 显示公共 IP 地址的详细信息。Show details of a public IP address.
az appservice plan create 创建应用服务计划。Creates an App Service plan.
az webapp create 创建应用服务 Web 应用。Creates an App Service web app.
az webapp show 显示应用服务 Web 应用的详细信息。Show details of an App Service web app.
az webapp config access-restriction add 将访问限制添加到应用服务 Web 应用。Adds an access restriction to the App Service web app.
az network application-gateway create 创建应用程序网关。Creates an Application Gateway.
az network application-gateway http-settings update 更新应用程序网关 HTTP 设置。Updates Application Gateway HTTP settings.

后续步骤Next steps

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档For more information on the Azure CLI, see Azure CLI documentation.

可以在 Azure 应用服务文档中找到其他应用服务 CLI 脚本示例。Additional App Service CLI script samples can be found in the Azure App Service documentation.