绑定 <binding>

概述

<bindings> 元素的 <binding> 元素可用于配置请求与网站通信时所需的信息。

可以在创建网站时配置绑定信息,也可以在创建网站后编辑绑定信息。 绑定信息包括客户端用于与站点通信的协议、站点的 IP 地址、端口号和主机头。

<binding> 元素包含用于配置绑定信息的两个特性:bindingInformation 和 protocol。 bindingInformation 特性包含站点的 IP 地址、端口号和主机头(可选)。 protocol 特性定义用于与站点通信的协议

可以通过选择站点并使用“操作”窗格中的“绑定...”或“高级设置...”链接,或者通过单击站点的快捷菜单上的“编辑绑定...”来使用 IIS 管理器配置站点的绑定集合

通过 IIS 8.0 或更高版本,可以使用 Windows Server 2012 上集中式证书存储中的证书为网站提供安全性。 有关详细信息,请参阅 IIS 8.0 集中式 SSL 证书支持:SSL 可伸缩性和可管理性

通过 binding 元素,可以指定用于安全套接字层 (SSL) 证书的绑定类型。

兼容性

版本 说明
IIS 10.0 <binding> 元素在 IIS 10.0 中未进行修改。
IIS 8.5 <binding> 元素在 IIS 8.5 中未进行修改。
IIS 8.0 添加了 sslFlags 特性,用于指定用于安全套接字层证书的绑定。
IIS 7.5 <binding> 元素在 IIS 7.5 中未进行修改。
IIS 7.0 IIS 7.0 中引入了 <bindings> 集合的 <binding> 元素。
IIS 6.0 <bindings> 集合替换了 IIS 6.0 IIsWebServer 元数据库对象上 ServerBindings 属性的部分。

安装

<binding> 元素包含在 IIS 7 及更高版本的默认安装中。

如何安装集中式证书存储

在 IIS 8 及更高版本中,集中式证书存储中的证书可用于为网站提供安全性。 若要在 Windows Server 2012 或更高版本的 Web 服务器上使用此存储,必须安装集中式 SSL 证书存储角色。 若要在已安装 IIS 的情况下执行此操作,请按如下所示进行:

  1. 按 Windows 键,然后单击“服务器管理器”
  2. 在“服务器管理器”中,单击“管理”,然后单击“添加角色和功能”
  3. 在“添加角色和功能”向导中,单击“开始之前”页,选择安装类型,然后单击“安装类型”页上的“下一步”,选择安装位置,并在“服务器选择”页上单击“下一步”
  4. 在“服务器角色”页上,确保选中“Web 服务器(IIS)”,然后将其展开。 展开“Web 服务器”,然后选择并展开“安全性”
  5. 选择“集中式 SSL 证书存储”,然后单击“下一步”
  6. 在“功能”页上,单击“下一步”。
  7. “确认安装选择”页中,单击“安装”
  8. 在“结果” 页面中单击“关闭”

操作方式

如何将绑定信息添加到站点

  1. 打开 Internet Information Services (IIS) 管理器:

    • 如果使用的是 Windows Server 2012 或 Windows Server 2012 R2:

      • 在任务栏上,单击“服务器管理器”,单击“工具”,然后单击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows 8 或 Windows 8.1:

      • 按住 Windows 键,按字母 X,然后单击“控制面板”。
      • 单击“管理工具”,然后双击“Internet Information Services (IIS)管理器”。
    • 如果使用的是 Windows Server 2008 或 Windows Server 2008 R2:

      • 在任务栏上,单击“开始”,指向“管理工具”,然后单击“Internet Information Services (IIS)管理器”
    • 如果使用的是 Windows Vista 或 Windows 7:

      • 在任务栏上,单击“开始”,然后单击“控制面板”。
      • 双击“管理工具”,然后双击“Internet Information Services (IIS)管理器”
  2. 在“连接”窗格中,展开服务器名称,展开“站点”,然后单击要在其中配置绑定的网站

  3. 在“操作”窗格中,单击“绑定…”

  4. 在“站点绑定”对话框中,单击“添加…”
    Screenshot of the Site Bindings dialog box.

  5. 在“添加站点绑定”对话框中,添加绑定信息,然后单击“确定”
    Screenshot of the Add Site Binding dialog box.

