教程:通过 Azure 门户使用基于路径的路由规则创建应用程序网关Tutorial: Create an application gateway with path-based routing rules using the Azure portal

创建应用程序网关时,可以使用 Azure 门户配置基于 URL 路径的路由规则You can use the Azure portal to configure URL path-based routing rules when you create an application gateway. 本教程中使用虚拟机创建后端池。In this tutorial, you create backend pools using virtual machines. 然后创建路由规则,以确保 Web 流量到达池中的相应服务器。You then create routing rules that make sure web traffic arrives at the appropriate servers in the pools.

本教程介绍如何执行下列操作:In this tutorial, you learn how to:

  • 创建应用程序网关Create an application gateway
  • 为后端服务器创建虚拟机Create virtual machines for backend servers
  • 使用后端服务器创建后端池Create backend pools with the backend servers
  • 创建后端侦听器Create a backend listener
  • 创建基于路径的路由规则Create a path-based routing rule

URL 路由示例

如果你愿意,可以使用 Azure CLIAzure PowerShell 完成本教程中的步骤。If you prefer, you can complete this tutorial using Azure CLI or Azure PowerShell.

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

登录 AzureSign in to Azure

https://portal.azure.cn 中登录 Azure 门户。Sign in to the Azure portal at https://portal.azure.cn.

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

若要在你创建的资源之间进行通信,需要设置虚拟网络。A virtual network is needed for communication between the resources you create. 在本示例中创建了两个子网:一个用于应用程序网关,另一个用于后端服务器。Two subnets are created in this example: one for the application gateway, and the other for the backend servers. 可以在创建应用程序网关的同时创建虚拟网络。You can create a virtual network at the same time you create the application gateway.

  1. 选择 Azure 门户左上角的“新建”。Select New found on the upper left-hand corner of the Azure portal.

  2. 选择“网络”,然后在“特色”列表中选择“应用程序网关”。Select Networking and then select Application Gateway in the Featured list.

  3. 输入应用程序网关的以下值:Enter these values for the application gateway:

    • myAppGateway - 应用程序网关的名称。myAppGateway - for the name of the application gateway.

    • myResourceGroupAG - 新资源组。myResourceGroupAG - for the new resource group.

      新建应用程序网关

  4. 接受其他设置的默认值,然后选择“确定”。Accept the default values for the other settings and then select OK.

  5. 依次选择“选择虚拟网络”、“新建”,然后为虚拟网络输入以下值:Select Choose a virtual network, select Create new, and then enter these values for the virtual network:

    • myVNet - 虚拟网络的名称。myVNet - for the name of the virtual network.

    • 10.0.0.0/16 - 虚拟网络地址空间。10.0.0.0/16 - for the virtual network address space.

    • myAGSubnet - 子网名称。myAGSubnet - for the subnet name.

    • 10.0.0.0/24 - 子网地址空间。10.0.0.0/24 - for the subnet address space.

      创建虚拟网络

  6. 选择“确定”以创建虚拟网络和子网。Select OK to create the virtual network and subnet.

  7. 依次选择“选择公共 IP 地址”、“新建”,然后输入公共 IP 地址的名称。Select Choose a public IP address, select Create new, and then enter the name of the public IP address. 在本示例中,公共 IP 地址名为 myAGPublicIPAddressIn this example, the public IP address is named myAGPublicIPAddress. 接受其他设置的默认值,然后选择“确定”。Accept the default values for the other settings and then select OK.

  8. 接受侦听器配置的默认值,让 Web 应用程序防火墙保留禁用状态,然后选择“确定”。Accept the default values for the Listener configuration, leave the Web application firewall disabled, and then select OK.

  9. 检查摘要页上的设置,然后选择“确定”创建网络资源和应用程序网关。Review the settings on the summary page, and then select OK to create the network resources and the application gateway. 创建应用程序网关可能需要几分钟时间,请等到部署成功完成,然后转到下一部分。It may take several minutes for the application gateway to be created, wait until the deployment finishes successfully before moving on to the next section.

