Home · Guides / 5 / Secure / 5.1
Guide 5.1
Install ModSecurity on NGINX
ModSecurity is an open source web application firewall. On this stack it sits in front of the NGINX site that Tor already forwards to 127.0.0.1:80, so it can still inspect requests that arrive over the onion address.
Install the module
-
Install ModSecurity 3 and the NGINX connector from APT (names vary slightly by Ubuntu release; search if yours differs).
sudo apt update sudo apt install libmodsecurity3 sudo apt install libnginx-mod-http-modsecurityIf the module package is missing, install
modsecurity-crswhen offered, or follow Geekflare’s source-compile method for older Nginx. -
Confirm NGINX loaded the module.
nginx -V 2>&1 | tr ' ' '\n' | grep -i modsecurity sudo nginx -t
Enable it on the onion site
-
Create a small rules include. Copy the recommended config if the package shipped one:
sudo mkdir -p /etc/nginx/modsec sudo cp /etc/modsecurity/modsecurity.conf-recommended /etc/nginx/modsec/modsecurity.conf sudo cp /usr/share/modsecurity-crs/unicode.mapping /etc/nginx/modsec/unicode.mapping 2>/dev/null || trueIf those paths do not exist on your release, copy
modsecurity.conf-recommendedandunicode.mappingfrom the ModSecurity tree, as Geekflare describes. -
Start in detection-only mode (log, do not block) until you know the ruleset is sane. Confirm the engine line:
grep SecRuleEngine /etc/nginx/modsec/modsecurity.confThe recommended file already uses
DetectionOnly. Leave it that way for the first run. Change it toOnonly after you have reviewed the audit log. -
Add a main include file:
sudo tee /etc/nginx/modsec/main.conf > /dev/null << 'EOF' Include /etc/nginx/modsec/modsecurity.conf EOF -
In
/etc/nginx/sites-enabled/default, inside theserverblock that listens on 127.0.0.1, turn the engine on:modsecurity on; modsecurity_rules_file /etc/nginx/modsec/main.conf;ModSecurity 2 with a source-built Nginx (Geekflare’s path) used
ModSecurityEnabled on;andModSecurityConfig modsecurity.conf;instead. Use the pair that matches the module you compiled or installed. -
Test and reload.
sudo nginx -t sudo service nginx reload
Optional: OWASP Core Rule Set
The engine alone does little until you load rules. OWASP CRS is the usual starting set.
-
Install the packaged CRS if available, otherwise clone it:
sudo apt install modsecurity-crsor
sudo git clone https://github.com/coreruleset/coreruleset.git /etc/nginx/modsec/coreruleset sudo cp /etc/nginx/modsec/coreruleset/crs-setup.conf.example /etc/nginx/modsec/coreruleset/crs-setup.conf -
Point
main.confat the setup file and the rules directory. Package layouts differ; adjust paths afterdpkg -L modsecurity-crs.Include /etc/nginx/modsec/modsecurity.conf Include /etc/nginx/modsec/coreruleset/crs-setup.conf Include /etc/nginx/modsec/coreruleset/rules/*.conf -
Reload NGINX again. Watch
/var/log/nginx/error.logand the ModSecurity audit log (often/var/log/modsec_audit.logor the path inSecAuditLog). -
When the logs look clean for real traffic, switch blocking on:
SecRuleEngine OnThen
sudo nginx -tand reload. Keep detection-only if a CMS or plugin is flooded with false positives.