Install Web Application Firewall (WAF) with NGINX Debian 10
1) ติดตั้ง Nginx stable เวอร์ชั่นล่าสุด ถ้าลงโดยใช้ repo ที่มีอยุ่แล้วจะติดตั้งไม่ได้ ต้องลงด้วยตัวเองตามลิงค์ด้านล่าง ต้องใช้เวอร์ชั้น 1.11.5 ขึ้นไปเท่านั้น

ติดตั้ง Packages ที่จำเป็นต้องใช้ ตามด้านล่าง
apt-get install -y apt-utils autoconf automake build-essential git libcurl4-openssl-dev libgeoip-dev liblmdb-dev libpcre++-dev libtool libxml2-dev libyajl-dev pkgconf wget zlib1g-dev
2) clone ModSecurity v3 จากเว็บ github และทำการติดตั้ง
git clone --depth 1 -b v3/master --single-branch https://github.com/SpiderLabs/ModSecurity
cd ModSecurity
git submodule init
git submodule update
./build.sh
./configure





make

make install
cd ..


3) เช็คเวอร์ชั่น Nginx ที่ติดตั้งอยู่

4) ติดตั้งตัว ModSecurity connector สำหรับ nginx
git clone --depth 1 https://github.com/SpiderLabs/ModSecurity-nginx.git

เช็คเวอร์ชั่น nginx ที่ติดตั้งแล้วโหลด sourcecode ให้ตรงกับเวอร์ชั่นที่เราติดตั้ง
wget http://nginx.org/download/nginx-1.xx.xx.tar.gz

จากนั้นแตกไฟล์ที่โหลดมา
tar -xvzmf nginx-1.xx.xx.tar.gz

cd เข้าไปในไฟล์ที่แตกมาแล้วรันทำสั่งต่อไปนี้
./configure --with-compat --add-dynamic-module=../ModSecurity-nginx


make modules

cp objs/ngx_http_modsecurity_module.so /etc/nginx/modules
cd ..

5) ทำการเพิ่มคอนฟิกใน nginx.conf
vim /etc/nginx/nginx.conf
เพิ่มคำสั่งนี้ลงในไฟล์
load_module modules/ngx_http_modsecurity_module.so;

6) ขั้นตอนสุดท้าย ทำการคอนฟิก สั่งรัน และ ทดสอบ ModSecurity
mkdir /etc/nginx/modsec
wget -P /etc/nginx/modsec/ https://raw.githubusercontent.com/SpiderLabs/ModSecurity/v3/master/modsecurity.conf-recommended
mv /etc/nginx/modsec/modsecurity.conf-recommended /etc/nginx/modsec/modsecurity.conf

วิธีสร้าง rule โดย สามารถใส่ไฟล์ rule ที่ path /etc/nginx/modsec
ตัวอย่าง

# From https://github.com/SpiderLabs/ModSecurity/blob/master/
# modsecurity.conf-recommended
#
# Edit to set SecRuleEngine On
Include "/etc/nginx/modsec/modsecurity.conf"
# Basic test rule
SecRule ARGS:testparam "@contains test" "id:1234,deny,status:403"
เพิ่ม rule ลงในไฟล์ nginx.conf เพื่อทำการทดสอบ
modsecurity on;
modsecurity_rules_file /etc/nginx/modsec/main.conf;

$ curl localhost?testparam=test

REF.
