How to Install ModSecurity for Nginx on CentOS 7, Debian 8, and Ubuntu 16.04

ModSecurity is an open source web application firewall (WAF) module which is great for protecting Apache, Nginx, and IIS from various cyber attacks that target potential vulnerabilities in various web applications

In this article, we will install and configure ModSecurity for Nginx on CentOS 7, Debian 8, and Ubuntu 16.04.


  • An up-to-date installation of CentOS 7, Debian 8, or Ubuntu 16.04 64-bit.
  • Logging in as root.

Step 1: Update the system

Following this guide, update your server’s Kernel and Packages to the latest available version.

Step 2: Install dependencies

Before you can compile Nginx and ModSecurity successfully, you need to install several software packages as follows.

a) On CentOS 7:

yum groupinstall -y "Development Tools"yum install -y httpd httpd-devel pcre pcre-devel libxml2 libxml2-devel curl curl-devel openssl openssl-develshutdown -r now

b) On Debian 8 or Ubuntu 16.04:

apt-get install -y git build-essential libpcre3 libpcre3-dev libssl-dev libtool autoconf apache2-dev libxml2-dev libcurl4-openssl-dev automake pkgconf

Step 3: Compile ModSecurity

Due to several instabilities reported on ModSecurity for Nginx master branch, for now, it is officially recommended to use the latest version of the nginx_refactoring branch whenever possible.

Download the nginx_refactoring branch of ModSecurity for Nginx:

cd /usr/srcgit clone -b nginx_refactoring

Compile ModSecurity:

a) On CentOS 7:

cd ModSecuritysed -i '/AC_PROG_CC/a/AM_PROG_CC_C_O' configure.acsed -i '1 i/AUTOMAKE_OPTIONS = subdir-objects' --enable-standalone-module --disable-mlogcmake

Note: the two sed commands above are used to prevent warning messages when using newer
automake versions.

b) On Debian 8 or Ubuntu 16.04:

cd ModSecurity./ --enable-standalone-module --disable-mlogcmake

Step 4: Compile Nginx

Download and unarchive the latest stable release of Nginx which is Nginx 1.10.3 at the time of writing:

cd /usr/srcwget -zxvf nginx-1.10.3.tar.gz && rm -f nginx-1.10.3.tar.gz

a) On CentOS 7:

First, you need to create a dedicated user nginx and a dedicated group nginx for Nginx:

groupadd -r nginxuseradd -r -g nginx -s /sbin/nologin -M nginx

Then compile Nginx while enabling ModSecurity and SSL modules:

cd nginx-1.10.3/./configure --user=nginx --group=nginx --add-module=/usr/src/ModSecurity/nginx/modsecurity --with-http_ssl_modulemakemake install

Modify the default user of Nginx:

sed -i "s/#user  nobody;/user nginx nginx;/" /usr/local/nginx/conf/nginx.conf

b) On Debian 8 or Ubuntu 16.04:

First, you should use the existing user www-data and the existing group www-data.

Then compile Nginx while enabling ModSecurity and SSL modules:

cd nginx-1.10.3/./configure --user=www-data --group=www-data --add-module=/usr/src/ModSecurity/nginx/modsecurity --with-http_ssl_modulemakemake install

Modify the default user of Nginx:

sed -i "s/#user  nobody;/user www-data www-data;/" /usr/local/nginx/conf/nginx.conf

Having Nginx successfully installed, related files will be located at:

nginx path prefix: "/usr/local/nginx"nginx binary file: "/usr/local/nginx/sbin/nginx"nginx modules path: "/usr/local/nginx/modules"nginx configuration prefix: "/usr/local/nginx/conf"nginx configuration file: "/usr/local/nginx/conf/nginx.conf"nginx pid file: "/usr/local/nginx/logs/"nginx error log file: "/usr/local/nginx/logs/error.log"nginx http access log file: "/usr/local/nginx/logs/access.log"nginx http client request body temporary files: "client_body_temp"nginx http proxy temporary files: "proxy_temp"nginx http fastcgi temporary files: "fastcgi_temp"nginx http uwsgi temporary files: "uwsgi_temp"nginx http scgi temporary files: "scgi_temp"

you can test the installation with:

/usr/local/nginx/sbin/nginx -t

If nothing goes wrong, the output should be:

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is oknginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

For your convenience, you can setup a systemd unit file for Nginx:

cat <<EOF>> /lib/systemd/system/nginx.service[Service]Type=forkingExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.confExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.confExecReload=/usr/local/nginx/sbin/nginx -s reloadKillStop=/usr/local/nginx/sbin/nginx -s stopKillMode=processRestart=on-failureRestartSec=42sPrivateTmp=trueLimitNOFILE=200000[Install]WantedBy=multi-user.targetEOF

