教程:使用 Azure 门户创建和配置托管多个网站的应用程序网关Tutorial: Create and configure an application gateway to host multiple web sites using the Azure portal

创建应用程序网关时,可以使用 Azure 门户配置多个网站的托管You can use the Azure portal to configure the hosting of multiple web sites when you create an application gateway. 本教程使用虚拟机定义后端地址池。In this tutorial, you define backend address pools using virtual machines. 然后,基于所拥有的域配置侦听器和规则,以确保 Web 流量可到达池中的相应服务器。You then configure listeners and rules based on domains that you own to make sure web traffic arrives at the appropriate servers in the pools. 本教程假定你拥有多个域,并使用示例 www.contoso.comwww.fabrikam.comThis tutorial assumes that you own multiple domains and uses examples of www.contoso.com and www.fabrikam.com.

本教程介绍如何执行下列操作: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 backend listeners
  • 创建路由规则Create routing rules
  • 在域中创建 CNAME 记录Create a CNAME record in your domain

多站点路由示例

如果没有 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

  1. 选择 Azure 门户左侧菜单上的“创建资源” 。Select Create a resource on the left menu of the Azure portal. 此时会显示“新建”窗口。 The New window appears.

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

“基本信息”选项卡Basics tab

  1. 在“基本信息”选项卡上,输入这些值作为以下应用程序网关设置 :On the Basics tab, enter these values for the following application gateway settings:

    • 资源组:选择 myResourceGroupAG 作为资源组。Resource group: Select myResourceGroupAG for the resource group. 如果该资源组不存在,请选择“新建”,创建一个新的 。If it doesn't exist, select Create new to create it.

    • 应用程序网关名称:输入 myAppGateway 作为应用程序网关的名称。Application gateway name: Enter myAppGateway for the name of the application gateway.

      新建应用程序网关:基础知识

  2. Azure 需要一个虚拟网络才能在创建的资源之间通信。For Azure to communicate between the resources that you create, it needs a virtual network. 可以创建新的虚拟网络,或者使用现有的虚拟网络。You can either create a new virtual network or use an existing one. 在此示例中,将在创建应用程序网关的同时创建新的虚拟网络。In this example, you'll create a new virtual network at the same time that you create the application gateway. 在独立的子网中创建应用程序网关实例。Application Gateway instances are created in separate subnets. 在本示例中创建两个子网:一个用于应用程序网关,另一个用于后端服务器。You create two subnets in this example: one for the application gateway, and another for the backend servers.

    在“配置虚拟网络”下,选择“新建”以创建新的虚拟网络 。Under Configure virtual network, select Create new to create a new virtual network . 在打开的“创建虚拟网络”窗口中,输入以下值以创建虚拟网络和两个子网 :In the Create virtual network window that opens, enter the following values to create the virtual network and two subnets:

    • 名称:输入 myVNet 作为虚拟网络的名称。Name: Enter myVNet for the name of the virtual network.

    • 子网名称(应用程序网关子网):子网网关将显示名为“默认值”的子网 。Subnet name (Application Gateway subnet): The Subnets grid will show a subnet named Default. 将此子网的名称更改为 myAGSubnet 。Change the name of this subnet to myAGSubnet.
      应用程序网关子网只能包含应用程序网关。The application gateway subnet can contain only application gateways. 不允许其他资源。No other resources are allowed.

    • 子网名称(后端服务器子网):在子网网关的第二行中,在“子网名称”列输入“myBackendSubnet” 。Subnet name (backend server subnet): In the second row of the Subnets grid, enter myBackendSubnet in the Subnet name column.

    • 地址范围(后端服务器子网):在子网网格的第二行中,输入不会与 myAGSubnet 的地址范围重叠的地址范围 。Address range (backend server subnet): In the second row of the Subnets Grid, enter an address range that doesn't overlap with the address range of myAGSubnet. 例如,如果 myAGSubnet 的地址范围为 10.0.0.0/24,则为 myBackendSubnet 的地址范围输入 10.0.1.0/24 。For example, if the address range of myAGSubnet is 10.0.0.0/24, enter 10.0.1.0/24 for the address range of myBackendSubnet.

    选择“确定”以关闭“创建虚拟网络”窗口,并保存虚拟网络设置 。Select OK to close the Create virtual network window and save the virtual network settings.

    新建应用程序网关:虚拟网络

  3. 在“基本信息” 选项卡上,接受其他设置的默认值,然后选择“下一步: 前端”。On the Basics tab, accept the default values for the other settings and then select Next: Frontends.