添加子网Add a subnet

  1. 选择左侧菜单中的“所有资源”,然后从资源列表中选择“myVNet”。Select All resources in the left-hand menu, and then select myVNet from the resources list.

  2. 依次选择“子网”、“子网”。Select Subnets, and then select Subnet.

    创建子网

  3. 输入 myBackendSubnet 作为子网的名称,然后选择“确定”。Enter myBackendSubnet for the name of the subnet and then select OK.

创建虚拟机Create virtual machines

本示例将创建三个虚拟机,用作应用程序网关的后端服务器。In this example, you create three virtual machines to be used as backend servers for the application gateway. 还可以在虚拟机上安装 IIS,以验证是否已成功创建应用程序网关。You also install IIS on the virtual machines to verify the application gateway was successfully created.

  1. 选择“新建”。Select New.

  2. 选择“计算”,然后在“特色”列表中选择“Windows Server 2016 Datacenter”。Select Compute and then select Windows Server 2016 Datacenter in the Featured list.

  3. 输入虚拟机的以下值:Enter these values for the virtual machine:

    • myVM1 - 虚拟机的名称。myVM1 - for the name of the virtual machine.
    • azureuser - 管理员用户名。azureuser - for the administrator user name.
    • Azure123456! -Azure123456! 密码。for the password.
    • 选择“使用现有资源组”,然后选择“myResourceGroupAG”。Select Use existing, and then select myResourceGroupAG.
  4. 选择“确定” 。Select OK.

  5. 选择“DS1_V2”作为虚拟机的大小,然后选择“选择”。Select DS1_V2 for the size of the virtual machine, and select Select.

  6. 请确保选择 myVNet 作为虚拟网络,子网是 myBackendSubnetMake sure that myVNet is selected for the virtual network and the subnet is myBackendSubnet.

  7. 选择“禁用”以禁用启动诊断。Select Disabled to disable boot diagnostics.

  8. 选择“确定”,检查摘要页上的设置,然后选择“创建”。Select OK, review the settings on the summary page, and then select Create.

安装 IISInstall IIS

Note

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

  1. 在 PowerShell 中使用以下命令登录到 Azure 门户:Log in to the Azure portal in powershell with command below:

    Connect-AzAccount -Environment AzureChinaCloud
    
  2. 运行以下命令以在虚拟机上安装 IIS:Run the following command to install IIS on the virtual machine:

    $publicSettings = @{ "fileUris" = (,"https://raw.githubusercontent.com/Azure/azure-docs-powershell-samples/master/application-gateway/iis/appgatewayurl.ps1");  "commandToExecute" = "powershell -ExecutionPolicy Unrestricted -File appgatewayurl.ps1" }
    Set-AzVMExtension `
      -ResourceGroupName myResourceGroupAG `
      -Location chinanorth `
      -ExtensionName IIS `
      -VMName myVM1 `
      -Publisher Microsoft.Compute `
      -ExtensionType CustomScriptExtension `
      -TypeHandlerVersion 1.4 `
      -Settings $publicSettings
    
  3. 使用刚刚完成的步骤创建另外两个虚拟机并安装 IIS。Create two more virtual machines and install IIS using the steps that you just finished. 在 Set-AzVMExtension 中输入 myVM2myVM3 作为名称,并输入 VMName 值。Enter the names of myVM2 and myVM3 for the names and for the values of VMName in Set-AzVMExtension.

