Microsoft IIS (Internet Information Services) 使用指南

Microsoft IIS (Internet Information Services) Usage Guide

概述 (Overview)

Microsoft Internet Information Services (IIS) 是Windows平台上的企业级Web服务器,与Windows Server深度集成。IIS以其强大的.NET支持、Windows集成认证、图形化管理界面和企业级功能而著称,是Windows环境下部署Web应用的首选方案。

Microsoft Internet Information Services (IIS) is an enterprise-class web server on the Windows platform, deeply integrated with Windows Server. IIS is known for its powerful .NET support, Windows Integrated Authentication, graphical management interface, and enterprise-grade features, making it the preferred solution for deploying web applications in Windows environments.

核心特性 (Core Features)

🪟 Windows集成 (Windows Integration)

  • 与Windows Server深度集成
  • Active Directory身份验证
  • Windows性能监控
  • 事件日志集成
  • IIS Manager图形化管理

🔧 应用平台 (Application Platform)

  • ASP.NET原生支持
  • .NET Core/ASP.NET Core托管
  • PHP、Python、Node.js支持
  • 经典ASP支持
  • CGI/FastCGI支持

🛡️ 安全特性 (Security Features)

  • Windows集成认证
  • SSL/TLS加密
  • IP地址限制
  • 请求过滤
  • URL授权
  • 应用池隔离

⚡ 性能与扩展 (Performance and Scalability)

  • HTTP.sys内核模式驱动
  • 应用池和进程隔离
  • 输出缓存
  • 动态内容压缩
  • Web Farm集群支持

版本历史 (Version History)

版本 Windows版本 主要特性
IIS 10.0 Windows Server 2016/2019/2022 HTTP/2、Nano Server支持
IIS 8.5 Windows Server 2012 R2 动态站点激活、ETW日志
IIS 8.0 Windows Server 2012 SNI、WebSocket支持
IIS 7.5 Windows Server 2008 R2 内核缓存改进
IIS 7.0 Windows Server 2008 模块化架构、集成管道

当前版本特性

IIS 10.0 (Windows Server 2022)

  • HTTP/2支持
  • 改进的容器支持
  • 增强的安全特性
  • 更好的性能优化

架构设计 (Architecture Design)

核心组件

IIS架构层次
├── HTTP.sys (内核模式)
│   ├── HTTP请求处理
│   ├── 响应缓存
│   └── SSL/TLS处理
├── WAS (进程激活服务)
│   ├── 应用池管理
│   └── 进程监控
├── W3SVC (WWW服务)
│   ├── HTTP协议支持
│   └── 配置管理
└── 应用池 (Application Pools)
    ├── Worker进程隔离
    ├── .NET运行时
    └── 应用程序域

请求处理流程

1. HTTP.sys接收请求(内核模式)
2. HTTP.sys路由到应用池
3. Worker进程处理请求
4. 管道模块处理(认证、授权等)
5. 应用程序代码执行
6. 响应返回给HTTP.sys
7. HTTP.sys发送响应给客户端

安装与配置 (Installation and Configuration)

Windows Server安装

# PowerShell安装IIS
Install-WindowsFeature -Name Web-Server -IncludeManagementTools

# 安装ASP.NET 4.x
Install-WindowsFeature -Name Web-Asp-Net45

# 安装.NET Core托管包
# 从 https://dotnet.microsoft.com/download 下载并安装

# 安装URL Rewrite模块
# 从 https://www.iis.net/downloads/microsoft/url-rewrite 下载

Windows 10/11启用IIS

控制面板 → 程序 → 启用或关闭Windows功能
勾选:
- Internet Information Services
  - Web管理工具
  - 万维网服务
    - 应用程序开发功能
    - 常见HTTP功能
    - 安全性

站点管理 (Site Management)

创建新站点

# PowerShell创建站点
New-IISSite -Name "MyWebSite" `
    -PhysicalPath "C:\inetpub\wwwroot\mysite" `
    -BindingInformation "*:80:www.example.com"

