Caution
Script is currently in beta.
Installs and configures ModSecurity with the OWASP Core Rule Set for Nginx.
This script builds software from source, installs packages, writes Nginx configuration, enables ModSecurity, tests the Nginx configuration, and restarts Nginx.
- Bash 4.0 or newer
- Root privileges
- Nginx installed and available in
PATH - A Debian/Ubuntu-style system with
apt-getanddpkg - Internet access
The script installs required packages such as:
gitautoconfautomakebuild-essentiallibcurl4-openssl-devlibgeoip-devlibpcre2-devlibtoollibxml2-devlibyajl-devpkgconfwgetzlib1g-dev
It may install additional development packages depending on how the installed Nginx binary was built.
Run the script from its directory:
cd hardening/Nginx\ WAF/
sudo ./nginx-waf.bash- Detects the installed Nginx version and configure arguments.
- Installs missing build dependencies through
apt-get. - Clones, builds, and installs ModSecurity v3.
- Clones the ModSecurity Nginx connector.
- Downloads matching Nginx source for the installed Nginx version.
- Builds the ModSecurity dynamic module for Nginx.
- Installs
ngx_http_modsecurity_module.sointo the Nginx modules path. - Writes module loading configuration under
/etc/nginx/modules-available. - Enables the module under
/etc/nginx/modules-enabled. - Creates
/etc/nginx/modsec. - Enables ModSecurity rule engine mode.
- Clones and configures the OWASP Core Rule Set.
- Writes
/etc/nginx/modsec/main.conf. - Runs
nginx -t. - Restarts Nginx.
System paths used by the script include:
/etc/nginx/modules-available/etc/nginx/modules-enabled/etc/nginx/modsec/etc/nginx/modsec/modsecurity.conf/etc/nginx/modsec/main.conf
The script also creates or reuses build directories in the current working directory:
ModSecurityModSecurity-nginxnginx-VERSIONnginx-VERSION.tar.gz
- Run this in a test environment before using it on a production web server.
- Review local Nginx packaging conventions before running it on systems with custom Nginx builds.
- The OWASP Core Rule Set can block legitimate traffic until tuned for the application.
- Existing local changes in reused
ModSecurity,ModSecurity-nginx, or CRS clone directories may affect the run.
Check the Nginx configuration:
sudo nginx -tCheck Nginx service status:
sudo systemctl status nginxConfirm the ModSecurity module load file exists:
ls -l /etc/nginx/modules-enabled/50-modsecurity.conf- Ubuntu 24.04
- Nginx 1.24.0 and later
See CHANGELOG.md.