如何创建集中式证书存储

  1. 按“开始”按钮,然后单击“服务器管理器”。 在服务器管理器中,单击“工具”,然后单击“Internet Information Services (IIS)管理器”
  2. 在“连接”窗格中,选择服务器名称。
  3. 在“主页”页的“管理”区域下,双击“集中式证书”
  4. “操作”窗格中,单击“编辑功能设置”
  5. 选择“启用集中式证书”并输入物理路径。 输入路径的用户名,输入路径的密码,并确认密码。 单击“确定”。

配置

可以在 ApplicationHost.config 文件中为每个站点添加 <bindings> 元素中的 <binding> 元素。 每个站点必须至少有一个 HTTP 或 HTTPS 绑定才能通过 Internet 进行查看。

特性

属性 说明
bindingInformation 必需的字符串属性。

指定要与网站通信的信息。 例如,网站绑定包括 IP 地址(或未指定的 IP 地址)、端口号和用于与网站通信的可选主机头。
protocol 必需的字符串属性。

指定用于与站点通信的协议。
sslFlags 可选 uint 属性。

指定用于安全套接字层 (SSL) 证书的绑定类型。
  • 值“0”指定使用 IP 与端口的组合建立安全连接。 只能将一个证书绑定到 IP 地址与端口的组合。
  • 值“1”指定使用端口号和通过使用服务器名称指示 (SNI) 获得的主机名建立安全连接。
  • 值“2”指定使用集中式 SSL 证书存储建立安全连接,而无需服务器名称指示。
  • 值“3”指定使用集中式 SSL 证书存储建立安全连接,同时也需要服务器名称指示
通过集中式 SSL 证书支持,可创建可包含多个证书文件的集中式证书存储。 可命名证书文件,使其名称与其包含的主机名对应。 这样便可创建一个仅需要端口的绑定,而无需使用 IP 与端口或主机名与端口的组合。 当有请求传入时,IIS 会匹配端口,确定请求中的主机名,搜索集中式证书存储中是否有名称与之匹配的证书文件。 然后会使用该证书。

通过服务器名称指示 (SNI),主机名将作为 SSL 握手的一部分进行交换。 添加 HTTPS 类型的绑定时,会在“添加站点绑定”对话框中启用 SNI。 这对于在单个网络地址上托管多个服务器的 SSL 连接尤其有用。 有关详细信息,请参阅 IIS 8.0 服务器名称指示 (SNI):SSL 可伸缩性

仅当协议为 https 时,才设置 sslFlags 属性。 默认值为 0

子元素

无。

配置示例

以下示例定义了一个名为 Contoso 且具有两个绑定的站点。 第一个绑定用于端口 80 上 IP 地址为 192.168.0.1 的主机名“www.contoso.com”,第二个绑定用于通过端口 443 的所有 IP 地址的 HTTPS 绑定。

<site name="Contoso" id="2">
   <application path="/" applicationPool="Contoso">
      <virtualDirectory path="/" physicalPath="C:\inetpub\wwwroot" />
   </application>
   <bindings>
      <binding protocol="http" bindingInformation="192.168.0.1:80:www.contoso.com" />
      <binding protocol="https" bindingInformation="*:443:" />
   </bindings>
</site>

代码示例

以下示例配置了一个名为 Contoso 的站点,该站点包含端口 80 上 IP 地址为 192.168.0.1 的主机名“www.contoso.com”,以及通过端口 443 的所有 IP 地址的 HTTPS 绑定。

AppCmd.exe

appcmd.exe set site /site.name:Contoso /+bindings.[protocol='http',bindingInformation='192.168.0.1:80:www.contoso.com']

appcmd.exe set site /site.name:Contoso /+bindings.[protocol='https',bindingInformation='*:443:']

或者,可以使用:

appcmd.exe set config -section:system.applicationHost/sites /+"[name='Contoso'].bindings.[protocol='http',bindingInformation='192.168.0.1:80:www.contoso.com']" /commit:apphost

