新闻
我们更期待的是,能在与您的沟通交流中获得启迪,
因为这是我们一起经历的时代。
分类
相关文章
热门标签

阿里云服务器waf自己部署时常见问题与故障排查指南

2026年10月23日

1.

准备工作与环境说明

说明部署前的基础要求:阿里云ECS一台(建议2核4G以上)、已开通安全组80/443/22端口、系统建议CentOS 7/8或Ubuntu 18.04+。
小分段:a) 登录ECS并更新系统(CentOS: yum update -y;Ubuntu: apt update && apt upgrade -y)。 b) 安装编译工具:yum groupinstall "Development Tools" -y或apt install build-essential -y。

2.

安装依赖与获取源码

安装依赖:CentOS示例:yum install git gcc make automake libtool pcre-devel openssl-devel zlib-devel yajl-devel -y。
小分段:a) 克隆ModSecurity v3:git clone --depth 1 https://github.com/SpiderLabs/ModSecurity && cd ModSecurity && git submodule init && git submodule update。 b) 编译安装:./build.sh && ./configure && make && make install。

3.

编译libmodsecurity与nginx连接器

下载并编译libmodsecurity后编译nginx connector(ModSecurity-nginx)。
小分段:a) 编译完成后,克隆 nginx-modules: git clone https://github.com/SpiderLabs/ModSecurity-nginx.git。 b) 如果用源码编译Nginx,需要在./configure时添加 --add-module=/path/to/ModSecurity-nginx,然后make && make install;若使用发行版包,可采用动态模块或使用已有第三方包。

4.

部署OWASP CRS规则集

建议使用OWASP CRS作为默认规则:git clone https://github.com/coreruleset/coreruleset /etc/modsecurity/crs。
小分段:a) 复制crs-setup.conf.example为crs-setup.conf并根据需要修改白名单。 b) 将rules目录下的规则在主配置中包含:Include /etc/modsecurity/crs/crs-setup.conf && Include /etc/modsecurity/crs/rules/*.conf。

5.

配置Nginx启用ModSecurity

在nginx.conf的http或server段加载modsecurity并指定规则文件。
小分段:示例配置片段:modsecurity on; modsecurity_rules_file /etc/nginx/modsec/main.conf;。保存后执行 nginx -t && systemctl reload nginx。

6.

日志与审计配置

配置ModSecurity审计日志和错误日志路径,便于排查。
小分段:在modsecurity.conf中设置 SecAuditEngine RelevantOnly 或 On、SecAuditLog /var/log/modsec_audit.log、SecDebugLog /var/log/modsec_debug.log(调试时使用,平时关闭)。

7.

测试是否生效

使用curl模拟攻击与规则触发测试:curl -H "User-Agent: sqlmap" https://yourdomain 。
小分段:a) 观察返回码和页面是否被WAF阻断。 b) tail -f /var/log/modsec_audit.log 查看触发日志。

8.

规则调优与白名单

根据业务请求日志调整误报:将误报请求的规则ID加入例外。
小分段:在自定义规则文件中添加: SecRuleRemoveById 981176(示例ID),或使用SecRule REMOTE_ADDR来放行特定IP。

9.

常见故障:部署后Nginx 502/503

原因与排查:通常是nginx与modsecurity模块不兼容或编译时缺少模块。
小分段:检查nginx error log(/var/log/nginx/error.log);确认nginx是带modsecurity模块或动态模块已加载;若不兼容,重新编译nginx并加入modsecurity connector。

云WAF

10.

常见故障:大量误报/业务中断

应急措施与解决:将SecRuleEngine设为 DetectionOnly,快速恢复业务。
小分段:编辑modsecurity.conf将 SecRuleEngine Off 或 DetectionOnly,然后 reload nginx;同时导出误报样例以便后续精细化规则调整。

11.

性能问题:CPU/内存高

排查与优化:规则过多或Debug级别过高会导致性能下降。
小分段:a) 关闭SecDebugLog或降低SecDebugLogLevel。 b) 精简启用的规则集,使用SecRuleEngine在流量高峰期使用轻量级策略。

12.

网络层与安全组检查

确认阿里云安全组与本机防火墙允许必要端口并无IP冲突。
小分段:使用iptables -L或firewalld查看规则;在阿里云控制台确认安全组入站/出站规则允许80/443/22。

13.

备份与版本管理

保存配置和规则快照以便回滚。
小分段:定期备份 /etc/nginx、/etc/modsecurity、/var/log/modsec* 到 OSS 或远端仓库,记录版本与变更说明。

14.

升级流程建议

先在测试环境升级规则/模块,再到线上平滑发布。
小分段:使用蓝绿或灰度发布,先将部分流量导向新WAF实例验证一周无误再全量切换。

15.

常见排查命令一览

便捷命令帮助快速定位问题。
小分段:a) nginx -t && systemctl reload nginx b) tail -f /var/log/nginx/error.log /var/log/modsec_audit.log c) ss -tlnp | grep nginx d) ps aux | grep ModSecurity。

16.

维护建议与监控

接入云监控并设置告警,监控QPS、错误率、CPU、内存与modsec触发率。
小分段:阿里云监控(CloudMonitor)可自定义告警策略并发送短信/邮件。

17.

问:如果启用后页面出现500或502应该先做什么?

答:先查看 /var/log/nginx/error.log 与 /var/log/modsec_debug.log(如开启),把 SecRuleEngine 临时设为 DetectionOnly 或 Off,nginx -t 检查配置语法并重新加载,确认是否为模块兼容或规则错误引起。

18.

问:如何快速定位误报并减少业务影响?

答:开启审计日志收集示例请求ID,使用 SecRuleRemoveById 针对问题规则快速屏蔽,同时将触发样例存档,在测试环境精细化修改规则或编写白名单。

19.

问:部署modsecurity后如何做升级与回滚?

答:先在测试环境使用相同数据验证新版本/规则,备份当前 /etc/nginx 与 /etc/modsecurity 配置与日志,升级时采用灰度流量策略;如出现问题,按备份直接回滚并 reload nginx。