ModSecurity with the OWASP Core Rule Set on nginx (Ubuntu 26.04)
6 min read
How to install ModSecurity 3 and the current OWASP CRS 4 on nginx, start in detection mode, switch to blocking, and remove false positives without turning the firewall off. Every step tested on Ubuntu 26.04.
A web application firewall (WAF) reads each request before your application does, and refuses the ones that look like attacks: SQL injection, cross-site scripting, attempts to read /etc/passwd. ModSecurity is the engine. The OWASP Core Rule Set (CRS) is the list of rules that tells it what an attack looks like.
This guide sets both up on nginx. We ran every command on a fresh Ubuntu 26.04.1 system on 12 November 2026.
In short
- Install
libnginx-mod-http-modsecurityfrom Ubuntu. It brings ModSecurity 3.0.14. - Do not use the CRS that Ubuntu packages. It is version 3.3.8. The current release is 4.30.0; download it from the CRS project.
- Start in
DetectionOnlymode. It logs what it would block and blocks nothing. - After a week of clean logs, switch to
On. - Fix false positives with a narrow exclusion (one rule, one parameter, one path), not by turning the WAF off.
1. Install the module
sudo apt-get install nginx libnginx-mod-http-modsecurity
On Ubuntu 26.04 this installs:
| Package | Version |
|---|---|
| nginx | 1.28.3 |
| libnginx-mod-http-modsecurity (the connector) | 1.0.3 |
| libmodsecurity3t64 (the engine) | 3.0.14 |
| modsecurity-crs | 3.3.8 |
The module is enabled in /etc/nginx/modules-enabled/, and the package puts a base configuration in /etc/nginx/modsecurity.conf.
The last line of the table is the trap. modsecurity-crs 3.3.8 comes along as a dependency, but the 3.3 line is old. We will not load it.
2. Get the current Core Rule Set
sudo mkdir -p /etc/nginx/modsec && cd /etc/nginx/modsec
V=4.30.0
sudo curl -sLO https://github.com/coreruleset/coreruleset/releases/download/v$V/coreruleset-$V-minimal.tar.gz
sha256sum coreruleset-$V-minimal.tar.gz
3d678a41fd5aade34760127fef5dd64fd7a77848913fc0f70dde0cf467c94427 coreruleset-4.30.0-minimal.tar.gz
Compare the checksum with the one on the release page, then unpack:
sudo tar -xzf coreruleset-$V-minimal.tar.gz
sudo cp coreruleset-$V/crs-setup.conf.example crs-setup.conf
sudo cp /etc/nginx/modsecurity.conf modsecurity.conf
sudo cp /etc/nginx/unicode.mapping .
Now make one file that loads everything in the right order, /etc/nginx/modsec/main.conf:
Include /etc/nginx/modsec/modsecurity.conf
Include /etc/nginx/modsec/crs-setup.conf
Include /etc/nginx/modsec/coreruleset-4.30.0/rules/*.conf
3. Turn it on for a site
In the site's server block:
server {
server_name example.test;
root /var/www/html;
modsecurity on;
modsecurity_rules_file /etc/nginx/modsec/main.conf;
}
sudo nginx -t && sudo systemctl reload nginx
nginx tells you how many rules it loaded:
ModSecurity-nginx v1.0.3 (rules loaded inline/local/remote: 0/852/0)
4. Detection mode first
The base configuration starts with:
SecRuleEngine DetectionOnly
In this mode nothing is blocked. Send a test attack:
curl -s -o /dev/null -w '%{http_code}\n' "http://example.test/?id=1%27%20OR%20%271%27=%271"
200
The request passes, but /var/log/nginx/modsec_audit.log records what the rules saw:
ModSecurity: Warning. detected SQLi using libinjection. [file ".../REQUEST-942-APPLICATION-ATTACK-SQLI.conf"] [id "942100"] [msg "SQL Injection Attack Detected via libinjection"] [data "Matched Data: s&sos found within ARGS:id: 1' OR '1'='1"]
Leave the site in this mode for about a week of real traffic. Use the admin area, save posts, upload files, run the checkout. Everything in the log that was a normal action is a false positive you must fix before you block.
5. Switch to blocking
In /etc/nginx/modsec/modsecurity.conf:
SecRuleEngine On
Reload nginx and test again. On our system:
| Request | Answer |
|---|---|
| Normal page | 200 |
?id=1' OR '1'='1 (SQL injection) |
403 |
?f=../../etc/passwd (file read) |
403 |
?q=<script>alert(1)</script> (XSS) |
403 |
A blocked request appears in nginx's error.log:
ModSecurity: Access denied with code 403 (phase 2). Matched "Operator `Ge' with parameter `5' against variable `TX:BLOCKING_INBOUND_ANOMALY_SCORE' (Value: `20' ) [id "949110"] [msg "Inbound Anomaly Score Exceeded (Total Score: 20)"]
How CRS decides: the anomaly score
That log line confuses everybody at first. Rule 949110 is not the rule that found the attack. It is the judge.
Each rule that matches adds points to the request: 5 for a critical match, less for smaller oddities. At the end, rule 949110 compares the total with the threshold, which is 5 by default. Our XSS test matched four rules and reached 20.
So when you look for the cause of a block, ignore 949110 and read the lines before it in the audit log. They name the rules that added the points.
Removing false positives
Say your editor at /admin/editor sends content in a parameter called id, and rule 942100 flags it. You have three options, from best to worst.
1. Exclude one parameter from one rule on one path. Create /etc/nginx/modsec/exclusions-before.conf:
SecRule REQUEST_URI "@beginsWith /admin/editor" \
"id:1000,phase:1,pass,nolog,ctl:ruleRemoveTargetById=942100;ARGS:id"
and load it in main.conf before the CRS rules:
Include /etc/nginx/modsec/modsecurity.conf
Include /etc/nginx/modsec/crs-setup.conf
Include /etc/nginx/modsec/exclusions-before.conf
Include /etc/nginx/modsec/coreruleset-4.30.0/rules/*.conf
We tested this: the same SQL injection string returns 200 on /admin/editor/ and still 403 everywhere else.
2. Turn the WAF off for one location. Simple, but that path is then unprotected:
location /admin/editor/ { modsecurity off; }
Only do this for paths that are already behind a login and an IP restriction.
3. Turn the WAF off for the site. This is what most people do after the first false positive, and it is why step 4 matters.
For well-known applications, check the CRS plugins first. The project publishes ready-made exclusion sets for WordPress, Nextcloud, phpMyAdmin and others; they go into the plugins directory of the rule set.
Paranoia levels
CRS has four levels. Level 1 is the default and has few false positives. Each higher level turns on more rules and needs more exclusions. To raise it, add to crs-setup.conf:
SecAction "id:900000,phase:1,pass,t:none,nolog,setvar:tx.blocking_paranoia_level=2"
Stay on level 1 until the site runs cleanly in blocking mode.
What a WAF does not do
It does not fix the hole. A WAF makes known attack patterns harder to use. A vulnerable plugin is still vulnerable, and a new trick may get through. Update the application; treat the WAF as a second layer.
With Zefily
In Zefily this is one button and one setting. Install WAF in the server's web stack installs ModSecurity and the pinned, checksum-verified CRS release. Each site then chooses Off, Detect only or Block. Zefily follows new CRS releases and ships them with its agent updates. See Access, caching and protection.
Running OpenLiteSpeed? See ModSecurity with the OWASP CRS on OpenLiteSpeed.
Frequently asked questions
Which CRS version should I use with nginx?
The current 4.x release from the CRS project, 4.30.0 on 12 November 2026. Ubuntu 26.04 packages CRS 3.3.8, which is an old line.
Does ModSecurity 3 work with nginx on Ubuntu 26.04?
Yes. The package libnginx-mod-http-modsecurity installs the connector 1.0.3 and the ModSecurity 3.0.14 engine for nginx 1.28.
What is rule 949110?
It is the CRS rule that blocks a request when its anomaly score reaches the threshold (5 by default). The rules that added the score are listed before it in the audit log.
What does SecRuleEngine DetectionOnly do?
ModSecurity checks every request and writes matches to the audit log, but blocks nothing. Use it to find false positives before you switch to On.
How do I fix a ModSecurity false positive?
Find the rule ID in the audit log and exclude only what is needed: one parameter, from one rule, on one path, with ctl:ruleRemoveTargetById in a file loaded before the CRS rules.
See Web stack for installing the WAF with Zefily.