使用 Azure CLI 创建支持外部重定向的应用程序网关Create an application gateway with external redirection using the Azure CLI

你可以使用 Azure CLI 配置 web 流量重定向创建时应用程序网关You can use the Azure CLI to configure web traffic redirection when you create an application gateway. 在本教程中,可以配置侦听器和重定向到外部站点的应用程序网关在到达的 web 流量的规则。In this tutorial, you configure a listener and rule that redirects web traffic that arrives at the application gateway to an external site.

在本文中,学习如何:In this article, you learn how to:

  • 设置网络Set up the network
  • 创建侦听器和重定向规则Create a listener and redirection rule
  • 创建应用程序网关Create an application gateway

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

如果选择在本地安装并使用 CLI,此快速入门教程要求运行 Azure CLI 2.0.4 版或更高版本。If you choose to install and use the CLI locally, this quickstart requires that you are running the Azure CLI version 2.0.4 or later. 若要查找版本,请运行 az --versionTo find the version, run az --version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

创建资源组Create a resource group

资源组是在其中部署和管理 Azure 资源的逻辑容器。A resource group is a logical container into which Azure resources are deployed and managed. 使用 az group create 创建资源组。Create a resource group using az group create.

以下示例在“chinanorth2” 位置创建名为“myResourceGroupAG” 的资源组。The following example creates a resource group named myResourceGroupAG in the chinanorth2 location.

az group create --name myResourceGroupAG --location chinanorth2

创建网络资源Create network resources

使用 az network vnet create 创建名为 myVNet 的虚拟网络和名为 myAGSubnet 的子网。Create the virtual network named myVNet and the subnet named myAGSubnet using az network vnet create. 使用 az network public-ip create 创建名为 myAGPublicIPAddress 的公共 IP 地址。Create the public IP address named myAGPublicIPAddress using az network public-ip create. 这些资源用于提供与应用程序网关及其关联资源的网络连接。These resources are used to provide network connectivity to the application gateway and its associated resources.

az network vnet create \
  --name myVNet \
  --resource-group myResourceGroupAG \
  --location chinanorth2 \
  --address-prefix 10.0.0.0/16 \
  --subnet-name myAGSubnet \
  --subnet-prefix 10.0.1.0/24
az network public-ip create \
  --resource-group myResourceGroupAG \
  --name myAGPublicIPAddress

创建应用程序网关Create an application gateway

可以使用 az network application-gateway create 创建名为 myAppGateway 的应用程序网关。You can use az network application-gateway create to create the application gateway named myAppGateway. 使用 Azure CLI 创建应用程序网关时,请指定配置信息,例如容量、sku 和 HTTP 设置。When you create an application gateway using the Azure CLI, you specify configuration information, such as capacity, sku, and HTTP settings. 将应用程序网关分配给之前创建的 myAGSubnetmyPublicIPAddressThe application gateway is assigned to myAGSubnet and myPublicIPAddress that you previously created.

az network application-gateway create \
  --name myAppGateway \
  --location chinanorth2 \
  --resource-group myResourceGroupAG \
  --vnet-name myVNet \
  --subnet myAGsubnet \
  --capacity 2 \
  --sku Standard_Medium \
  --http-settings-cookie-based-affinity Disabled \
  --frontend-port 8080 \
  --http-settings-port 80 \
  --http-settings-protocol Http \
  --public-ip-address myAGPublicIPAddress

创建应用程序网关可能需要几分钟时间。It may take several minutes for the application gateway to be created. 创建应用程序网关后,可以看到它的这些新功能:After the application gateway is created, you can see these new features of it:

  • appGatewayBackendPool - 应用程序网关必须至少具有一个后端地址池。appGatewayBackendPool - An application gateway must have at least one backend address pool.
  • appGatewayBackendHttpSettings - 指定将端口 80 和 HTTP 协议用于通信。appGatewayBackendHttpSettings - Specifies that port 80 and an HTTP protocol is used for communication.
  • appGatewayHttpListener - 与 appGatewayBackendPool 关联的默认侦听器。appGatewayHttpListener - The default listener associated with appGatewayBackendPool.
  • appGatewayFrontendIP - 将 myAGPublicIPAddress 分配给 appGatewayHttpListenerappGatewayFrontendIP - Assigns myAGPublicIPAddress to appGatewayHttpListener.
  • rule1 - 与 appGatewayHttpListener 关联的默认路由规则。rule1 - The default routing rule that is associated with appGatewayHttpListener.

添加重定向配置Add the redirection configuration

使用 az network application-gateway redirect-config create 在应用程序网关中添加从 www.consoto.org 将流量发送到 www.contoso.com 的侦听器的重定向配置。Add the redirection configuration that sends traffic from www.consoto.org to the listener for www.contoso.com to the application gateway using az network application-gateway redirect-config create.

az network application-gateway redirect-config create \
  --name myredirect \
  --gateway-name myAppGateway \
  --resource-group myResourceGroupAG \
  --type Temporary \
  --target-url "https://bing.com"

添加侦听器和路由规则Add a listener and routing rule

应用程序网关需要侦听器才能适当地将流量路由到后端池。A listener is required to enable the application gateway to appropriately route traffic. 使用创建侦听器az 网络应用程序网关 http 侦听器创建使用创建的前端端口与az 网络应用程序网关前端端口创建Create the listener using az network application-gateway http-listener create with the frontend port created with az network application-gateway frontend-port create. 侦听器需要使用规则来了解哪个后端池使用传入流量。A rule is required for the listener to know where to send incoming traffic. 使用 az network application-gateway rule create 创建名为 redirectRule 的基本规则。Create a basic rule named redirectRule using az network application-gateway rule create.

az network application-gateway frontend-port create \
  --port 80 \
  --gateway-name myAppGateway \
  --resource-group myResourceGroupAG \
  --name redirectPort
az network application-gateway http-listener create \
  --name redirectListener \
  --frontend-ip appGatewayFrontendIP \
  --frontend-port redirectPort \
  --resource-group myResourceGroupAG \
  --gateway-name myAppGateway
az network application-gateway rule create \
  --gateway-name myAppGateway \
  --name redirectRule \
  --resource-group myResourceGroupAG \
  --http-listener redirectListener \
  --rule-type Basic \
  --redirect-config myredirect

测试应用程序网关Test the application gateway

若要获取应用程序网关的公共 IP 地址,可以使用 az network public-ip showTo get the public IP address of the application gateway, you can use az network public-ip show. 复制该公共 IP 地址,并将其粘贴到浏览器的地址栏。Copy the public IP address, and then paste it into the address bar of your browser.

应该会看到 bing.com 出现在浏览器中。You should see bing.com appear in your browser.

后续步骤Next steps