在实际运维和架构优化中,Web 应用防火墙(WAF)是保护 Web 应用免受常见攻击(如 SQL 注入、XSS、文件包含等)的重要组件。A5数据本教程基于 Ubuntu 22.04 LTS,在香港云服务器环境下,从硬件规格、模块依赖、编译安装到规则配置、性能评估全面展开,帮助你构建一个可用于生产的 Nginx+ModSecurity WAF 解决方案。
我在部署过程中选择手工编译 ModSecurity v3 及 Nginx ModSecurity 动态模块,以便精细调优和升级兼容性更强。本方案明确区分编译依赖、模块加载、规则集集成、日志输出格式和性能评估,是面向运维工程师和安全工程师的深度指导。
一、部署环境与硬件规格
首先说明测试环境和硬件配置,这有助于后续性能评估与容量规划。
1.1 香港服务器www.a5idc.com硬件配置对比表
| CPU 型号 | Intel Xeon E5-2640 v4 | Intel Xeon Silver 4216 |
| 核心/线程 | 10 核 / 20 线程 | 16 核 / 32 线程 |
| 内存 | 32 GB DDR4 | 64 GB DDR4 |
| 网络 | 1 Gbps BGP | 1–5 Gbps BGP/CN2 多线 |
| 存储 | 2 × 500 GB NVMe RAID1 | 2 × 1 TB NVMe RAID1/RAID10 |
| 操作系统 | Ubuntu 22.04 LTS | Ubuntu 22.04 LTS |
说明:表中测试机配置 A 是我在实验环境用于初步验证的配置。生产机建议配置 B 用于高并发防护场景。对于高流量站点,网络建议采用 BGP+CN2 混合出口以提升国内访问稳定性。
二、系统准备与前置依赖
2.1 更新系统及安装基础工具
在 Ubuntu 22.04 上执行:
sudo apt update
sudo apt upgrade -y
sudo apt install -y build-essential git wget unzip \\
libpcre3 libpcre3-dev zlib1g zlib1g-dev libssl-dev \\
libxml2 libxml2-dev libyajl-dev
说明:libyajl-dev 是 ModSecurity v3 的 JSON 解析依赖,libpcre3-dev、zlib1g-dev、libssl-dev 则是 Nginx 编译依赖。
三、安装 ModSecurity v3
3.1 获取代码
cd /usr/local/src
git clone –depth 1 https://github.com/SpiderLabs/ModSecurity
cd ModSecurity
git submodule init
git submodule update
3.2 编译和安装
./build.sh
./configure
make -j$(nproc)
sudo make install
安装后,ModSecurity 库通常会放在 /usr/local/modsecurity。
检查安装:
/usr/local/modsecurity/bin/modsec-util –version
四、安装 Nginx 与 ModSecurity 动态模块
4.1 获取 Nginx 源码
cd /usr/local/src
wget http://nginx.org/download/nginx-1.24.0.tar.gz
tar zxvf nginx-1.24.0.tar.gz
4.2 获取 Nginx ModSecurity Connector
git clone –depth 1 https://github.com/SpiderLabs/ModSecurity-nginx.git
4.3 编译 Nginx(含 ModSecurity 模块)
cd nginx-1.24.0
./configure \\
–with-compat \\
–add-dynamic-module=../ModSecurity-nginx \\
–with-http_ssl_module \\
–with-http_v2_module
make -j$(nproc)
sudo make install
说明:–with-compat 允许生成通用动态模块。
4.4 配置动态模块加载
在 Nginx 全局配置 /etc/nginx/nginx.conf 第一行加入:
load_module modules/ngx_http_modsecurity_module.so;
五、集成 OWASP Core Rule Set(CRS)
OWASP CRS 是常用的规则集,建议结合自定义规则以兼顾安全和误报控制。
5.1 下载 Rule Set
cd /etc/nginx
sudo git clone https://github.com/coreruleset/coreruleset.git
cd coreruleset
sudo mv coreruleset crs-3.4.4
5.2 基本配置
在 Nginx 的 modsecurity.conf 中包含规则:
cp /usr/local/modsecurity/etc/modsecurity.conf-recommended /usr/local/modsecurity/etc/modsecurity.conf
编辑 modsecurity.conf:
SecRuleEngine On
SecRequestBodyAccess On
SecResponseBodyAccess Off
Include /etc/nginx/coreruleset/crs-3.4.4/crs-setup.conf
Include /etc/nginx/coreruleset/crs-3.4.4/rules/*.conf
六、Nginx 中激活 WAF 配置
6.1 Nginx 站点配置示例
在站点配置 /etc/nginx/conf.d/my_site.conf 中:
server {
listen 80;
server_name example.com;
modsecurity on;
modsecurity_rules_file /usr/local/modsecurity/etc/modsecurity.conf;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
说明:
- modsecurity on; 启用防护。
- 所有规则由 modsecurity.conf 集中管理。
七、测试与验证
7.1 基本规则测试
使用 curl 测试 SQL 注入模式阻断:
curl -s "http://example.com/?id=1' OR '1'='1" -o /tmp/test.log
检查 Nginx 响应代码和日志:
tail -n 100 /usr/local/nginx/logs/error.log | grep ModSecurity
八、日志格式与告警设计
8.1 JSON 格式日志
在 modsecurity.conf 末尾启用:
SecAuditLogType Serial
SecAuditLogFormat JSON
SecAuditLog /var/log/modsecurity/audit.log
8.2 日志切割与监控集成
建议配合 Filebeat/Fluentd 推送到 ELK/OSSEC/云日志平台,方便态势分析。
九、性能测试与评估
9.1 性能对比测试(wrk 10 万并发)
| 普通 Nginx | 15000 | 20 | 30% |
| Nginx + ModSecurity | 9000 | 35 | 55% |
测试参数:wrk -t8 -c10000 -d60s http://example.com/; QPS 和延迟因规则集数量和复杂度有所差异。
如上表所示,启用 WAF 会带来一定性能损耗,但在合理硬件配置下完全可控。
十、误报控制与规则调优
10.1 误报示例和处理
当某些正常请求被拦截,可使用:
SecRuleRemoveById 981176
加入自定义规则黑/白名单:
SecRule REMOTE_ADDR "@ipMatch 203.0.113.0/24" "id:1001,phase:1,ctl:ruleEngine=Off"
十一、总结与建议
通过A5数据以上步骤,我构建了一个稳定且具备实用防护能力的 Nginx+ModSecurity WAF 体系。该方案强调:
- 使用最新 ModSecurity v3 和动态模块,兼容性好。
- 结合 OWASP CRS 减少重复规则维护。
- 日志结构化输出便于安全监控平台集成。
- 性能测试结果可量化评估 WAF 引入的开销。
A5数据建议生产环境逐步启用规则,通过灰度测试、白名单机制不断调整,以达到“安全与可用兼顾”的最佳状态。
网硕互联帮助中心






评论前必须登录!
注册