企业级IIS配置手册:安全加固/负载均衡/性能优化最佳实践

发布于:2025-07-23 ⋅ 阅读:(17) ⋅ 点赞:(0)

基础

安装

在Windows 10、8.1、7 等系统中,可以通过一下步骤安装IIS:

  1. 打开"控制面板",选择 “程序” ,点击 “启用或关闭Windows 功能
  2. 在弹出的窗口中,勾选 “IIS” 复选框,然后点击 “确定”
  3. 等待安装完成,重启计算机

核心组件

应用程序池(Application pool)
名词解释
  • 工作进程隔离机制(W3WP.exe)
  • 托管管道模式:集成模式VS经典模式
  • 自动回收配置(内存/时间/请求数触发)
配置应用程序池
  1. 在IIS 管理器中,找到"应用程序池"节点,右键点击,选择"添加应用程序池"
  2. 在弹出的对话框中,输入应用程序池名称,然后点击"确定"
  3. 将刚创建的网站绑定到该应用池
配置绑定网站
  1. 在IIS 管理器中,找到您刚创建的网站,右键点击,选择"确定"
  2. 在弹出的对话框中,勾选"HTTP"和"HTTPS"复选框,然后设置绑定信息,如域名,IP地址和端口
  3. 点击"确定"保存设置。
配置网站权限
  1. 在IIS管理器中,找到您刚创建的网站,右键点击,选择“权限”。
  2. 在弹出的对话框中,可以设置网站文件的读写权限。
  3. 点击“确定”保存设置。
部署网站文件

将网站文件复制到IIS中设置的物理路径下,即可完成网站部署。

配置文件层级

# 配置文件路径
C:\Windows\System32\inetsrv\config\applicationHost.config  # 全局配置
Web.config  # 站点级配置(覆盖全局)

关键运维操作

性能调优

  • 静态内容缓存
<!--web.config 配置-->
<staticContent>
   <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
</staticContent>
  • 动态压缩已启用
Set-WebConfigurationProperty -Filter /system.webServer/httpCompression -Name dynamicCompressionEnable -Value true

安全加固

  • SSL/TLS 最佳实践
# 禁用不安全的协议(PowerShell)
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.0\Server" -Name Enabled -Value 0
  • 请求过滤规则
<security>
  <requestFiltering>
    <fileExtensions allowUnlisted="false">
      <add fileExtension=".exe" allowed="false" />
    </fileExtensions>
  </requestFiltering>
</security>

故障排查

日志分析

  • 日志位置

C:\inetpub\logs\LogFiles\W3SVC1

  • 关键日志字段

sc-status(状态码)、cs-method(请求方法)、cs-uri-stem(请求路径)

常见错误处理

错入代码 可能原因 解决方案
Http 503 应用程序池崩溃 检查事件查看器->windows 日志->Application
HTTP 403.14 目录浏览禁用 添加默认文档(index.html等)
HTTP 500.19 Web.config格式错误 运行%windir%\system32\inetsrv\appcmd migrate config

诊断工具

  • Failed Request Tracing(FRT)
# 启用失败请求跟踪
Set-WebConfigurationProperty -Filter system.applicationHost/sites/siteDefaults/traceFailedRequestsLogging -Name enabled -Value true
  • 实时监控
# 查看当前请求(需安装IIS管理控制台)
Get-WebRequest | Format-Table ProcessId, Url, TimeElapsed

高级功能

1. 负载均衡

<!-- applicationHost.config配置 -->
<webFarms>
  <webFarm name="Farm1" enabled="true">
    <server address="192.168.1.10" enabled="true" />
    <server address="192.168.1.11" enabled="true" />
  </webFarm>
</webFarms>

2. URL 重写

<rule name="Redirect HTTPS" stopProcessing="true">
  <match url="(.*)" />
  <conditions>
    <add input="{HTTPS}" pattern="^OFF$" />
  </conditions>
  <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" />
</rule>

3. 应用程序初始化

# 预加载站点(避免首次访问延迟)
Set-WebConfigurationProperty -Filter /system.applicationHost/applicationPools/applicationPoolDefaults -Name startMode -Value "AlwaysRunning"

典型运维场景

解决内存泄漏

  1. 在应用池中启用private Memory Limit(如1024MB)
  2. 配置自动回收
Set-ItemProperty "IIS:\AppPools\DefaultAppPool" -Name Recycling.periodicRestart.privateMemory -Value 1024000
  1. 使用Procdump抓取W3wp 进程内存Dump
procdump -ma w3wp.exe

其他常见故障排查

一、HTTP 5xx 服务器错误

1. HTTP 503 Service Unavailable
  • 典型现象

    • 应用池自动停止,事件日志中出现WAS 5002警告
    • 浏览器显示"Service Unavailable"
  • 根因

# 常见触发条件
1. 应用程序池崩溃(内存泄漏/未处理异常)
2. 进程模型配置错误(如标识权限不足)
3. 达到回收条件(内存/CPU超限)
  • 解决方案
