Include /etc/nginx/modsec/modsecurity.conf
Include /etc/nginx/modsec/crs/crs-setup.conf
Include /etc/nginx/modsec/crs/rules/*.conf
EOF
4. 调整配置(按需)
编辑 /etc/nginx/modsec/modsecurity.conf:
ini
SecRuleEngine On # On=拦截,DetectionOnly=仅检测
SecAuditLog /var/log/nginx/modsec_audit.log
SecAuditLogParts ABIJDEFHZ
5. 重启与验证
bash
运行
nginx -t # 检查配置语法
systemctl restart nginx
# 测试拦截
curl -I "http://你的域名/?id=1" OR 1=1--"
# 应返回 HTTP/1.1 403 Forbidden
三、Apache 手动启用 ModSecurity + OWASP CRS
1. 安装模块
bash
运行
# Ubuntu/Debian
apt update && apt install -y libapache2-mod-security2
a2enmod security2
# CentOS/RHEL
yum install -y mod_security mod_security_crs httpd
2. 配置规则
bash
运行
cp /etc/modsecurity/modsecurity.conf-recommended /etc/modsecurity/modsecurity.conf
sed -i "s/SecRuleEngine DetectionOnly/SecRuleEngine On/" /etc/modsecurity/modsecurity.conf
3. 启用并重启
bash
运行
# Ubuntu/Debian
systemctl restart apache2
# CentOS/RHEL
systemctl restart httpd
四、关键配置与验证
表格
项目 操作 路径 / 命令
日志路径 查看拦截日志 宝塔:/www/wwwlogs/waf/;Nginx/Apache:/var/log/nginx/modsec_audit.log / /var/log/modsec_audit.log
误杀处理 白名单 / 排除 宝塔 WAF 规则组排除;ModSecurity 用 SecRuleRemoveById
安全组 放行必要端口 80、443、自定义 SSH 端口
测试 触发拦截 curl -I "http://域名/?xss=alert(1)"
五、常见问题与加固
开启后站点异常:先设 SecRuleEngine DetectionOnly,查日志定位误报并添加排除规则。
性能优化:启用缓存、调整规则级别,优先放行业务白名单。
定期更新规则与系统补丁,配合 Fail2ban 防御暴力破解。
三二互联专业提供香港VPS,美国VPS主机,香港云服务器租用等业务香港美国到大陆CN2 GIA速度最快