appcmd.exe set config -section:system.applicationHost/sites /+"[name='Contoso'].bindings.[protocol='https',bindingInformation='*:443:']" /commit:apphost

注意

使用 AppCmd.exe 配置这些设置时,必须确保将 commit 参数设置为 apphost。 这会将配置设置提交到 ApplicationHost.config 文件中的相应位置部分。

C#

using System;
using System.Text;
using Microsoft.Web.Administration;

internal static class Sample
{
   private static void Main()
   {
      using (ServerManager serverManager = new ServerManager())
      {
          Configuration config = serverManager.GetApplicationHostConfiguration();
          ConfigurationSection sitesSection = config.GetSection("system.applicationHost/sites");
          ConfigurationElementCollection sitesCollection = sitesSection.GetCollection();
          ConfigurationElement siteElement = FindElement(sitesCollection, "site", "name", @"Contoso");

          if (siteElement == null) throw new InvalidOperationException("Element not found!");

          ConfigurationElementCollection bindingsCollection = siteElement.GetCollection("bindings");
          ConfigurationElement bindingElement = bindingsCollection.CreateElement("binding");
          bindingElement["protocol"] = @"http";
          bindingElement["bindingInformation"] = @"192.168.0.1:80:www.contoso.com";
          bindingsCollection.Add(bindingElement);

          ConfigurationElement bindingElement1 = bindingsCollection.CreateElement("binding");
          bindingElement1["protocol"] = @"https";
          bindingElement1["bindingInformation"] = @"*:443:";
          bindingsCollection.Add(bindingElement1);

          serverManager.CommitChanges();
      }
   }

   private static ConfigurationElement FindElement(ConfigurationElementCollection collection, string elementTagName, params string[] keyValues)
   {
      foreach (ConfigurationElement element in collection)
      {
         if (String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase))
         {
            bool matches = true;
            for (int i = 0; i < keyValues.Length; i += 2)
            {
               object o = element.GetAttributeValue(keyValues[i]);
               string value = null;
               if (o != null)
               {
                  value = o.ToString();
               }
               if (!String.Equals(value, keyValues[i + 1], StringComparison.OrdinalIgnoreCase))
               {
                  matches = false;
                  break;
               }
            }
            if (matches)
            {
               return element;
            }
         }
      }
      return null;
   }
}

VB.NET

Imports System
Imports System.Text
Imports Microsoft.Web.Administration

Module Sample
   Sub Main()
      Dim serverManager As ServerManager = New ServerManager
      Dim config As Configuration = serverManager.GetApplicationHostConfiguration
      Dim sitesSection As ConfigurationSection = config.GetSection("system.applicationHost/sites")
      Dim sitesCollection As ConfigurationElementCollection = sitesSection.GetCollection
      Dim siteElement As ConfigurationElement = FindElement(sitesCollection, "site", "name", "Contoso")

      If (siteElement Is Nothing) Then
         Throw New InvalidOperationException("Element not found!")
      End If

      Dim bindingsCollection As ConfigurationElementCollection = siteElement.GetCollection("bindings")

      Dim bindingElement As ConfigurationElement = bindingsCollection.CreateElement("binding")
      bindingElement("protocol") = "http"
      bindingElement("bindingInformation") = "192.168.0.1:80:www.contoso.com"
      bindingsCollection.Add(bindingElement)

      Dim bindingElement1 As ConfigurationElement = bindingsCollection.CreateElement("binding")
      bindingElement1("protocol") = "https"
      bindingElement1("bindingInformation") = "*:443:"
      bindingsCollection.Add(bindingElement1)

      serverManager.CommitChanges()
   End Sub

   Private Function FindElement(ByVal collection As ConfigurationElementCollection, ByVal elementTagName As String, ByVal ParamArray keyValues() As String) As ConfigurationElement
      For Each element As ConfigurationElement In collection
         If String.Equals(element.ElementTagName, elementTagName, StringComparison.OrdinalIgnoreCase) Then
            Dim matches As Boolean = True
            Dim i As Integer
            For i = 0 To keyValues.Length - 1 Step 2
               Dim o As Object = element.GetAttributeValue(keyValues(i))
               Dim value As String = Nothing
               If (Not (o) Is Nothing) Then
                  value = o.ToString
               End If
               If Not String.Equals(value, keyValues((i + 1)), StringComparison.OrdinalIgnoreCase) Then
                  matches = False
                  Exit For
               End If
            Next
            If matches Then
               Return element
            End If
         End If
      Next
      Return Nothing
   End Function


