Block Foreign IPs in Nginx with the GeoIP2 Module – Step‑by‑Step Guide
This guide explains how to install the ngx_http_geoip2 module, upgrade Nginx, download the MaxMind GeoIP2 database, configure geoip2 directives and map rules, and finally block non‑Chinese IP addresses by returning a 404 response.
Introduction
After noticing many malicious foreign IP requests in the Nginx access log, I decided to block all non‑Chinese IPs using the ngx_http_geoip2 module.
Install geoip2 dependencies
# yum install libmaxminddb-devel -yDownload ngx_http_geoip2_module
# git clone https://github.com/leev/ngx_http_geoip2_module.git
# mv ngx_http_geoip2_module/ /usr/local/Upgrade Nginx and compile with the module
My current Nginx version is 1.16, which does not support the module, so I upgraded to 1.18 and added the module during compilation.
# yum install libmaxminddb-devel -y
# tar -xf nginx-1.18.0.tar.gz
# cd nginx-1.18.0
# ./configure --with-http_stub_status_module \
--prefix=/usr/local/nginx \
--user=nginx --group=nginx --with-http_ssl_module --with-stream \
--add-module=/usr/local/ngx_http_geoip2_module
# make
# cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx1.16 # backup
# cp objs/nginx /usr/local/nginx/sbin/ # replace
# pkill nginx
# /usr/local/nginx/sbin/nginx # restartVerify Nginx version and modules
# /usr/local/nginx/sbin/nginx -VDownload the latest GeoIP2 database
Log in to www.maxmind.com , create an account, and download the GeoLite2‑Country (or City) GZIP file, then extract it to /usr/share/GeoIP/.
# cd /usr/share/GeoIP/
# ll # shows GeoIP.dat, GeoIP‑initial.dat, GeoLite2‑Country.mmdb, etc.Configure nginx.conf
Backup the original configuration file first.
# cp /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf-bak
# vim /usr/local/nginx/conf/nginx.confAdd the following directives inside the http block:
geoip2 /usr/share/GeoIP/GeoLite2-City.mmdb {
auto_reload 5m;
$geoip2_data_country_code country iso_code;
}
map $geoip2_data_country_code $allowed_country {
default yes;
CN no;
}Then inside the desired
server locationblock add:
if ($allowed_country = yes) {
return 404;
}Test the configuration
Reload Nginx and verify the syntax.
# /usr/local/nginx/sbin/nginx -t
# /usr/local/nginx/sbin/nginx -s reloadAccess the site from a foreign IP (e.g., a Korean IP) and observe a 404 response. Check the access log to confirm the request was blocked:
"13.125.1.194 - - [14/Aug/2020:16:15:51 +0800] "GET /favicon.ico HTTP/1.1" 404 548 "https://www.fxkjnj.com/"With these steps, the website now denies traffic from non‑Chinese IP addresses using Nginx and the GeoIP2 module.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
