一、系统准备与环境配置
1. 系统要求检查
-
硬件要求:
-
最低配置:2核CPU/4GB内存/50GB存储
-
生产环境推荐:4核CPU/8GB内存/100GB SSD
-
-
操作系统版本:
-
Windows Server 2019/2022(长期服务版)
-
Windows 10/11 Pro(开发测试环境)
-
2. 基础环境配置
# 启用远程管理 Enable-PSRemoting -Force # 关闭IE增强安全配置 Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Active Setup\Installed Components\{A509B1A7-37EF-4b3f-8CFC-4F3A74704073}" -Name "IsInstalled" -Value 0 # 设置高性能电源计划 powercfg /setactive 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c
3. 网络配置优化
-
禁用TCP/IPv6(如不需要)
-
调整TCP参数:
# 增大TCP窗口大小 Set-NetTCPSetting -SettingName InternetCustom -AutoTuningLevelLocal Restricted
二、IIS服务器安装与配置
1. IIS安装
# 通过PowerShell安装IIS Install-WindowsFeature -Name Web-Server -IncludeManagementTools -IncludeAllSubFeature
2. 核心组件选择
组件名称 | 生产环境 | 测试环境 |
---|---|---|
HTTP重定向 | ✓ | ✓ |
静态内容压缩 | ✓ | ✗ |
动态内容压缩 | ✓ | ✗ |
应用程序初始化 | ✓ | ✗ |
ASP.NET 4.8 | ✓ | ✓ |
CGI | 按需 | ✗ |
3. 基础配置
# 设置应用程序池 Import-Module WebAdministration New-WebAppPool -Name "MyAppPool" -Force Set-ItemProperty "IIS:\AppPools\MyAppPool" -Name managedRuntimeVersion -Value "v4.0" Set-ItemProperty "IIS:\AppPools\MyAppPool" -Name processModel.idleTimeout -Value "00:00:00"
三、网站部署实战
1. 网站创建
# 新建网站 New-Website -Name "MySite" -Port 80 -PhysicalPath "C:\sites\mysite" -ApplicationPool "MyAppPool"
2. 高级配置项
-
URL重写规则:
<!-- web.config示例 --> <rewrite> <rules> <rule name="Redirect to HTTPS" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTPS}" pattern="^OFF$" /> </conditions> <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" /> </rule> </rules> </rewrite>
-
MIME类型添加:
New-WebHandler -Name "*.webmanifest" -Path "*.webmanifest" -Verb GET -Type System.Web.StaticFileHandler -Precondition integratedMode
3. SSL证书配置
# 导入PFX证书 Import-PfxCertificate -FilePath "C:\certs\mysite.pfx" -CertStoreLocation Cert:\LocalMachine\My -Password (ConvertTo-SecureString -String "yourpassword" -Force -AsPlainText) # 绑定到网站 New-WebBinding -Name "MySite" -Protocol "https" -Port 443 -SslFlags 1 -HostHeader "www.yourdomain.com"
四、性能优化配置
1. 静态内容缓存
<!-- 在web.config中添加 --> <staticContent> <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" /> </staticContent>
2. 动态压缩配置
# 启用动态压缩 Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter 'system.webServer/httpCompression' -name 'dynamicCompression' -value 'True' # 设置压缩级别 Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter 'system.webServer/httpCompression/dynamicTypes' -name '.' -value @{mimeType='text/*';enabled='True'}
3. 连接限制调整
# 修改IIS全局设置 Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter 'system.applicationHost/applicationPools' -name 'processModel' -value @{idleTimeout='00:00:00';maxProcesses='1'}
五、安全加固措施
1. 权限配置
# 设置网站目录权限 icacls "C:\sites\mysite" /grant "IIS AppPool\MyAppPool":(OI)(CI)(RX)
2. 请求过滤
<!-- 在web.config中添加 --> <security> <requestFiltering> <requestLimits maxAllowedContentLength="30000000" /> <fileExtensions allowUnlisted="false"> <add fileExtension=".asp" allowed="false" /> </fileExtensions> </requestFiltering> </security>
3. 日志审计
# 配置W3C日志格式 Set-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter 'system.applicationHost/sites/siteDefaults' -name 'logFile' -value @{directory='C:\logs';enabled='true';logExtFileFlags='Date,Time,ClientIP,UserName,SiteName,ServerIP,Method,UriStem,UriQuery,HttpStatus,Win32Status,TimeTaken,ServerPort,UserAgent,Referer,Host,HttpSubStatus'}
六、高可用配置
1. 负载均衡设置
# 安装ARR模块 Install-WindowsFeature Web-ARR # 配置服务器组 Add-WebConfiguration -PSPath IIS:\ -Filter "system.webServer/webFarms" -Value @{name="MyFarm";enabled="true"} Add-WebConfigurationProperty -PSPath IIS:\ -Filter "system.webServer/webFarms/webFarm[@name='MyFarm']" -Name "server" -Value @{address="192.168.1.10";enabled="true"}
2. 数据库连接配置
# 创建SQL连接字符串 Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter 'connectionStrings' -name '.' -value @{name="MyDB";connectionString="Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;"}
七、维护与监控
1. 备份策略
# 创建IIS配置备份 $backupName = "IISConfig_$(Get-Date -Format 'yyyyMMdd')" Backup-WebConfiguration -Name $backupName
2. 性能监控
# 安装性能计数器 New-Counter -CounterName "\Web Service(_Total)\Current Connections" -CategoryName "Web Service"
3. 自动维护脚本
# 日志清理脚本 $limit = (Get-Date).AddDays(-30) Get-ChildItem "C:\logs\" -Recurse -File | Where-Object { $_.LastWriteTime -lt $limit } | Remove-Item
常见问题解决方案
-
HTTP 500.19错误:
-
检查应用程序池标识权限
-
验证web.config格式是否正确
-
-
静态文件无法访问:
-
确认StaticFile处理程序已启用
-
检查文件系统权限
-
-
高CPU使用率:
-
使用Process Explorer分析具体进程
-
检查是否有恶意请求
-
-
内存泄漏:
-
配置应用程序池回收条件
-
使用DebugDiag工具分析内存dump
-
最佳实践建议
-
开发环境与生产环境一致性:
-
使用Docker容器部署IIS
-
通过DSC(Desired State Configuration)管理配置
-
-
CI/CD集成:
# Azure DevOps示例 - task: IISWebAppManagementOnMachineGroup@0 inputs: IISDeploymentType: 'IISWebsite' ActionIISWebsite: 'CreateOrUpdateWebsite' WebsiteName: 'MySite' WebsitePhysicalPath: 'C:\sites\mysite'
-
灾难恢复:
-
定期测试系统还原
-
维护详细的配置文档
-
通过以上步骤,您可以在Windows系统上搭建一个高性能、安全的网站服务器环境。建议在正式上线前进行全面的负载测试和安全扫描,确保系统稳定可靠。