“前端”选项卡Frontends tab

  1. 在“前端”选项卡上,验证“IP 地址类型”是否设置为“公共” 。On the Frontends tab, verify Frontend IP address type is set to Public.
    可根据用例将前端 IP 配置为公共或专用 IP。You can configure the Frontend IP to be Public or Private as per your use case. 本示例将选择公共前端 IP。In this example, you'll choose a Public Frontend IP.

    Note

    对于应用程序网关 v2 SKU,只能选择公共前端 IP 配置。For the Application Gateway v2 SKU, you can only choose Public frontend IP configuration. 目前尚未为此 v2 SKU 启用专用前端 IP 配置。Private frontend IP configuration is currently not enabled for this v2 SKU.

  2. 为“公共 IP 地址”选择“新建”,输入“myAGPublicIPAddress”作为公共 IP 地址名称,然后选择“确定” 。Choose Create new for the Public IP address and enter myAGPublicIPAddress for the public IP address name, and then select OK.

    新建应用程序网关:前端

  3. 在完成时选择“下一步:后端Select Next: Backends.

“后端”选项卡Backends tab

后端池用于将请求路由到为请求提供服务的后端服务器。The backend pool is used to route requests to the backend servers that serve the request. 后端池可以包括 NIC、虚拟机规模集、公共 IP、内部 IP、完全限定的域名 (FQDN) 和多租户后端(例如 Azure 应用服务)。Backend pools can be NICs, virtual machine scale sets, public IPs, internal IPs, fully qualified domain names (FQDN), and multi-tenant back-ends like Azure App Service. 在此示例中,将使用应用程序网关创建空的后端池,然后将后端目标添加到后端池。In this example, you'll create an empty backend pool with your application gateway and then add backend targets to the backend pool.

  1. 在“后端”选项卡上,选择“+添加后端池” 。On the Backends tab, select +Add a backend pool.

  2. 在打开的“添加后端池”窗口中,输入以下值以创建空的后端池 :In the Add a backend pool window that opens, enter the following values to create an empty backend pool:

    • 名称:输入“contosoPool” 作为后端池的名称。Name: Enter contosoPool for the name of the backend pool.
    • 添加不包含目标的后端池:选择“是”以创建不包含目标的后端池 。Add backend pool without targets: Select Yes to create a backend pool with no targets. 你将在创建应用程序网关之后添加后端目标。You'll add backend targets after creating the application gateway.
  3. 在“添加后端池”窗口中,选择“添加”以保存后端池配置并返回到“后端”选项卡 。In the Add a backend pool window, select Add to save the backend pool configuration and return to the Backends tab.

  4. 现在添加另一个名为“fabrikamPool” 的后端池。Now add another backend pool called fabrikamPool.

    新建应用程序网关:后端

  5. 在“后端” 选项卡上,选择“下一步: 配置”。On the Backends tab, select Next: Configuration.

配置选项卡Configuration tab

