iis服务器修改端口,Bindings <bindings>

该元素用于配置IIS7及以上版本网站的绑定信息,定义了请求接触网站的协议方式。例如,若网站需要HTTP和HTTPS协议,需分别设置绑定。此元素可在ApplicationHost.config文件中为每个站点添加,并通过元素覆盖服务器级别的默认设置。设置包括添加新的站点绑定,如指定IP地址、端口和主机名。
摘要由CSDN通过智能技术生成

Bindings

09/26/2016

6 分钟可看完

本文内容

Overview

The element configures binding information for an IIS 7 or later Web site. It can also define the default bindings for all sites on the Web server if it is included in the element.

This element can contain a collection of elements. Each element in the collection defines a separate set of binding information that a request can use to contact the Web site. For example, if your site requires users to contact it using both the HTTP protocol and the HTTPS protocol, you must define a binding for each protocol.

You can also use element in the element to override binding defaults inherited from the server level element.

Compatibility

Version

Notes

IIS 10.0

The element was not modified in IIS 10.0.

IIS 8.5

The element was not modified in IIS 8.5.

IIS 8.0

The element was not modified in IIS 8.0.

IIS 7.5

The element was not modified in IIS 7.5.

IIS 7.0

The element was introduced in IIS 7.0.

IIS 6.0

The collection replaces sections of the ServerBindings property on the IIS 6.0 IIsWebServer metabase object.

Setup

The element is included in the default installation of IIS 7 or later.

How To

How to add binding information to a site

Open Internet Information Services (IIS) Manager:

If you are using Windows Server 2012 or Windows Server 2012 R2:

On the taskbar, click Server Manager, click Tools, and then click Internet Information Services (IIS) Manager.

If you are using Windows 8 or Windows 8.1:

Hold down the Windows key, press the letter X, and then click Control Panel.

Click Administrative Tools, and then double-click Internet Information Services (IIS) Manager.

If you are using Windows Server 2008 or Windows Server 2008 R2:

On the taskbar, click Start, point to Administrative Tools, and then click Internet Information Services (IIS) Manager.

If you are using Windows Vista or Windows 7:

On the taskbar, click Start, and then click Control Panel.

Double-click Administrative Tools, and then double-click Internet Information Services (IIS) Manager.

In the Connections pane, expand the server name, expand Sites, and then click the Web site on which you want to configure the bindings.

In the Actions pane, click Bindings...

In the Site Bindings dialog box, click Add...

1b48fd441e663a5a2ba6150243716c1c.png

In the Add Site Binding dialog box, add the binding information, and then click OK.

1461850e07eb00007b60eaa4f8f26001.png

Configuration

You can add a element for each site in the ApplicationHost.config file, which can contain a collection of individual elements that define the individual protocol bindings for the site. Each site will need at least one HTTP or HTTPS binding to be viewable over the Internet.

You can also use element in the element to override binding defaults inherited from the server level element.

Attributes

None.

Child Elements

Element

Description

Optional element.

Configures a binding in the parent site.

clear

Optional element.

Clears references to default settings that are inherited from the parent level of the configuration.

Configuration Sample

The following example defines a site named Contoso with two bindings. The first binding is for a hostname of "www.contoso.com" on port 80 for the IP address of 192.168.0.1, and the second binding is for an HTTPS binding for all IP addresses over port 443.

Sample Code

The following examples configure a site named Contoso with a hostname of "www.contoso.com" on port 80 for the IP address of 192.168.0.1, and an HTTPS binding for all IP addresses over port 443.

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:']

Or you can use:

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

Note

You must be sure to set the commit parameter to apphost when you use AppCmd.exe to configure these settings. This commits the configuration settings to the appropriate location section in the ApplicationHost.config file.

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值