24/10/2026

Free WAF with nginx, ModSecurity and OWASP CRS

That's where a WAF comes in. In this post we put a free WAF in front of nginx using ModSecurity and OWASP CRS.

Your site has HTTPS, your server is patched, your server firewall is locked down. Then someone types ' OR '1'='1 into your search box, or puts /etc/passwd in a URL. That request goes straight past all of that and lands on your application code.

That's where a WAF comes in. In this post we put a free WAF in front of nginx using ModSecurity and OWASP CRS. The ITcorv team ran the tests in this post ourselves, and the output is right here.

What is a WAF, and what are ModSecurity and CRS?

WAF stands for web application firewall. A normal firewall looks at IPs and ports. A WAF looks inside the HTTP request itself: the URL, parameters, headers, form bodies and JSON.

There are two pieces:

  • ModSecurity is the engine. Version 3 is a library called libmodsecurity, and it plugs into nginx through a module. It's an OWASP project now.
  • OWASP CRS is the rules. A ready-made rule set for SQL injection, XSS, command execution, reading system files, scanners and a lot more.

CRS gives each suspicious request a score. When the total reaches the threshold (5 by default), the request is blocked with a 403. That way one small hint on its own doesn't get a request blocked.

First, a trap: versions

The first thing most people try is installing it from Debian or Ubuntu packages. We tried that on Debian 13 and hit a wall:

  • libmodsecurity is 3.0.14 on Debian 13 and 3.0.12 on Ubuntu 24.04.
  • CRS 4.25.1 and later need libmodsecurity 3.0.16 or newer. That's in the official CRS changelog.
  • On Debian 13 with CRS 4.25.2, nginx -t failed with this error and nginx wouldn't start at all:
text@itcorv:~
"modsecurity_rules_file" directive Rules error. File: .../REQUEST-901-INITIALIZATION.conf. Line: 333. Column: 53. Expecting an action, got:  @*,\
  • The distros' own modsecurity-crs package is still on the 3.3 series, and CRS 3.3 reached end of life in Q3 2026. No more security updates.

So you either build libmodsecurity from source yourself, or you use the official CRS container image. For most projects we recommend the second option. Its nginx image ships ModSecurity 3.0.17, the latest release.

For the CRS version you have two choices:

  • 4.25.x (LTS) gets security fixes until Q3 2027, but no new rules. This is what we prefer for production.
  • The latest release (4.30.0 right now) has newer rules, but you'll need to upgrade it more often.

The layout

text@itcorv:~
user  ->  nginx on the server (TLS)  ->  WAF container  ->  application

The nginx on your server keeps handling HTTPS like before. If you haven't set nginx up as a reverse proxy yet, see our nginx post. The WAF needs to see the decrypted request, so it sits after TLS. The application no longer has a public port; it's only reachable from inside the Docker network.

Sample ModSecurity nginx config with Docker Compose

1) docker-compose.yml

yaml@itcorv:~
services:
  waf:
    image: owasp/modsecurity-crs:4.25.2-nginx-lts
    restart: unless-stopped
    ports:
      - "127.0.0.1:8080:8080"
    environment:
      BACKEND: "http://app:3000"
      SERVER_NAME: "example.com"
      MODSEC_RULE_ENGINE: "DetectionOnly"
      BLOCKING_PARANOIA: "1"
      ANOMALY_INBOUND: "5"
      ANOMALY_OUTBOUND: "4"
      REAL_IP_HEADER: "X-Real-IP"
      SET_REAL_IP_FROM: "172.30.0.0/24"
    volumes:
      - ./waf/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf:/etc/modsecurity.d/owasp-crs/rules/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf:ro
    depends_on:
      - app
    networks:
      - waf-net

  app:
    image: your-app:latest
    expose:
      - "3000"
    networks:
      - waf-net

networks:
  waf-net:
    ipam:
      config:
        - subnet: 172.30.0.0/24

A few notes:

  • Replace app:3000 and your-app:latest with your own app, and example.com with your domain.
  • The WAF port is only open on 127.0.0.1. Nobody on the internet can reach it directly, only the nginx on your server.
  • MODSEC_RULE_ENGINE is set to DetectionOnly for now. That means it only logs and doesn't block anything. The tips section explains why.
  • BLOCKING_PARANOIA is how strict the rules are, from 1 to 4. Start with 1.
  • If pulling from Docker Hub gives you trouble, see our Docker Hub post. The same image is also on ghcr.io/coreruleset/modsecurity-crs.

2) Exclusion file: waf/REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf

Say you have a docs page where users search for Linux paths like /etc/passwd. Those requests are legit, but CRS blocks them. Instead of turning the whole rule off, take just that parameter on just that path out of the rule:

apache@itcorv:~
SecRule REQUEST_URI "@beginsWith /docs/" \
    "id:1000,\
    phase:1,\
    pass,\
    nolog,\
    ctl:ruleRemoveTargetById=930120;ARGS:q,\
    ctl:ruleRemoveTargetById=932160;ARGS:q"

