如何開啟Nginx防火墻的全局設(shè)置?
1. 確認(rèn)Nginx支持防火墻模塊
大多數(shù)場景下,Nginx通過集成第三方模塊(如ModSecurity)實現(xiàn)防火墻功能。執(zhí)行以下命令驗證模塊是否已編譯:
nginx -V 2>&1 | grep --color modsecurity
若未顯示相關(guān)模塊,需重新編譯Nginx并添加--with-http_modsecurity_module
參數(shù)。
2. 配置全局防火墻規(guī)則
2.1 加載核心規(guī)則集
在nginx.conf
的http上下文中添加以下配置:
modsecurity on;
modsecurity_rules_file /etc/nginx/modsec/main.conf;
2.2 創(chuàng)建主配置文件
在/etc/nginx/modsec/main.conf
中定義全局策略:
Include /usr/local/modsecurity/modsecurity.conf
Include /path/to/owasp-modsecurity-crs/crs-setup.conf
Include /path/to/owasp-modsecurity-crs/rules/*.conf
3. 設(shè)置運行模式與日志
在modsecurity.conf
中修改關(guān)鍵參數(shù):
SecRuleEngine On
SecAuditLog /var/log/modsec_audit.log
SecDebugLog /var/log/modsec_debug.log
4. 驗證與重載配置
執(zhí)行配置檢查后重載服務(wù):
nginx -t && nginx -s reload
5. 高級配置建議
- 設(shè)置白名單策略:通過
SecRuleRemoveById
排除誤報規(guī)則 - 限制請求體大小:
SecRequestBodyLimit 13107200
- 啟用實時監(jiān)控:配置syslog或ELK日志分析
6. 常見問題處理
6.1 性能優(yōu)化
通過設(shè)置SecRuleEngine DetectionOnly
進(jìn)行測試,逐步啟用阻斷模式
6.2 規(guī)則更新
git -C /path/to/owasp-modsecurity-crs pull
nginx -s reload