Would you like to learn how to install the Nginx Modsecurity feature?  In this tutorial, we are going to configure the Nginx Modsecurity feature on a computer running Ubuntu Linux.

• Ubuntu 18
• Ubuntu 19
• Ubuntu 20
• Nginx 1.18.0
• ModSecurity 3.0.4

Equipment list

The following section presents the list of equipment used to create this tutorial.

As an Amazon Associate, I earn from qualifying purchases.

Tutorial Nginx - ModSecurity installation

Install the Nginx server.

Copy to Clipboard

Install the required packages.

Copy to Clipboard

Install the software named SSDEP.

Copy to Clipboard

Download the latest version of ModSecurity.

Copy to Clipboard

Compile and install ModSecurity.

Copy to Clipboard

Download the latest version of the Nginx connector for ModSecurity.

Copy to Clipboard

Verify the version of Nginx installed on your system.

Copy to Clipboard

Here is the command output.

Copy to Clipboard

Download the source code of the same version of Nginx installed on your system.

Copy to Clipboard

Compile and install the Nginx connector.

Copy to Clipboard

Download and install the ModSecurity Core Rule Set.

Copy to Clipboard

Congratulations! You have finished the ModSecurity installation on the Nginx server.

Tutorial Nginx - ModSecurity configuration

Edit the Nginx configuration file.

Copy to Clipboard

Add the following line in the Nginx configuration file.

Copy to Clipboard

Here is the file before our configuration.

Copy to Clipboard

Here is the file after our configuration.

Copy to Clipboard

Create a directory named Modsec and copy the required configuration files.

Copy to Clipboard

Edit the ModSecurity configuration file.

Copy to Clipboard

Locate the following lines.

Copy to Clipboard

Change these lines to the following configuration.

Copy to Clipboard

Here is the file after our configuration.

Copy to Clipboard

Create a file to enable ModSecurity to use the installed CRS rules.

Copy to Clipboard

Here is the file content.

Copy to Clipboard

Edit the Nginx configuration file for the default website.

Copy to Clipboard

Add the following line in the Nginx configuration file.

Copy to Clipboard

Here is the file before our configuration.

Copy to Clipboard

Here is the file after our configuration.

Copy to Clipboard

Restart the Nginx service.

Copy to Clipboard

Optionally, use your browser to send a test request to the Nginx server.

After sending a test request, verify the ModSecurity log

Copy to Clipboard

Congratulations! You have finished the ModSecurity configuration on the Nginx server.