End Module

JavaScript

var adminManager = new ActiveXObject('Microsoft.ApplicationHost.WritableAdminManager');
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST";
var sitesSection = adminManager.GetAdminSection("system.applicationHost/sites", "MACHINE/WEBROOT/APPHOST");
var sitesCollection = sitesSection.Collection;
var siteElementPos = FindElement(sitesCollection, "site", ["name", "Contoso"]);

if (siteElementPos == -1) throw "Element not found!";

var siteElement = sitesCollection.Item(siteElementPos);
var bindingsCollection = siteElement.ChildElements.Item("bindings").Collection;

var bindingElement = bindingsCollection.CreateNewElement("binding");
bindingElement.Properties.Item("protocol").Value = "http";
bindingElement.Properties.Item("bindingInformation").Value = "192.168.0.1:80:www.contoso.com";
bindingsCollection.AddElement(bindingElement);

var bindingElement1 = bindingsCollection.CreateNewElement("binding");
bindingElement1.Properties.Item("protocol").Value = "https";
bindingElement1.Properties.Item("bindingInformation").Value = "*:443:";
bindingsCollection.AddElement(bindingElement1);

adminManager.CommitChanges();

function FindElement(collection, elementTagName, valuesToMatch) {
   for (var i = 0; i < collection.Count; i++) {
      var element = collection.Item(i);
      if (element.Name == elementTagName) {
         var matches = true;
         for (var iVal = 0; iVal < valuesToMatch.length; iVal += 2) {
            var property = element.GetPropertyByName(valuesToMatch[iVal]);
            var value = property.Value;
            if (value != null) {
               value = value.toString();
            }
            if (value != valuesToMatch[iVal + 1]) {
               matches = false;
               break;
            }
         }
         if (matches) {
            return i;
         }
      }
   }
   return -1;
}

VBScript

Set adminManager = createObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set sitesSection = adminManager.GetAdminSection("system.applicationHost/sites", "MACHINE/WEBROOT/APPHOST")
Set sitesCollection = sitesSection.Collection
siteElementPos = FindElement(sitesCollection, "site", Array("name", "Contoso"))

If siteElementPos = -1 Then
   WScript.Echo "Element not found!"
   WScript.Quit
End If

Set siteElement = sitesCollection.Item(siteElementPos)
Set bindingsCollection = siteElement.ChildElements.Item("bindings").Collection

Set bindingElement = bindingsCollection.CreateNewElement("binding")
bindingElement.Properties.Item("protocol").Value = "http"
bindingElement.Properties.Item("bindingInformation").Value = "192.168.0.1:80:www.contoso.com"
bindingsCollection.AddElement(bindingElement)

Set bindingElement1 = bindingsCollection.CreateNewElement("binding")
bindingElement1.Properties.Item("protocol").Value = "https"
bindingElement1.Properties.Item("bindingInformation").Value = "*:443:"
bindingsCollection.AddElement(bindingElement1)

adminManager.CommitChanges()

Function FindElement(collection, elementTagName, valuesToMatch)
   For i = 0 To CInt(collection.Count) - 1
      Set element = collection.Item(i)
      If element.Name = elementTagName Then
         matches = True
         For iVal = 0 To UBound(valuesToMatch) Step 2
            Set property = element.GetPropertyByName(valuesToMatch(iVal))
            value = property.Value
            If Not IsNull(value) Then
               value = CStr(value)
            End If
            If Not value = CStr(valuesToMatch(iVal + 1)) Then
               matches = False
               Exit For
            End If
         Next
         If matches Then
            Exit For
         End If
      End If
   Next
   If matches Then
      FindElement = i
   Else
      FindElement = -1
   End If
End Function