# 1. 检查崩溃原因
Get-EventLog -LogName Application -Source "Windows Error Reporting" -After (Get-Date).AddHours(-1)

# 2. 调整回收设置(示例:限制内存为2GB)
Set-ItemProperty "IIS:\AppPools\DefaultAppPool" -Name Recycling.periodicRestart.privateMemory -Value 2048000

# 3. 启用故障转储
c:\Windows\System32\inetsrv\appcmd set apppool "DefaultAppPool" /failure.loadBalancerCapabilities:Always
. HTTP 500.19 Internal Server Error
  • 关键特征

    • 错误页面显示"Configuration Error"
    • 详细错误中包含configSourcelockAttributes相关提示
  • 排查步骤

    1. 检查**Web.config**格式:
# 验证XML语法
[xml](Get-Content .\Web.config) | Out-Null
    1. 解决配置继承冲突:
<!-- 示例:解除锁定 -->
<location path="." allowOverride="true">
  <system.webServer>
    <modules>
      <clear />
    </modules>
  </system.webServer>
</location>

二、HTTP 4xx 客户端错误

HTTP 403.14 Forbidden
  • 触发场景

    • 访问目录时显示"Directory listing is denied"
    • 默认文档(如index.html)未配置或丢失
  • 修复方案

# 1. 添加默认文档
Add-WebConfigurationProperty -Filter "/system.webServer/defaultDocument/files" -Name "." -Value @{value="index.html"}

# 2. 或启用目录浏览(测试环境)
Set-WebConfigurationProperty -Filter /system.webServer/directoryBrowse -Name enabled -Value $true
HTTP 401 Unauthorized
  • 认证类型判断
子错误码 认证类型 解决方案
401.1 Windows认证失败 检查IIS_IUSRS权限
401.2 URL授权失败 检查<authorization>规则
401.3 ACL权限不足 运行icacls命令重置权限
# 权限修复示例
icacls C:\inetpub\wwwroot /grant "IIS_IUSRS:(OI)(CI)(RX)"

三、性能类异常

高CPU占用(w3wp.exe)
  • 诊断工具链
    1. 实时监控
# 1. 使用PerfMon监控
typeperf "\Process(w3wp)\% Processor Time" -si 5

# 2. 抓取内存转储
procdump -ma -c 90 -n 3 w3wp.exe
    1. 分析工具
  • DebugDiag:分析内存转储中的线程堆栈
  • PerfView:定位热点代码路径
连接池耗尽
  • 症状

    • 日志中出现Timeout expired. The timeout period elapsed...
    • 应用响应变慢甚至超时
  • 调优方案

<!-- Machine.config调整 -->
<system.web>
  <processModel autoConfig="false" maxWorkerThreads="100" maxIoThreads="100" />
  <httpRuntime minFreeThreads="50" minLocalRequestFreeThreads="10" />
</system.web>
证书相关错误
  • 常见问题

    • ERR_CERT_COMMON_NAME_INVALID:证书绑定不匹配
    • SCHANNEL_ERROR:协议不兼容
  • 修复命令

# 1. 检查当前绑定
Get-ChildItem IIS:\SslBindings

# 2. 重新绑定证书
New-WebBinding -Name "Default Web Site" -Protocol "https" -Port 443 -IPAddress "*"
$cert = Get-Item Cert:\LocalMachine\My\<thumbprint>
New-Item IIS:\SslBindings\0.0.0.0!443! -Value $cert
请求过滤拦截
  • 日志特征

    • 日志字段sc-filter-result值为BlockedByRequestFiltering
  • 解除限制

<system.webServer>
  <security>
    <requestFiltering>
      <requestLimits maxAllowedContentLength="1073741824" /> <!-- 1GB -->
      <fileExtensions allowUnlisted="true" />
    </requestFiltering>
  </security>
</system.webServer>
WebSocket连接失败

排查要点

  1. 确认Windows Feature已启用
Get-WindowsFeature Web-WebSockets | Where InstallState -eq "Installed"
  1. 检查web.config配置:
<system.webServer>
  <webSocket enabled="true" receiveBufferLimit="4194304" />
</system.webServer>
ARR反向代理故障
  • 典型错误

    • 502.3 - Bad Gateway(后端连接超时)
  • 调优参数

# 调整代理超时(默认120秒)
Set-WebConfigurationProperty -Filter /system.webServer/proxy -Name timeout -Value "00:05:00"

排查工具箱

工具 适用场景 示例命令
FailedReqLogFiles 请求追踪 C:\Windows\System32\inetsrv\config\schema\IIS_schema.xml
LogParser 日志统计分析 LogParser.exe "SELECT * FROM ex*.log" -i:IISW3C
Http.sys事件日志 内核级问题 Get-WinEvent -ProviderName "HTTP Service"

网站公告

今日签到

点亮在社区的每一天
去签到