添加默认绑定 <binding>

概述

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

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

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

兼容性

版本 说明
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 及更高版本的默认安装中。

操作方式

如何将默认绑定信息添加到服务器

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

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

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

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

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

      • 在任务栏上,单击“开始”,然后单击“控制面板”。
      • 双击“管理工具”,然后双击“Internet 信息服务(IIS)管理器”。
  2. 在“连接”窗格中,选择服务器名称。

  3. 在“主页”窗格中,双击“配置编辑器”。

  4. 移到“部分”中的“system.applicationHost/sites”。

  5. 在“网站绑定”对话框中,单击“添加...”

  6. 展开“siteDefaults”

  7. 选择“绑定”,然后单击右侧的省略号以打开集合编辑器。

    “网站绑定”对话框的屏幕截图,其中显示了“网站默认值”节点的展开和选中的“绑定”。

  8. 单击“添加”,然后输入 bindingInformation、protocol 以及 sslFlags 的值。

  9. 关闭“集合编辑器”,然后在“操作”窗格中,单击“应用”。

配置

可在 ApplicationHost.config 文件的 <siteDefaults> 部分的 <bindings> 元素中添加默认 <binding> 元素。

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

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

指定用于与站点通信的协议。
sslFlags 类似于标志类型的可选 uint 属性,具有以下可能的标志:
说明
0 使用 IP/端口组合建立的安全连接。 只能将一个证书绑定到 IP 地址与端口的组合。
1 使用端口号和使用服务器名称指示(SNI)获取的主机名的安全连接。
2 使用集中式 SSL 证书存储建立安全连接。
4 禁用 HTTP/2。
8 禁用 OCSP 装订。
16 禁用 QUIC。
32 通过 TCP 禁用 TLS 1.3。
64 禁用旧版 TLS。
集中式 SSL 证书支持允许创建可包含多个证书文件的集中式证书存储。 证书文件可以命名为对应于它们包含的主机名。 这允许创建仅需要端口的绑定,而不是 IP/端口或主机名/端口组合。 当有请求传入时,IIS 会匹配端口,确定请求中的主机名,搜索集中式证书存储区中是否有名称与之匹配的证书文件。 然后会使用该证书。

通过服务器名称指示 (SNI),主机名将作为 SSL 握手的一部分进行交换。 添加具有 HTTPS 类型的绑定时,在“添加网站绑定”对话框中启用 SNI。 这对于在单个网络地址上托管多个服务器的 SSL 连接非常有用。

只有 IIS 10 版本 1809 及更高版本中才支持大于 4 的值。 默认值为 0

子元素

无。

配置示例

以下配置示例指定 IIS 7 的默认 bindings 选项。

<system.applicationHost>
   <sites>
      <siteDefaults>
         <bindings>
            <binding protocol="http" bindingInformation="127.0.0.1:8080:" />
         </bindings>
      </siteDefaults>
   </sites>
</system.applicationHost>

代码示例

以下代码示例配置 IIS 7 的默认 bindings 选项。

AppCmd.exe

appcmd.exe set config -section:system.applicationHost/sites /siteDefaults.bindings.[protocol='http',bindingInformation='*:8080:contoso.com'].bindingInformation:"127.0.0.1:8080:" /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");
         ConfigurationElement siteDefaultsElement = sitesSection.GetChildElement("siteDefaults");

         ConfigurationElementCollection bindingsCollection = siteDefaultsElement.GetCollection("bindings");
         ConfigurationElement bindingElement = bindingsCollection.CreateElement("binding");
         bindingElement["protocol"] = @"http";
         bindingElement["bindingInformation"] = @"127.0.0.1:8080:";
         bindingsCollection.Add(bindingElement);

         serverManager.CommitChanges();
      }
   }
}

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 siteDefaultsElement As ConfigurationElement = sitesSection.GetChildElement("siteDefaults")

      Dim bindingsCollection As ConfigurationElementCollection = siteDefaultsElement.GetCollection("bindings")
      Dim bindingElement As ConfigurationElement = bindingsCollection.CreateElement("binding")
      bindingElement("protocol") = "http"
      bindingElement("bindingInformation") = "127.0.0.1:8080:"
      bindingsCollection.Add(bindingElement)

      serverManager.CommitChanges()
   End Sub

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 siteDefaultsElement = sitesSection.ChildElements.Item("siteDefaults");

var bindingsCollection = siteDefaultsElement.ChildElements.Item("bindings").Collection;
var bindingElement = bindingsCollection.CreateNewElement("binding");
bindingElement.Properties.Item("protocol").Value = "http";
bindingElement.Properties.Item("bindingInformation").Value = "127.0.0.1:8080:";
bindingsCollection.AddElement(bindingElement);

adminManager.CommitChanges();

VBScript

Set adminManager = WScript.CreateObject("Microsoft.ApplicationHost.WritableAdminManager")
adminManager.CommitPath = "MACHINE/WEBROOT/APPHOST"
Set sitesSection = adminManager.GetAdminSection("system.applicationHost/sites", "MACHINE/WEBROOT/APPHOST")
Set siteDefaultsElement = sitesSection.ChildElements.Item("siteDefaults")

Set bindingsCollection = siteDefaultsElement.ChildElements.Item("bindings").Collection
Set bindingElement = bindingsCollection.CreateNewElement("binding")
bindingElement.Properties.Item("protocol").Value = "http"
bindingElement.Properties.Item("bindingInformation").Value = "127.0.0.1:8080:"
bindingsCollection.AddElement(bindingElement)

adminManager.CommitChanges()