# 添加HTTPS绑定
New-IISSiteBinding -Name "MyWebSite" `
    -BindingInformation "*:443:www.example.com" `
    -Protocol https `
    -CertificateThumbPrint "cert_thumbprint"

web.config配置示例

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <!-- URL重写 -->
    <rewrite>
      <rules>
        <rule name="HTTPS Redirect" stopProcessing="true">
          <match url="(.*)" />
          <conditions>
            <add input="{HTTPS}" pattern="off" />
          </conditions>
          <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
        </rule>
      </rules>
    </rewrite>

    <!-- 压缩配置 -->
    <urlCompression doStaticCompression="true" doDynamicCompression="true" />

    <!-- 静态内容缓存 -->
    <staticContent>
      <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
    </staticContent>

    <!-- 安全头 -->
    <httpProtocol>
      <customHeaders>
        <add name="X-Frame-Options" value="SAMEORIGIN" />
        <add name="X-Content-Type-Options" value="nosniff" />
      </customHeaders>
    </httpProtocol>
  </system.webServer>
</configuration>

应用池配置 (Application Pool Configuration)

应用池设置

# 创建应用池
New-WebAppPool -Name "MyAppPool"

# 配置.NET版本
Set-ItemProperty IIS:\AppPools\MyAppPool `
    -Name managedRuntimeVersion -Value "v4.0"

# 配置管道模式(集成/经典)
Set-ItemProperty IIS:\AppPools\MyAppPool `
    -Name managedPipelineMode -Value "Integrated"

# 配置32位应用支持
Set-ItemProperty IIS:\AppPools\MyAppPool `
    -Name enable32BitAppOnWin64 -Value $true

# 配置空闲超时
Set-ItemProperty IIS:\AppPools\MyAppPool `
    -Name processModel.idleTimeout -Value "00:20:00"

# 配置回收设置
Set-ItemProperty IIS:\AppPools\MyAppPool `
    -Name recycling.periodicRestart.time -Value "29.00:00:00"

应用池隔离

优势:
├── 安全隔离:一个应用崩溃不影响其他应用
├── 资源控制:限制CPU和内存使用
├── 独立回收:每个池独立回收策略
└── 身份管理:不同身份运行不同应用

SSL/TLS配置 (SSL/TLS Configuration)

证书绑定

# 导入证书
$cert = Import-PfxCertificate -FilePath "C:\cert\mysite.pfx" `
    -CertStoreLocation Cert:\LocalMachine\My `
    -Password (ConvertTo-SecureString "password" -AsPlainText -Force)

# 绑定到站点
New-WebBinding -Name "MyWebSite" `
    -Protocol https -Port 443 `
    -IPAddress * -HostHeader "www.example.com"

# 关联证书
$binding = Get-WebBinding -Name "MyWebSite" -Protocol https
$binding.AddSslCertificate($cert.Thumbprint, "My")

SSL配置优化

# 禁用弱加密协议
New-Item 'HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.0\Server' -Force
New-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.0\Server' -Name 'Enabled' -Value 0 -PropertyType 'DWord'

# 启用TLS 1.2和1.3
New-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Server' -Name 'Enabled' -Value 1 -PropertyType 'DWord'

ASP.NET Core托管 (ASP.NET Core Hosting)

安装托管包

# 下载并安装.NET Core Hosting Bundle
# https://dotnet.microsoft.com/download/dotnet

# 重启IIS
net stop was /y
net start w3svc

web.config for ASP.NET Core

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" 
           modules="AspNetCoreModuleV2" 
           resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath="dotnet" 
                arguments=".\MyApp.dll" 
                stdoutLogEnabled="false" 
                stdoutLogFile=".\logs\stdout" 
                hostingModel="inprocess">
      <environmentVariables>
        <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Production" />
      </environmentVariables>
    </aspNetCore>
  </system.webServer>
</configuration>

负载均衡与高可用 (Load Balancing and High Availability)

ARR (Application Request Routing)

# 安装ARR模块
# 从 https://www.iis.net/downloads/microsoft/application-request-routing 下载

