Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

IIS 與 web.config 性能優化全棧指南:從應用程序池到 HTTP 模塊精簡

編輯:關於伺服器

一、IIS 層面優化

1. 應用程序池:最大工作進程數(Web Garden)

原理:單池多 Worker Process (w3wp.exe),利用多核 CPU 並行處理請求。

適用場景**:CPU 密集型、無共享內存狀態(Session 存 Redis/SQL)、靜態資源多。

不適用**:InProc Session、大量文件鎖/緩存依賴進程內存、COM 組件單線程模型。

設置路徑**:IIS 管理器 → 應用程序池 → 右鍵「高級設置」→「最大工作進程數」。

經驗值**:32 核 32G 內存服務器測試最優值為 5。公式參考:MIN(CPU核心數, 8),避免上下文切換開銷超過收益。

2. 關閉 IIS 訪問日誌(提升 5-8% 吞吐)

操作**:站點屬性 → 「網站」選項卡 → 取消勾選「啟用日誌記錄」。

替代方案**:啟用 ETW (Event Tracing for Windows) 或 Failed Request Tracing (FREB) 僅記錄錯誤/慢請求,平衡診斷與性能。

3. machine.config 解鎖 processModel(舊版 .NET 2.0/3.5)

<!-- C:\Windows\Microsoft.NET\Framework64\v2.0.50727\CONFIG\machine.config -->
<processModel allowDefinition="Everywhere" />

允許在 Web.config 覆蓋 processModel(注意:.NET 4.0+ 已移除此節點,改用 IIS 管理界面)。

二、web.config 應用層優化(生產環境必備)

1. 移除無用 HttpModules(減少管線開銷)

<system.webServer>
  <modules>
    <remove name="OutputCache" />
    <remove name="Session" />                  <!-- 若使用 Redis Session -->
    <remove name="WindowsAuthentication" />      <!-- 非 Windows 認證 -->
    <remove name="FormsAuthentication" />        <!-- 非 Forms 認證 -->
    <remove name="PassportAuthentication" />
    <remove name="RoleManager" />
    <remove name="UrlAuthorization" />
    <remove name="FileAuthorization" />
    <remove name="AnonymousIdentification" />
    <remove name="Profile" />
  </modules>
</system.webServer>

原則**:僅保留業務必需模塊。每個模塊都會攔截每個請求,移除無用者可顯著降低 CPU/延遲。

2. 關閉頁面級無用機制

<system.web>
  <pages
    enableViewState="false"           <!-- 不用 ViewState -->
    enableViewStateMac="false"        <!-- 關閉 MAC 校驗 -->
    validateRequest="false"           <!-- 關閉內置 XSS/SQLi 檢查(需自行WAF) -->
    autoEventWireup="false"           <!-- 手動綁定事件 -->
    buffer="true"                     <!-- 保持響應緩衝 -->
  />
</system.web>

3. 自定義錯誤頁面(防止信息洩露)

<system.webServer>
  <httpErrors errorMode="DetailedLocalOnly" existingResponse="Auto" />
</system.webServer>
<system.web>
  <customErrors mode="RemoteOnly" defaultRedirect="/Error.html" />
</system.web>

RemoteOnly:本地開發見詳細錯誤,遠程用戶見友好頁面。

4. 禁用調試編譯

<system.web>
  <compilation debug="false" targetFramework="4.8" />
</system.web>

發布時必須 debug="false",否則:禁用編譯優化、產生 PDB 文件、增加內存佔用、阻礙 JIT 內聯。

5. 連接並發優化(突破 2 連接限制)

<system.net>
  <connectionManagement>
    <add address="*" maxconnection="100" />  <!-- 針對所有域名 -->
  </connectionManagement>
  <settings>
    <httpWebRequest useUnsafeHeaderParsing="true" />  <!-- 兼容非標準 Header -->
  </settings>
</system.net>

默認 maxconnection=2 導致高併發外部 API 調用排隊,調大至 100-200 視帶寬/對端限制而定。

6. 隱藏 ASP.NET 版本 Header(安全加固)

<system.web>
  <httpRuntime enableVersionHeader="false" />
</system.web>

移除 X-AspNet-Version、X-Powered-By,減少攻擊面。

三、進階:輸出緩存/壓縮/靜態文件

<system.webServer>
  <caching enabled="true" enableKernelCache="true" />
  <urlCompression doStaticCompression="true" doDynamicCompression="true" />
  <staticContent>
    <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="30.00:00:00" />
  </staticContent>
</system.webServer>

四、驗證清單

檢查項工具/命令預期結果
調試關閉瀏覽器 F12 Network → Response Headers無 X-AspNet-Version、X-Powered-By
壓縮生效curl -H "Accept-Encoding: gzip" -I URLContent-Encoding: gzip
靜態緩存F12 Network → Cache-Controlmax-age=2592000
錯誤頁面訪問不存在頁面顯示 /Error.html,無堆疊信息

總結

IIS + ASP.NET 優化核心三板斧:**減少管線攔截(移除模塊)、關閉調試與無用特性、啟用壓縮緩存**。配合應用程序池進程數調優、日誌策略收斂,可在不擴容硬件前提下提升 20-40% 吞吐量。記住:**每個配置變更必須經過預發壓測驗證**,避免因誤關鍵模塊導致業務異常。

Copyright © Windows教程網 All Rights Reserved