作为IT工程师,我将为您详细介绍在Windows Server环境下保护Nginx Web服务器的安全措施。
# 设置Nginx目录权限(管理员PowerShell)
icacls "C:\nginx" /grant "NT AUTHORITY\SYSTEM:(OI)(CI)(F)"
icacls "C:\nginx" /grant "BUILTIN\Administrators:(OI)(CI)(F)"
icacls "C:\nginx" /grant "IIS_IUSRS:(OI)(CI)(RX)"
icacls "C:\nginx" /remove "Everyone"
# 隐藏Nginx版本信息
server_tokens off;
# 限制HTTP方法
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 405;
}
# 禁用自动目录列表
autoindex off;
# 限制请求大小
client_max_body_size 1m;
client_body_buffer_size 1k;
client_header_buffer_size 1k;
large_client_header_buffers 2 1k;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305';
ssl_ecdh_curve secp384r1;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_session_tickets off;
ssl_stapling on;
ssl_stapling_verify on;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
add_header Referrer-Policy "no-referrer-when-downgrade";
add_header Content-Security-Policy "default-src 'self' http: https: data: blob: 'unsafe-inline'" always;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
# 限制IP访问
location /admin {
allow 192.168.1.0/24;
deny all;
}
# 速率限制
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
limit_req zone=one burst=5 nodelay;
# 详细日志记录
log_format security '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'$request_time $upstream_response_time '
'$http_x_forwarded_for';
access_log logs/access.log security;
error_log logs/error.log warn;
# 允许HTTP/HTTPS流量(管理员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
# 创建Nginx配置备份脚本
$backupDir = "C:\nginx_backups\$(Get-Date -Format 'yyyyMMdd')"
New-Item -ItemType Directory -Path $backupDir -Force
Copy-Item -Path "C:\nginx\conf\*" -Destination $backupDir -Recurse
通过实施这些措施,您可以显著提高Windows Server上Nginx Web服务器的安全性。根据您的具体环境和需求,可能需要进一步调整这些配置。