Security
What Is a Web Application Firewall (WAF)? Setup Guide for Nginx
By NileVPS Team · Published · 5 min read
اقرأ المقال بالعربيA normal firewall decides whether a packet may reach a port. Once port 443 is open, it has no opinion about what travels through it. That is where most website attacks live: a login form flooded with password guesses, a search box fed with SQL fragments, a comment field carrying a script tag. A web application firewall (WAF) sits in front of your application and inspects HTTP requests themselves, blocking the ones that look malicious before your code ever sees them. This guide explains how a WAF works, what it can and cannot do, and how to run an open-source WAF on your own Nginx server.
Network firewall vs WAF
| Network firewall | Web application firewall | |
|---|---|---|
| Layer | Network and transport (IP, TCP, UDP) | Application (HTTP and HTTPS) |
| Looks at | Addresses, ports, protocols | URLs, headers, cookies, request bodies |
| Typical rule | Allow TCP 443 from anywhere | Block requests containing SQL injection patterns |
| Stops | Port scans, access to closed services | SQLi, XSS, path traversal, bad bots |
| Examples | ufw, nftables, cloud firewall | ModSecurity, Coraza, CDN-based WAFs |
You need both. The network firewall closes everything that should not be public, and the WAF filters traffic on the ports that must stay open. Our article on protecting a website from common attacks covers the wider picture.
How a WAF decides what to block
Most WAFs combine several techniques:
- Signature rules that match known attack patterns, such as
UNION SELECTin a query string or../sequences in a file path. - Anomaly scoring, where each suspicious trait adds points and the request is blocked only when the total passes a threshold. This reduces false alarms from a single odd character.
- Protocol validation, which rejects malformed requests, unusual methods or oversized headers.
- Positive rules you write yourself, for example allowing only
GETandPOSTon a given path.
A WAF can run in front of the server as a hosted service, as a module inside the web server, or as a separate reverse proxy. Running it on your own VPS gives you full control and keeps request data on infrastructure you manage.
Running ModSecurity with the OWASP CRS on Nginx
ModSecurity is a long-standing open-source WAF engine, now maintained under the OWASP project. Version 3 (libmodsecurity) works with Nginx through a connector module. The OWASP Core Rule Set (CRS) provides a well-tested generic rule set.
1. Install the packages
On Debian and Ubuntu, packaged versions are available:
sudo apt update
sudo apt install libnginx-mod-http-modsecurity modsecurity-crsPackage names and file locations differ between distributions and releases, so check where files landed with dpkg -L modsecurity-crs.
2. Start in detection-only mode
Copy the recommended configuration and keep the engine in logging mode at first:
sudo cp /etc/modsecurity/modsecurity.conf-recommended /etc/modsecurity/modsecurity.confInside that file, confirm the line:
SecRuleEngine DetectionOnlyIn this mode ModSecurity logs what it would block without blocking anything, which is the safest way to begin on a live site.
3. Load the rules
Create /etc/nginx/modsec/main.conf that includes the engine settings and the CRS (adjust paths to match your system):
Include /etc/modsecurity/modsecurity.conf
Include /etc/modsecurity/crs/crs-setup.conf
Include /usr/share/modsecurity-crs/rules/*.confThen enable it in your server block:
server {
listen 443 ssl;
server_name example.com;
modsecurity on;
modsecurity_rules_file /etc/nginx/modsec/main.conf;
}Test and reload:
sudo nginx -t && sudo systemctl reload nginx4. Review the audit log
Matches are written to the audit log, by default /var/log/modsec_audit.log on Debian-based systems. Browse your site normally, submit forms, use the admin area, and watch which rules fire. Real attacks will appear quickly once the site is public, but so will false positives.
5. Tune, then enforce
When a legitimate request triggers a rule, exclude that rule for the specific path instead of disabling it everywhere:
SecRule REQUEST_URI "@beginsWith /wp-admin/post.php" \
"id:1000,phase:1,pass,nolog,ctl:ruleRemoveById=942100"Rule exclusions like this belong in a separate file loaded before the CRS rules, or in the CRS exclusion file, so updates do not overwrite them. After a quiet period with no false positives, switch to SecRuleEngine On.
Paranoia levels
The CRS uses paranoia levels from 1 to 4. Level 1 is the default and aims for very few false positives. Higher levels add stricter rules that catch more unusual attacks but need much more tuning. Most sites should stay at level 1 or 2 unless they handle highly sensitive data and have time to maintain exclusions.
What a WAF cannot do
- It does not fix vulnerable code. A WAF is a safety net, not a replacement for updates and secure coding.
- It cannot stop large volumetric DDoS floods that saturate your network link; that needs upstream filtering.
- It may miss attacks hidden in business logic, such as abusing a discount code.
- It adds some CPU work per request, so size your server accordingly.
Pair the WAF with request limits so a single client cannot overwhelm your app; see our guide on rate limiting with Nginx.
FAQ
Will a WAF slow down my website?
Inspection adds a small amount of processing to each request, and the effect grows with large request bodies and higher paranoia levels. For a typical site the difference is usually small compared to application and database time, but measure response times before and after enabling it on your own workload.
Do I need a WAF if I keep WordPress and plugins updated?
Updates are the most important defense, but there is always a gap between a vulnerability becoming known and a patch being installed. A WAF can block common exploit patterns during that gap and filters a lot of automated junk traffic.
Hosted WAF or self-hosted WAF?
A hosted WAF filters traffic before it reaches your server and can absorb more volume. A self-hosted WAF such as ModSecurity gives you full control over rules and logs at no extra service cost. Many setups combine an upstream filter with a local WAF for the rules specific to their application.