1. Nginx在Windows环境下的安装准备
作为一名长期从事Web服务部署的运维工程师,我经常需要在Windows服务器上部署Nginx。与Linux环境不同,Windows下的Nginx安装有其特殊性,下面我将分享完整的实战经验。
1.1 系统环境要求检查
在开始安装前,首先要确认系统环境是否符合要求:
- 操作系统版本:Windows Server 2012 R2及以上版本(实测在Win10 1809后版本也能稳定运行)
- 磁盘空间:至少100MB可用空间(建议预留500MB用于日志存储)
- 内存:建议1GB以上(实际运行占用约20MB基础内存)
- 网络配置:确保80/443端口未被IIS或其他服务占用
提示:可通过
netstat -ano | findstr :80命令检查端口占用情况,如果发现占用,需要先停止相关服务。
1.2 安装包获取与验证
官方提供了两个版本的Windows安装包:
- Mainline版本:包含最新功能,但稳定性稍逊
- Stable版本:经过充分测试的生产环境推荐版本
下载步骤:
- 访问Nginx官网下载页
- 选择
nginx/Windows-x.x.x格式的zip包(x.x.x为版本号) - 下载完成后务必校验文件哈希值:
bash复制
certutil -hashfile nginx-x.x.x.zip SHA256
1.3 安装目录规划建议
不同于Linux的标准化路径,Windows下的安装位置需要合理规划。我推荐的结构:
code复制D:\WebServices\
├── nginx-1.25.3\ # 主程序目录
│ ├── conf\ # 配置文件
│ ├── logs\ # 日志文件
│ └── html\ # 网站根目录
└── nginx-data\ # 共享数据目录
├── cache\ # 代理缓存
└── temp\ # 临时文件
这种结构便于多版本管理和数据隔离,特别是当需要运行多个Nginx实例时。
2. Nginx安装与基础配置
2.1 解压安装详细步骤
-
创建目标目录(以D盘为例):
powershell复制mkdir D:\WebServices cd D:\WebServices -
解压安装包(假设下载到C:\Downloads):
powershell复制Expand-Archive -Path C:\Downloads\nginx-x.x.x.zip -DestinationPath . -
重命名目录为带版本号的形式:
powershell复制Rename-Item nginx-x.x.x nginx-1.25.3 -
创建符号链接方便管理:
powershell复制cmd /c mklink /D D:\WebServices\nginx nginx-1.25.3
2.2 初始配置调整
修改conf/nginx.conf基础配置:
nginx复制worker_processes 2; # 设置为CPU核心数
events {
worker_connections 1024; # 每个worker最大连接数
accept_mutex on; # 启用连接互斥
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on;
keepalive_timeout 65;
access_log logs/access.log combined buffer=32k flush=5s;
error_log logs/error.log warn;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
}
}
关键参数说明:
worker_processes:建议等于CPU物理核心数worker_connections:理论最大连接数=worker_processes × worker_connectionstcp_nopush:与sendfile配合提升网络效率
2.3 防火墙配置
允许Nginx通过防火墙:
powershell复制New-NetFirewallRule -DisplayName "Nginx HTTP" -Direction Inbound -Protocol TCP -LocalPort 80 -Action Allow
New-NetFirewallRule -DisplayName "Nginx HTTPS" -Direction Inbound -Protocol TCP -LocalPort 443 -Action Allow
3. Windows服务化管理
3.1 注册为系统服务
使用第三方工具winsw将Nginx注册为服务:
-
下载winsw
-
创建
nginx-service.xml配置文件:xml复制<service> <id>nginx</id> <name>Nginx Web Server</name> <description>High Performance Web Server</description> <executable>D:\WebServices\nginx\nginx.exe</executable> <logpath>D:\WebServices\nginx\logs</logpath> <logmode>rotate</logmode> <startargument>-p D:\WebServices\nginx</startargument> </service> -
安装服务:
powershell复制.\winsw.exe install nginx-service.xml
3.2 服务管理命令
- 启动服务:
Start-Service nginx - 停止服务:
Stop-Service nginx - 重启服务:
Restart-Service nginx - 查看状态:
Get-Service nginx
3.3 开机自启动配置
确保服务设置为自动启动:
powershell复制Set-Service -Name nginx -StartupType Automatic
4. 虚拟主机配置实战
4.1 单IP多站点配置
假设需要在同一服务器上部署两个网站:
-
在
conf/nginx.conf的http块中添加:nginx复制include vhosts/*.conf; -
创建
conf/vhosts目录存放各站点配置 -
示例站点配置
site1.conf:nginx复制server { listen 80; server_name site1.example.com; access_log logs/site1.access.log main; error_log logs/site1.error.log warn; location / { root D:\Websites\site1; index index.html; } }
4.2 SSL证书配置
以Let's Encrypt证书为例:
nginx复制server {
listen 443 ssl;
server_name site1.example.com;
ssl_certificate D:\Certificates\site1\fullchain.pem;
ssl_certificate_key D:\Certificates\site1\privkey.pem;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
ssl_ciphers HIGH:!aNULL:!MD5;
ssl_prefer_server_ciphers on;
location / {
root D:\Websites\site1;
index index.html;
}
}
5. 性能优化与安全加固
5.1 性能调优参数
nginx复制http {
# 启用gzip压缩
gzip on;
gzip_min_length 1k;
gzip_types text/plain text/css application/json application/javascript text/xml;
# 静态文件缓存
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
# 代理缓存配置
proxy_cache_path D:\WebServices\nginx-data\cache levels=1:2 keys_zone=my_cache:10m inactive=60m;
}
5.2 安全防护措施
-
隐藏Nginx版本信息:
nginx复制server_tokens off; -
限制敏感目录访问:
nginx复制location ~ /\.git { deny all; } -
防止点击劫持:
nginx复制add_header X-Frame-Options "SAMEORIGIN";
6. 日常维护与监控
6.1 日志轮转配置
创建logrotate.bat脚本:
batch复制@echo off
set date=%date:~0,4%%date:~5,2%%date:~8,2%
rename D:\WebServices\nginx\logs\access.log access_%date%.log
rename D:\WebServices\nginx\logs\error.log error_%date%.log
nginx -s reopen
添加到计划任务每日执行。
6.2 基础监控实现
使用Performance Monitor添加计数器:
- Process(nginx)% Processor Time
- Web Service(Default Web Site)\Current Connections
7. 常见问题排查
7.1 端口冲突问题
错误现象:
code复制nginx: [emerg] bind() to 0.0.0.0:80 failed (10013: An attempt was made to access a socket in a way forbidden by its access permissions)
解决方案:
- 查找占用进程:
powershell复制netstat -ano | findstr :80 - 根据PID结束进程或修改Nginx监听端口
7.2 配置文件语法错误
验证配置:
powershell复制nginx -t -c D:\WebServices\nginx\conf\nginx.conf
典型错误包括:
- 缺少分号
- 括号不匹配
- 路径格式错误(应使用正斜杠/)
7.3 性能瓶颈分析
检查方向:
- 工作进程CPU占用:
tasklist /fi "imagename eq nginx.exe" - 网络连接状态:
netstat -ano | findstr nginx - 磁盘IO性能:使用Resource Monitor观察日志写入速度
8. 升级与备份策略
8.1 平滑升级步骤
-
备份当前配置:
powershell复制Compress-Archive -Path D:\WebServices\nginx\conf -DestinationPath nginx-conf-backup.zip -
下载新版本并解压到新目录(如nginx-1.25.4)
-
复制旧配置到新目录:
powershell复制Copy-Item D:\WebServices\nginx\conf\* D:\WebServices\nginx-1.25.4\conf\ -Recurse -
测试新版本配置:
powershell复制D:\WebServices\nginx-1.25.4\nginx.exe -t -
切换符号链接指向新版本
8.2 备份方案设计
建议的备份内容:
- 配置文件目录(conf/)
- 网站数据目录
- SSL证书文件
- 日志文件(至少保留30天)
自动化备份脚本示例:
powershell复制$date = Get-Date -Format "yyyyMMdd"
$backupDir = "D:\Backups\nginx\$date"
New-Item -ItemType Directory -Path $backupDir -Force
# 备份配置
Copy-Item D:\WebServices\nginx\conf $backupDir\conf -Recurse
# 备份网站数据
Compress-Archive -Path D:\Websites -DestinationPath $backupDir\websites.zip
# 上传到远程存储
aws s3 cp $backupDir s3://my-backup-bucket/nginx/$date/ --recursive