How v2 and v3 handle a request differently
mod_security2) is an Apache module. It hooks directly into Apache's request lifecycle, so Apache itself decides when each phase runs. Phase 1 runs when request headers arrive, phase 2 after the request body is read, phase 3 on response headers, phase 4 on the response body, and phase 5 at logging time. When a rule fires deny, the module tells Apache to stop and return the status code, and Apache does it.
ModSecurity 3 (libmodsecurity) is a standalone C++ library that knows nothing about Apache or Nginx. A separate piece of code called a connector (ModSecurity-nginx or ModSecurity-apache) feeds request data into the library and asks for a decision. For each request, the connector creates a transaction and calls the library step by step:
msc_process_connection()with client and server IP and port.msc_process_uri()with the URI, method and HTTP version.msc_add_request_header()for each header, thenmsc_process_request_headers(), which runs phase 1.msc_append_request_body()with body chunks, thenmsc_process_request_body(), which runs phase 2.- The same pattern for response headers (phase 3) and the response body (phase 4).
msc_process_logging(), which runs phase 5.
msc_intervention() to ask whether a rule requested a block, a redirect or a status change. The library does not block anything itself. It records that an intervention is pending, and the connector has to act on it.
That design explains most of the surprises after an upgrade. A rule can match and log correctly while the connector is no longer in a position to change the response. On the response side, the web server may already have sent headers to the client by the time phase 3 or phase 4 finishes. A deny at that point can end up as a log entry only, or as a truncated response, depending on the connector and how it buffers. Rules that relied on phase 4 blocking in Apache with v2 are the most likely to lose their effect.
The directives and actions that changed
| Area | What happens under v3 | What to do |
|---|---|---|
Engine-level directives such as SecGuardianLog, SecChrootDir, SecDisableBackendCompression, SecContentInjection | Not implemented in libmodsecurity, so the feature is gone | Remove them. Do the same job in the web server or OS if you still need it |
SecRequestBodyInMemoryLimit and stream inspection directives | Listed as unsupported in v3 docs | Rely on SecRequestBodyLimit and the connector or web server body limits |
append / prepend actions | Content injection is not available | Drop rules that depend on it |
Persistent collections (initcol, setsid, setuid) | Persistence depends on how the library was built (LMDB is optional) | Confirm the build, or move rate limiting to the web server |
expirevar | Has had known gaps in v3 | Test any counter that relies on expiry |
ip.login_attempts on every failed login and blocks once the counter passes a threshold. If the v3 build keeps collections only in memory per worker, or never writes them anywhere, the counter never reaches the threshold. The rule parses, it matches every request, and it still never blocks.
The other common cause is the config file itself. The Nevis guide on upgrading CRS recommends comparing your old modsecurity.conf with the modsecurity.conf-recommended shipped with the new version. The new file includes settings such as SecPcreMatchLimit and request body controls. It also ships with SecRuleEngine DetectionOnly, which logs matches and never blocks. If someone copied the recommended file in place and never changed that line, nothing blocks at all.
Lint the rules with modsec-rules-check
modsec-rules-check. It loads a rules file with the same parser the engine uses and reports anything it cannot handle. Whether your distribution package includes it varies. If it is missing, it is built from the tools/rules-check directory of the libmodsecurity source tree.
Run it against the main config and against each custom rules file separately, so an error points to a specific file:
BASH
modsec-rules-check /etc/modsecurity/modsecurity.conf
modsec-rules-check /etc/modsecurity/custom/login-protect.conf
# Illustrative output format (wording differs by version):
# : /etc/modsecurity/modsecurity.conf -- Loaded 0 rules.
# Test ok.
# : /etc/modsecurity/custom/login-protect.conf --
# Problems loading the rules: ... unsupported directive or action ...BASH
grep -rnE 'initcol|setsid|setuid|expirevar|append:|prepend:|phase:(3|4)|SecRuleEngine|SecDefaultAction' /etc/modsecurity/phase:3 or phase:4 combined with deny need a check of how your connector handles response-side blocking. Lines with collections need confirmation that the build persists them. SecRuleEngine shows you whether detection-only mode is set anywhere, including in per-site overrides.
Also check SecDefaultAction. In older setups, many rules had no disruptive action of their own and inherited deny from a SecDefaultAction line in the old config. If the new config sets a different default, or the line now carries a different phase, those rules start logging matches without blocking.
Prove blocking end to end with a canary rule
TEXT
# Canary rule: blocks only requests carrying ?modsec_canary=1
SecRule ARGS_GET:modsec_canary '@streq 1' 'id:1009001,phase:1,deny,status:403,log,msg:'"'"'Canary rule hit'"'"''
# Then, from a shell (reload the web server first):
# curl -s -o /dev/null -w '%{http_code}
' 'https://example.com/?modsec_canary=1'
# Expected: 403. A 200 means the connector is not enforcing interventions.msg with double quotes in the rules file, which is the usual SecLang style. The single-quote form above exists only to survive copying through shell snippets.
A 200 on the canary points to something more basic than individual rules. On Nginx, check that modsecurity on; is set in the right server or location block, and that modsecurity_rules_file points to the file that includes your rules. On Apache with the v3 connector, check that the old mod_security2 module is not still loaded alongside it, competing for the same config. When the canary returns 403, move the same deny to phase 2, test with a POST body, and then test any response-side rules you depend on. Remove the canary when you finish.
Rewriting what no longer works
RESPONSE-999-EXCLUSION-RULES.conf. That way, a rule set update does not overwrite them, and you can review them as one unit after each upgrade.
After any ModSecurity 3 upgrade, run three checks before you trust the WAF. Run modsec-rules-check on every rules file. Grep for collections, response-phase denies and the engine mode. Confirm with a canary request that a phase 1 deny actually returns 403. A clean config test and a running web server only show that the rules loaded.