使用虚拟机创建后端池Create backend pools with the virtual machines

  1. 选择“所有资源”,然后选择“myAppGateway”。Select All resources and then select myAppGateway.

  2. 选择“后端池”。Select Backend pools. 默认池已随应用程序网关自动创建。A default pool was automatically created with the application gateway. 选择“appGatewayBackendPool”。Select appGatewayBackendPool.

  3. 选择“添加目标”,将 myVM1 添加到 appGatewayBackendPool。Select Add target to add myVM1 to appGatewayBackendPool.

    添加后端服务器

  4. 选择“其他安全性验证” 。Select Save.

  5. 依次选择“后端池”、“添加”。Select Backend pools and then select Add.

  6. 输入名称 imagesBackendPool,并使用“添加目标”添加 myVM2Enter a name of imagesBackendPool and add myVM2 using Add target.

  7. 选择“确定” 。Select OK.

  8. 再次选择“添加”以添加名为 videoBackendPool 的另一个后端池,并在其中添加 myVM3Select Add again to add another backend pool with a name of videoBackendPool and add myVM3 to it.

创建后端侦听器Create a backend listener

  1. 选择“侦听器”,然后选择“基本”。Select Listeners and the select Basic.
  2. 输入 myBackendListener 作为名称,输入 myFrontendPort 作为前端端口的名称,输入 8080 作为侦听器的端口。Enter myBackendListener for the name, myFrontendPort for the name of the frontend port, and then 8080 as the port for the listener.
  3. 选择“确定” 。Select OK.

创建基于路径的路由规则Create a path-based routing rule

  1. 选择“规则”,然后选择“基于路径”。Select Rules and then select Path-based.

  2. 输入 rule2 作为名称。Enter rule2 for the name.

  3. 输入 Images 作为第一个路径的名称。Enter Images for the name of the first path. 输入 /images/ * 作为路径。Enter /images/* for the path. 选择后端池的 imagesBackendPoolSelect imagesBackendPool for the backend pool.

  4. 输入 Video 作为第二个路径的名称。Enter Video for the name of the second path. 输入 /video/ * 作为路径。Enter /video/* for the path. 选择后端池的 videoBackendPoolSelect videoBackendPool for the backend pool.

    创建基于路径的规则

  5. 选择“确定” 。Select OK.

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

  1. 依次选择“所有资源”、“myAGPublicIPAddress”。Select All resources, and then select myAGPublicIPAddress.

    记下应用程序网关的公共 IP 地址

  2. 复制该公共 IP 地址,并将其粘贴到浏览器的地址栏。Copy the public IP address, and then paste it into the address bar of your browser. 例如 http://40.121.222.19Such as, http://40.121.222.19.

    在应用程序网关中测试基 URL

  3. 将 URL 更改为 http://<ip-address>:8080/images/test.htm(请将 <ip-address> 替换为你的 IP 地址),然后,应会看到如以下示例所示的内容:Change the URL to http://<ip-address>:8080/images/test.htm, replacing <ip-address> with your IP address, and you should see something like the following example:

    在应用程序网关中测试映像 URL

  4. 将 URL 更改为 http://<ip-address>:8080/video/test.htm(请将 <ip-address> 替换为你的 IP 地址),然后,应会看到如以下示例所示的内容:Change the URL to http://<ip-address>:8080/video/test.htm, replacing <ip-address> with your IP address, and you should see something like the following example:

    在应用程序网关中测试视频 URL

清理资源Clean up resources

如果不再需要通过应用程序网关创建的资源,请删除资源组。When you no longer need the resources that you created with the application gateway, remove the resource group. 删除资源组时,也会删除应用程序网关和及其所有的相关资源。By removing the resource group, you also remove the application gateway and all its related resources.

若要删除资源组,请执行以下操作:To remove the resource group:

  1. 在 Azure 门户的左侧菜单上选择“资源组”。On the left menu of the Azure portal, select Resource groups.
  2. 在“资源组”页的列表中搜索“myResourceGroupAG”,然后将其选中。On the Resource groups page, search for myResourceGroupAG in the list, then select it.
  3. 在“资源组”页上,选择“删除资源组”。On the Resource group page, select Delete resource group.
  4. 在“键入资源组名称”字段中输入“myResourceGroupAG”,然后选择“删除”。Enter myResourceGroupAG for TYPE THE RESOURCE GROUP NAME and then select Delete.

后续步骤Next steps