In plain words: only under /docs/, and only for the q parameter, skip rule 930120 (system file access) and 932160 (shell code). The rest of the site and every other rule work exactly as before.

If you don't need any exclusions yet, still create this file and leave it empty, so the mount doesn't fail.

3) nginx on the server

nginx@itcorv:~
server {
    listen 443 ssl;
    server_name example.com;

    # ssl_certificate and ssl_certificate_key as before

    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;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

X-Real-IP matters. Without it, every request in the WAF logs looks like it came from the same IP. SET_REAL_IP_FROM in the compose file is there so the WAF trusts that header.

Testing

Bring it up:

bash@itcorv:~
docker compose up -d
docker compose ps

Wait a bit until waf shows healthy.

A normal request:

bash@itcorv:~
curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: example.com' http://127.0.0.1:8080/

You should get 200.

Now an attack-style request. This is the same test the CRS docs suggest:

bash@itcorv:~
curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: example.com' 'http://127.0.0.1:8080/?foo=/etc/passwd&bar=/bin/sh'

Since we're still on DetectionOnly, you'll get 200 again. But it should show up in the logs:

bash@itcorv:~
docker compose logs waf | grep -E '930120|932160|949110'

In our test, this request triggered these three rules:

  • 930120: OS File Access Attempt
  • 932160: Remote Command Execution: Unix Shell Code Found
  • 949110: Inbound Anomaly Score Exceeded, meaning the total score went over the threshold

Now set MODSEC_RULE_ENGINE to On and run docker compose up -d again. The same attack curl should now return 403, and the normal request still 200.

We ran a few more tests, and all of them got a 403:

bash@itcorv:~
# SQL injection
curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: example.com' "http://127.0.0.1:8080/?id=1%27%20OR%20%271%27=%271"
# XSS
curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: example.com' "http://127.0.0.1:8080/?q=%3Cscript%3Ealert(1)%3C/script%3E"
# Scanner User-Agent
curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: example.com' -A "Nikto" http://127.0.0.1:8080/

Check the /docs/ exclusion like this:

bash@itcorv:~
curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: example.com' 'http://127.0.0.1:8080/docs/?q=/etc/passwd'
curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: example.com' 'http://127.0.0.1:8080/?q=/etc/passwd'

The first should return 200, the second 403.

Why -H 'Host: example.com'? If you test with a bare IP, rule 920350 (Host header is a numeric IP address) fires too and pushes the score up, which makes your test results misleading.

Tips

  • Start with DetectionOnly. Let it only log for a week or two. Then go through the logs and see what got flagged by mistake (false positives). Write exclusions for those, then switch to On. If you block from day one, you'll probably break a perfectly good form or API.
  • Keep exclusions narrow. Just that rule, that parameter, that path. Turning ModSecurity off for a whole path, or killing a whole rule group, means you have no WAF there anymore.
  • IDs for your own rules. Keep your own rules in the 1 to 99999 range so they don't clash with CRS rules.
  • Don't jump the paranoia level. Each level up catches more attacks and brings more false positives. Level 1 is enough for most sites. If you have something sensitive, like a finance panel, try 2, and try it in DetectionOnly first.
  • Response rules. By default CRS also checks what your server sends back. If your site shows user input on a page (like comments), someone could post specific text that gets that page blocked for everyone. The CRS docs call this RFDoS. If that's a risk for you, rule 900500 in crs-setup.conf turns that part off.
  • Pin the image tag. 4.25.2-nginx-lts is better than plain nginx, because the nginx tag moves with every new release. But don't forget to update. CRS and ModSecurity ship security fixes regularly.
  • Collect the logs. This image writes its audit log as JSON to the container output by default. You can ship it to Loki and use Grafana to see which rules and which IPs fire the most.
  • A WAF doesn't replace patching. It catches a lot of well-known attacks, but if your app has a bug, the bug still needs fixing. A WAF buys you time; it doesn't do miracles.
  • The rest of the network matters too. A WAF only sees web traffic. You still need a secure router and edge firewall. For MikroTik, see our MikroTik security checklist.

FAQ

Should I install ModSecurity from Debian/Ubuntu packages?

No. Debian 13 ships 3.0.14 and Ubuntu 24.04 ships 3.0.12, and CRS 4.25.1 and later won't work with either. Use the official CRS image or build it from source.

Does a WAF slow my site down?

It adds some processing, but at paranoia level 1 most sites won't notice. Raise the level gradually and test in DetectionOnly first.

How do I fix false positives?

With a narrow exclusion in the REQUEST-900 file: that rule, that parameter, that path. Never by turning off a whole rule.

Does a WAF replace patching?

No. It buys you time against well-known attacks, but the bug in your app still has to be fixed.

If you want a WAF on your company's site or API, tuned to your real traffic, the ITcorv team can help through our network security services. You can also start with a consultation.