Modul GeoIP memungkinkan Anda melakukan operasi berbasis geografis di NGINX seperti memblokir IP dari lokasi tertentu atau mengarahkan pengunjung website berdasarkan negara. Namun pada awal instalasi nginx modul GeoIP tidak selalu disertakan sehingga perlu ditambahakan secara manual dengan mengkompilasi modul lalu load modul di nginx.
Install Pre-Requisites #
Install package dependensi GeoIP seperti berikut.
yum -y install libmaxminddb libmaxminddb-devel geoipupdate geolite2-city geolite2-country GeoIP GeoIP-GeoLite-data GeoIP-GeoLite-data-extra
Download NGINX source #
Download nginx source menyesuaikan versi yang Anda gunakan.
wget https://nginx.org/download/nginx-1.24.0.tar.gz
Download GeoIP Module #
Download modul GeoIP dari repository github.
wget https://github.com/leev/ngx_http_geoip2_module/archive/refs/tags/3.4.tar.gz
Build Module #
Ekstrak archive nginx dan modul GeoIP.
tar -xaf nginx-1.24.0.tar.gz
tar -xaf 3.4.tar.gz -C nginx-1.24.0/
Compile modul.
cd nginx-1.24.0
./configure --with-compat --add-dynamic-module=ngx_http_geoip2_module-3.4
make modules
Jika proses compile berhasil, selanjutnya copy file ngx_http_geoip2_module.so
ke /etc/nginx/modules
.
cp objs/ngx_http_geoip2_module.so /etc/nginx/modules/
Lalu load modul geoip.
nano /etc/nginx/modules.conf.d/geoip.conf
load_module "modules/ngx_http_geoip2_module.so";
Example Usage #
Untuk kasusnya akan mencoba memblokir trafik dan semua negara dan mengizinkan negara Indonesia (ID) yang mendapatkan izin akses.
Buat konfigurasi file GeoIP seperti berikut.
nano /etc/nginx/conf.d/geoip.conf
geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {
$geoip2_data_country_code default=ID country iso_code;
$geoip2_data_country_name default=ID country names en;
$geoip2_data_city_name default=ID city names en;
$geoip2_data_postal_code default=ID postal code;
$geoip2_data_latitude default=ID location latitude;
$geoip2_data_longitude default=ID location longitude;
}
geoip2 /usr/share/GeoIP/GeoLite2-City.mmdb {
$geoip2_data_city_name default=ID city names en;
$geoip2_data_state_name default=ID subdivisions 0 names en;
$geoip2_data_state_code default=ID subdivisions 0 iso_code;
}
map $geoip2_data_country_code $allowed_country {
default no;
ID yes;
}
Edit virtualhost yang akan dilimit aksesnya seperti berikut.
location / {
...
if ($allowed_country = no) {
return 444;
}
...
}
Restart service nginx untuk menerapkan perubahan.
nginx -t
systemctl restart nginx
Testing #
Test dengan konfig ID yes
$ curl -I example.com
HTTP/1.1 200 OK
Server: nginx/1.24.0
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/7.4.33
Link: <http://example.com/wp-json/>; rel="https://api.w.org/"
Link: <http://example.com/wp-json/wp/v2/pages/25>; rel="alternate"; type="application/json"
Link: <http://example.com/>; rel=shortlink
Date: Mon, 23 Oct 2023 00:29:12 GMT
X-Page-Speed: 1.13.35.2-0
Cache-Control: max-age=0, no-cache
Test dengan konfig ID no
$ curl -I example.com
curl: (52) Empty reply from server
Source: