概觀
元素 <limits> 的 <site> 元素會設定限制對月臺用戶端要求之頻寬數量、連線數目或連線逾時的設定。
注意
如果元素 <limits> 同時在 <siteDefaults> 區段和 <site> 特定月臺的 區段中設定,則區段中的 <site> 組態會用於該網站。
相容性
| 版本 | 備註 |
|---|---|
| IIS 10.0 | 未在 IIS 10.0 中修改專案 <limits> 。 |
| IIS 8.5 | 未在 IIS 8.5 中修改專案 <limits> 。 |
| IIS 8.0 |
maxUrlSegments已新增 屬性,以指定 URL 中允許的區段數目上限。 |
| IIS 7.5 | 未在 IIS 7.5 中修改專案 <limits> 。 |
| IIS 7.0 | 元素 <limits> 的 <site> 元素是在 IIS 7.0 中引進。 |
| IIS 6.0 | 元素 <limits> 會取代下列 IIS 6.0 中繼基底設定:
|
安裝程式
元素 <limits> 的 <site> 元素包含在 IIS 7 和更新版本的預設安裝中。
作法
如何設定月臺的連線限制選項
開啟 [Internet Information Services (IIS) 管理員:
如果您使用 Windows Server 2012 或 Windows Server 2012 R2:
- 在工作列上,依序按一下 [伺服器管理員]、[工具],然後按一下 [Internet Information Services (IIS) Manager]。
如果您使用 Windows 8 或 Windows 8.1:
- 按住Windows鍵,按字母X,然後按一下[主控台]。
- 按一下 [系統管理工具],然後按兩下 [ Internet Information Services (IIS) Manager]。
如果您使用 Windows Server 2008 或 Windows Server 2008 R2:
- 在工作列上,按一下 [ 開始],指向 [ 系統管理工具],然後按一下 [ Internet Information Services (IIS) 管理員]。
如果您使用 Windows Vista 或 Windows 7:
- 在工作列上,按一下 [開始],然後按一下[主控台]。
- 按兩下 [系統管理工具],然後按兩下 [ Internet Information Services] (IIS) Manager。
在 [ 連線] 窗格中,展開伺服器名稱、展開 [ 站 台] 節點,然後按一下網站的名稱。
在網站的 [首頁] 窗格中,按一下 [動作] 窗格中的 [進階設定...]。
在 [ 進階設定] 對話方塊中,展開 [ 限制],指定連線限制選項,然後按一下 [ 確定]。
組態
屬性
| 屬性 | 描述 |
|---|---|
connectionTimeout |
選擇性的 timeSpan 屬性。 指定 IIS 在中斷連線時, (以秒為單位的時間,) IIS 會先等候,然後再將被視為非作用中的連線中斷連線。 基於下列原因,連線可視為非使用中:
00:02:00 兩分鐘) 。 |
maxBandwidth |
選擇性 uint 屬性。 指定用於月臺的網路頻寬上限,以每秒位元組為單位。 使用此設定可協助防止使用 IIS 活動多載網路。 預設值是 4294967295。 |
maxConnections |
選擇性 uint 屬性。 指定月臺的連線數目上限。 使用此設定來限制同時用戶端連線的數目。 預設值是 4294967295。 |
maxurlSegments |
選擇性 uint 屬性。 指定 URL 中允許的區段數目上限。 預設值是 32。 |
子元素
無。
組態範例
下列設定範例顯示一個網站,其中最大頻寬設定為每秒 65,536 個位元組、連線數目上限設定為 1024,而連線逾時設定為 1 分鐘。
<sites>
<site name="Default Web Site" id="1" serverAutoStart="true">
<application path="/">
<virtualDirectory path="/"
physicalPath="%SystemDrive%\inetpub\wwwroot" />
</application>
<bindings>
<binding protocol="http"
bindingInformation="*:80:" />
</bindings>
<limits maxBandwidth="65536"
maxConnections="1024"
connectionTimeout="00:01:00" />
</site>
</sites>
範例程式碼
下列程式碼範例會將預設網站設定為每秒 65,536 個位元組的最大頻寬、1024 個最大連線,以及連線逾時 1 分鐘。
AppCmd.exe
appcmd.exe set config -section:system.applicationHost/sites "/[name='Default Web Site'].limits.maxBandwidth:65536" /commit:apphost
appcmd.exe set config -section:system.applicationHost/sites "/[name='Default Web Site'].limits.maxConnections:1024" /commit:apphost
appcmd.exe set config -section:system.applicationHost/sites "/[name='Default Web Site'].limits.connectionTimeout:00:01:00" /commit:apphost
注意
當您使用 AppCmd.exe 來設定這些設定時,請務必將 認可 參數 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", @"Default Web Site");
if (siteElement == null) throw new InvalidOperationException("Element not found!");
ConfigurationElement limitsElement = siteElement.GetChildElement("limits");
limitsElement["maxBandwidth"] = 65536;
limitsElement["maxConnections"] = 1024;
limitsElement["connectionTimeout"] = TimeSpan.Parse("00:01:00");
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", "Default Web Site")
If (siteElement Is Nothing) Then
Throw New InvalidOperationException("Element not found!")
End If
Dim limitsElement As ConfigurationElement = siteElement.GetChildElement("limits")
limitsElement("maxBandwidth") = 65536
limitsElement("maxConnections") = 1024
limitsElement("connectionTimeout") = TimeSpan.Parse("00:01:00")
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", "Default Web Site"]);
if (siteElementPos == -1) throw "Element not found!";
var siteElement = sitesCollection.Item(siteElementPos);
var limitsElement = siteElement.ChildElements.Item("limits");
limitsElement.Properties.Item("maxBandwidth").Value = 65536;
limitsElement.Properties.Item("maxConnections").Value = 1024;
limitsElement.Properties.Item("connectionTimeout").Value = "00:01:00";
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 = WScript.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", "Default Web Site"))
If siteElementPos = -1 Then
WScript.Echo "Element not found!"
WScript.Quit
End If
Set siteElement = sitesCollection.Item(siteElementPos)
Set limitsElement = siteElement.ChildElements.Item("limits")
limitsElement.Properties.Item("maxBandwidth").Value = 65536
limitsElement.Properties.Item("maxConnections").Value = 1024
limitsElement.Properties.Item("connectionTimeout").Value = "00:01:00"
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