Third-party module examples: Geoip2
Step 1:
Select compile and install Nginx
Step 2:
Click Add custom module and fill in the corresponding information
--add-module=/tmp/ngx_http_geoip2_module
yum install libmaxminddb-devel -y
git clone https://github.com/leev/ngx_http_geoip2_module.git /tmp/ngx_http_geoip2_module
Prefix script: Before compiling nginx, the commands in it are executed first, and the commands are separated by newlines.
Here we first install the Geoip2 dependency, and then use git to clone Geoip2 to the / tmp directory
After clicking submit, the submitted module information will be saved
Step 3:
Select the module you need to compile Click Submit again to start the installation
After the installation is complete, we can check whether the module has been compiled on the command line
Step 4:
Download the geoip2 core library and save it to the specified directory
https://dev.maxmind.com/geoip/geoip2/geolite2/
We download the GeoLite2-City and GeoLite2-Country databases
After downloading, upload it to the server and decompress the database ( .mmdb ) file to /tmp (note that this is only for testing, please store it in another directory)
Step 5:
Now we set the rules to test
Set to block IP access outside China
Open Nginx Manager --> config
Add the following code under http
geoip2 /tmp/GeoLite2-Country.mmdb {
$geoip2_data_country_code country iso_code;
}
map $geoip2_data_country_code $allowed_country {
default yes;
CN no;
}
Step 6:
Activate rules under the site you need to set
if ($allowed_country = yes) { return 403; }
Try to visit the site