Moving forward, you can start/stop/restart Nginx as follows:

systemctl start nginx.servicesystemctl stop nginx.servicesystemctl restart nginx.service

Step 4: Configure ModSecurity and Nginx

4.1 Configure Nginx:

vi /usr/local/nginx/conf/nginx.conf

Find the following segment within the http {} segment:

location / {    root   html;    index  index.html index.htm;}

Insert the below lines into the location / {} segment:

ModSecurityEnabled on;ModSecurityConfig modsec_includes.conf;#proxy_pass http://localhost:8011;#proxy_read_timeout 180s;

The final result should be:

location / {    ModSecurityEnabled on;    ModSecurityConfig modsec_includes.conf;    #proxy_pass http://localhost:8011;    #proxy_read_timeout 180s;    root   html;    index  index.html index.htm;}

Save and quit:


Note: The Nginx config above is only a sample config for using Nginx as a web server rather than a reverse proxy. If you are using Nginx as a reverse proxy, remove the # character in last two lines and make appropriate modifications to them.

4.2 Create a file named /usr/local/nginx/conf/modsec_includes.conf:

cat <<EOF>> /usr/local/nginx/conf/modsec_includes.confinclude modsecurity.confinclude owasp-modsecurity-crs/crs-setup.confinclude owasp-modsecurity-crs/rules/*.confEOF

Note: The config above will apply all of the OWASP ModSecurity Core Rules in the owasp-modsecurity-crs/rules/ directory. If you want to apply selective rules only, you should remove the include owasp-modsecurity-crs/rules/*.conf line, and then specify exact rules you need after step 4.5.

4.3 Import ModSecurity configuration files:

cp /usr/src/ModSecurity/modsecurity.conf-recommended /usr/local/nginx/conf/modsecurity.confcp /usr/src/ModSecurity/unicode.mapping /usr/local/nginx/conf/

4.4 Modify the /usr/local/nginx/conf/modsecurity.conf file:

sed -i "s/SecRuleEngine DetectionOnly/SecRuleEngine On/" /usr/local/nginx/conf/modsecurity.conf

4.5 Add OWASP ModSecurity CRS (Core Rule Set) files:

cd /usr/local/nginx/confgit clone owasp-modsecurity-crsmv crs-setup.conf.example crs-setup.confcd rulesmv REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf.example REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.confmv RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf

Step 5: Test ModSecurity

Start Nginx:

systemctl start nginx.service

Open port 80 in order to allow outside access:

a) On CentOS 7:

firewall-cmd --zone=public --permanent --add-service=httpfirewall-cmd --reload

b) On Debian 8:

iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPTiptables -A INPUT -p tcp --dport 80 -j ACCEPTiptables -A INPUT -p tcp --dport 22 -j ACCEPTiptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPTiptables -P INPUT DROPiptables -P OUTPUT ACCEPTiptables -P FORWARD DROPtouch /etc/iptablesiptables-save > /etc/iptables

c) On Ubuntu 16.04:

ufw allow OpenSSHufw allow 80ufw default denyufw enable    

Point your web browser to:"><script>alert(1);</script>

Use grep to fetch error messages as follows:

grep error /usr/local/nginx/logs/error.log

The output should include several error messages which are similar to:

2017/02/15 14:07:54 [error] 10776#0: [client] ModSecurity: Warning. detected XSS using libinjection. [file "/usr/local/nginx/conf/owasp-modsecurity-crs/rules/REQUEST-941-APPLICATION-ATTACK-XSS.conf"] [line "56"] [id "941100"] [rev "2"] [msg "XSS Attack Detected via libinjection"] [data "Matched Data:  found within ARGS:param: /x22><script>alert(1);</script>"] [severity "CRITICAL"] [ver "OWASP_CRS/3.0.0"] [maturity "1"] [accuracy "9"] [tag "application-multi"] [tag "language-multi"] [tag "platform-multi"] [tag "attack-xss"] [tag "OWASP_CRS/WEB_ATTACK/XSS"] [tag "WASCTC/WASC-8"] [tag "WASCTC/WASC-22"] [tag "OWASP_TOP_10/A3"] [tag "OWASP_AppSensor/IE1"] [tag "CAPEC-242"] [hostname ""] [uri "/index.html"] [unique_id "ATAcAcAkucAchGAcPLAcAcAY"]

That’s it. As you see, The ModSecurity module has successfully logged this attack in accordance with its default action policy. If you want to make more custom settings, please carefully review and edit /usr/local/nginx/conf/modsecurity.conf and /usr/local/nginx/conf/owasp-modsecurity-crs/crs-setup.conf files.

Want to contribute?

You could earn up to $300 by adding new articles

Submit your article
Suggest an update
Request an article

No comments

Powered by Blogger.