在“配置”选项卡上,将连接使用传递规则创建的前端和后端池 。On the Configuration tab, you'll connect the frontend and backend pools you created using a routing rule.

  1. 选择“传递规则”列中的“添加规则” 。Select Add a rule in the Routing rules column.

  2. 在打开的“添加传递规则”窗口中,输入“contosoRule”作为规则名称 。In the Add a routing rule window that opens, enter contosoRule for the Rule name.

  3. 传递规则需要侦听器。A routing rule requires a listener. 在“添加传递规则”窗口中的“侦听器”选项卡上,输入侦听器的以下值 :On the Listener tab within the Add a routing rule window, enter the following values for the listener:

    • 侦听器名称:输入“contosoListener” 作为侦听器的名称。Listener name: Enter contosoListener for the name of the listener.
    • 前端 IP:选择“公共”,以选择为前端创建的公共 IP 。Frontend IP: Select Public to choose the public IP you created for the frontend.

    在“其他设置” 下:Under Additional settings:

    接受“侦听器”选项卡上其他设置的默认值,然后选择“后端目标”选项卡以配置剩余的传递规则 。Accept the default values for the other settings on the Listener tab, then select the Backend targets tab to configure the rest of the routing rule.

    新建应用程序网关:侦听器

  4. 在“后端目标”选项卡上,选择“contosoPool”为“后端目标” 。On the Backend targets tab, select contosoPool for the Backend target.

  5. 对于“HTTP 设置”,选择“新建”以创建新的 HTTP 设置 。For the HTTP setting, select Create new to create a new HTTP setting. HTTP 设置将决定传递规则的行为。The HTTP setting will determine the behavior of the routing rule. 在打开的“添加 HTTP 设置”窗口中,”输入“contosoHTTPSetting”作为“HTTP 设置名称” 。In the Add an HTTP setting window that opens, enter contosoHTTPSetting for the HTTP setting name. 接受“添加 HTTP 设置”窗口中其他设置的默认值,然后选择“添加”以返回到“添加传递规则”窗口 。Accept the default values for the other settings in the Add an HTTP setting window, then select Add to return to the Add a routing rule window.

  6. 在“添加传递规则”窗口上,选择“添加”以保存传递规则并返回到“配置”选项卡 。On the Add a routing rule window, select Add to save the routing rule and return to the Configuration tab.

  7. 选择“添加规则” ,并为 Fabrikam 添加类似的规则、侦听器、后端目标和 HTTP 设置。Select Add a rule and add a similar rule, listener, backend target, and HTTP setting for Fabrikam.

    新建应用程序网关:传递规则

  8. 在完成时选择“下一步: 标记”,然后选择“下一步: 查看 + 创建”。Select Next: Tags and then Next: Review + create.

“查看 + 创建”选项卡Review + create tab

复查“查看 + 创建”选项卡上的设置,然后选择“创建”以创建虚拟网络、公共 IP 地址和应用程序网关 。Review the settings on the Review + create tab, and then select Create to create the virtual network, the public IP address, and the application gateway. Azure 可能需要数分钟时间来创建应用程序网关。It may take several minutes for Azure to create the application gateway.

请等待部署成功完成,然后再前进到下一部分。Wait until the deployment finishes successfully before moving on to the next section.

添加后端目标Add backend targets

本示例将使用虚拟机作为目标后端。In this example, you'll use virtual machines as the target backend. 可以使用现有的虚拟机,或创建新的虚拟机。You can either use existing virtual machines or create new ones. 将创建两个虚拟机,供 Azure 用作应用程序网关的后端服务器。You'll create two virtual machines that Azure uses as backend servers for the application gateway.

若要添加后端目标,需要:To add backend targets, you'll:

  1. 创建两个新的 VM(contosoVM 和 fabrikamVM),用作后端服务器 。Create two new VMs, contosoVM and fabrikamVM, to be used as backend servers.
  2. 在虚拟机上安装 IIS,以验证是否成功创建了应用程序网关。Install IIS on the virtual machines to verify that the application gateway was created successfully.
  3. 将后端服务器添加到后端池。Add the backend servers to the backend pools.

创建虚拟机Create a virtual machine

  1. 在 Azure 门户中,选择“创建资源”。 On the Azure portal, select Create a resource. 此时会显示“新建”窗口。 The New window appears.

  2. 选择“计算”,然后在“常用”列表中选择“Windows Server 2016 Datacenter” 。Select Compute and then select Windows Server 2016 Datacenter in the Popular list. 此时会显示“创建虚拟机”页。 The Create a virtual machine page appears.
    应用程序网关可将流量路由到其后端池中使用的任何类型的虚拟机。Application Gateway can route traffic to any type of virtual machine used in its backend pool. 本示例使用 Windows Server 2016 Datacenter。In this example, you use a Windows Server 2016 Datacenter.

  3. 对于以下虚拟机设置,请在“基本信息”选项卡中输入相应值: Enter these values in the Basics tab for the following virtual machine settings:

    • 资源组:选择 myResourceGroupAG 作为资源组名称。Resource group: Select myResourceGroupAG for the resource group name.
    • 虚拟机名称:输入“contosoVM” 作为虚拟机的名称。Virtual machine name: Enter contosoVM for the name of the virtual machine.
    • 用户名:输入 azureuser 作为管理员用户名。Username: Enter azureuser for the administrator user name.
    • 密码:输入 Azure123456!Password: Enter Azure123456! 作为管理员密码。for the administrator password.
  4. 接受其他默认值,然后选择“下一步:磁盘”Accept the other defaults and then select Next: Disks.

  5. 接受“磁盘”选项卡的默认值,然后选择“下一步:网络”Accept the Disks tab defaults and then select Next: Networking.

  6. 在“网络” 选项卡上,验证是否已选择 myVNet 作为虚拟网络,以及是否已将“子网” 设置为 myBackendSubnetOn the Networking tab, verify that myVNet is selected for the Virtual network and the Subnet is set to myBackendSubnet. 接受其他默认值,然后选择“下一步:管理”Accept the other defaults and then select Next: Management.
    应用程序网关可与其所在的虚拟网络外部的实例进行通信,但需要确保已建立 IP 连接。Application Gateway can communicate with instances outside of the virtual network that it is in, but you need to ensure there's IP connectivity.

  7. 在“管理” 选项卡上,将“启动诊断” 设置为“关闭”。 On the Management tab, set Boot diagnostics to Off. 接受其他默认值,然后选择“复查 + 创建”。 Accept the other defaults and then select Review + create.

  8. 在“复查 + 创建”选项卡上复查设置,更正任何验证错误,然后选择“创建”。 On the Review + create tab, review the settings, correct any validation errors, and then select Create.

  9. 等待虚拟机创建完成,然后再继续操作。Wait for the virtual machine creation to complete before continuing.