# 创建服务器农场
New-WebFarm -Name "MyFarm" `
    -Servers @(
        @{Address="server1.local"; Port=80},
        @{Address="server2.local"; Port=80}
    )

健康检查配置

<webFarms>
  <webFarm name="MyFarm" enabled="true">
    <servers>
      <server address="server1.local" enabled="true" />
      <server address="server2.local" enabled="true" />
    </servers>
    <applicationRequestRouting>
      <protocol timeout="00:01:00" />
      <healthCheck url="http://localhost/health" 
                   interval="00:00:30" 
                   timeout="00:00:10" />
    </applicationRequestRouting>
  </webFarm>
</webFarms>

性能优化 (Performance Optimization)

缓存配置

<!-- 输出缓存 -->
<system.webServer>
  <caching enabled="true">
    <profiles>
      <add extension=".html" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
      <add extension=".js" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
      <add extension=".css" policy="CacheUntilChange" kernelCachePolicy="CacheUntilChange" />
    </profiles>
  </caching>
</system.webServer>

压缩配置

<system.webServer>
  <httpCompression>
    <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
    <dynamicTypes>
      <add mimeType="text/*" enabled="true" />
      <add mimeType="application/javascript" enabled="true" />
      <add mimeType="application/json" enabled="true" />
    </dynamicTypes>
    <staticTypes>
      <add mimeType="text/*" enabled="true" />
      <add mimeType="application/javascript" enabled="true" />
      <add mimeType="image/svg+xml" enabled="true" />
    </staticTypes>
  </httpCompression>
</system.webServer>

安全最佳实践 (Security Best Practices)

请求过滤

<system.webServer>
  <security>
    <requestFiltering>
      <!-- 限制请求大小 -->
      <requestLimits maxAllowedContentLength="10485760" maxQueryString="2048" />

      <!-- 文件扩展名过滤 -->
      <fileExtensions>
        <remove fileExtension=".exe" />
        <remove fileExtension=".bat" />
      </fileExtensions>

      <!-- 隐藏敏感目录 -->
      <hiddenSegments>
        <add segment="bin" />
        <add segment="App_Data" />
      </hiddenSegments>
    </requestFiltering>
  </security>
</system.webServer>

IP安全限制

# 允许特定IP访问
Add-WebConfigurationProperty -Filter "/system.webServer/security/ipSecurity" `
    -PSPath "IIS:\Sites\MyWebSite" `
    -Name "." -Value @{ipAddress="192.168.1.100"; allowed="true"}

# 拒绝IP范围
Add-WebConfigurationProperty -Filter "/system.webServer/security/ipSecurity" `
    -PSPath "IIS:\Sites\MyWebSite" `
    -Name "." -Value @{ipAddress="10.0.0.0"; subnetMask="255.0.0.0"; allowed="false"}

监控与日志 (Monitoring and Logging)

日志配置

<system.webServer>
  <httpLogging dontLog="false">
    <customFields>
      <clear />
      <add logFieldName="User-Agent" sourceName="User-Agent" sourceType="RequestHeader" />
      <add logFieldName="X-Forwarded-For" sourceName="X-Forwarded-For" sourceType="RequestHeader" />
    </customFields>
  </httpLogging>
</system.webServer>

性能监控

# 使用性能计数器
Get-Counter '\Web Service(*)\Current Connections'
Get-Counter '\ASP.NET Applications(*)\Requests/Sec'
Get-Counter '\Process(w3wp*)\% Processor Time'

常用命令 (Common Commands)

# 站点管理
Start-WebSite -Name "MyWebSite"
Stop-WebSite -Name "MyWebSite"
Restart-WebAppPool -Name "MyAppPool"

# 重启IIS
iisreset /restart

# 查看站点列表
Get-Website

# 查看应用池
Get-IISAppPool

# 导出配置
Export-WebConfiguration -Path "IIS:\Sites\MyWebSite" -Destination "C:\backup\config.xml"

# 查看绑定
Get-WebBinding -Name "MyWebSite"

与其他Web服务器对比 (Comparison)

特性 IIS Nginx Apache
平台 Windows 跨平台 跨平台
.NET支持 ⭐⭐⭐⭐⭐ ⭐⭐ ⭐⭐⭐
图形界面 ✅ 内置
Windows认证 ✅ 原生 ⚠️ 有限
性能 ⭐⭐⭐⭐ ⭐⭐⭐⭐⭐ ⭐⭐⭐
学习曲线 平缓 中等 陡峭
开源

使用场景 (Use Cases)

✅ 理想场景

  • Windows Server环境
  • ASP.NET/.NET Core应用
  • 需要Windows集成认证
  • SharePoint、Exchange等微软产品
  • 企业内部应用
  • Active Directory集成

⚠️ 考虑其他方案

  • Linux优先环境
  • 非.NET技术栈
  • 需要极致性能
  • 开源方案优先
  • 预算受限(Windows授权)

学习资源 (Learning Resources)

总结 (Summary)

IIS是Windows环境下功能强大、企业级的Web服务器解决方案,特别适合:

  1. .NET应用托管: ASP.NET和ASP.NET Core的最佳平台
  2. Windows集成: 深度集成Windows Server功能
  3. 企业应用: 成熟的管理和监控工具
  4. 微软生态: 与微软产品无缝集成

对于Windows环境下的Web应用部署,IIS提供了完整、可靠的解决方案。


最后更新: 2025年11月

Last Updated: November 2025

powered by Gitbook© 2025 编外计划 | 最后修改: 2025-11-25 18:08:14

results matching ""

    No results matching ""