安装 IIS 用于测试Install IIS for testing

  1. 在 PowerShell 中使用以下命令登录 Azure 门户:Login 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:

    Set-AzVMExtension `
      -ResourceGroupName myResourceGroupAG `
      -ExtensionName IIS `
      -VMName contosoVM `
      -Publisher Microsoft.Compute `
      -ExtensionType CustomScriptExtension `
      -TypeHandlerVersion 1.4 `
      -SettingString '{"commandToExecute":"powershell Add-WindowsFeature Web-Server; powershell Add-Content -Path \"C:\\inetpub\\wwwroot\\Default.htm\" -Value $($env:computername)"}' `
      -Location ChinaNorth
    
  3. 使用以前完成的步骤创建第二个虚拟机并安装 IIS。Create a second virtual machine and install IIS using the steps that you previously completed. 使用“fabrikamVM” 作为虚拟机名称和 Set-AzVMExtension cmdlet 的 VMName 设置。Use fabrikamVM for the virtual machine name and for the VMName setting of the Set-AzVMExtension cmdlet.

将后端服务器添加到后端池Add backend servers to backend pools

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

  2. 从左侧菜单中选择“后端池”。 Select Backend pools from the left menu.

  3. 选择 contosoPoolSelect contosoPool.

  4. 在“目标” 下,从下拉列表中选择“虚拟机”。 Under Targets, select Virtual machine from the drop-down list.

  5. 在“虚拟机” 和“网络接口” 下,从下拉列表中选择“contosoVM” 虚拟机及其关联的网络接口。Under VIRTUAL MACHINE and NETWORK INTERFACES, select the contosoVM virtual machine and it's associated network interface from the drop-down lists.

    添加后端服务器

  6. 选择“保存” 。Select Save.

  7. 重复此步骤,将 fabrikamVM 和接口添加到 fabrikamPool 。Repeat to add the fabrikamVM and interface to the fabrikamPool.

等待部署完成之后再继续下一步。Wait for the deployment to complete before proceeding to the next step.

在域中创建 www A 记录Create a www A record in your domains

使用其公共 IP 地址创建应用程序网关后,可以获取 IP 地址并使用它在域中创建 A 记录。After the application gateway is created with its public IP address, you can get the IP address and use it to create an A record in your domains.

  1. 单击“所有资源” ,然后单击“myAGPublicIPAddress” 。Click All resources, and then click myAGPublicIPAddress.

    记下应用程序网关的 DNS 地址

  2. 复制 IP 地址并将其用作域中新 www A 记录的值。Copy the IP address and use it as the value for a new www A record in your domains.

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

  1. 在浏览器的地址栏中输入域名。Enter your domain name into the address bar of your browser. 例如, http://www.contoso.comSuch as, http://www.contoso.com.

    在应用程序网关中测试 contoso 站点

  2. 将地址更改为其他域,应看到类似下例所示的内容:Change the address to your other domain and you should see something like the following example:

    在应用程序网关中测试 fabrikam 站点

清理资源Clean up resources

如果不再需要通过应用程序网关创建的资源,请删除资源组。When you no longer need the resources that you created with the application gateway, remove the resource group. 删除资源组时,也会删除应用程序网关和及其所有的相关资源。When you remove 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