====== NGINX GeoIP2 modul ======
==== Dinamikus modul fordítása ====
Az NGINX szerverhez használható nyílt forráskódú geoip2 modul legfrissebb változata a https://github.com/leev/ngx_http_geoip2_module oldalról tölthető le. A fordításhoz szükség van a gépen futó nginx szerver verziójának forráskódjára, amely a https://nginx.org/en/download.html oldalon érhető el.
=== Előkészítő műveletek ===
Telepíteni kell a szükséges csomagokat.
# dnf install gcc glibc-headers libmaxminddb-devel make openssl-devel pcre2-devel redhat-rpm-config tar zlib-devel
Meg kell határozni a használt nginx szerver verziót.
# nginx -V 2>&1 | grep '^nginx version:'
nginx version: nginx/1.29.5
A megfelelő verziót le kell tölteni, majd ki kell csomagolni.
# curl -L -O https://nginx.org/download/nginx-1.29.5.tar.gz
# tar -C /usr/local/src -xzf nginx-1.29.5.tar.gz
Le kell tölteni és ki kell csomagolni az ngx_http_geoip2 modult.
# curl -L -o ngx_http_geoip2_module.tar.gz https://github.com/leev/ngx_http_geoip2_module/archive/refs/tags/3.4.tar.gz
# tar -C /usr/local/src -xzf ngx_http_geoip2_module.tar.gz
=== Fordítás ===
Meg kell határozni a szükséges fordítási paramétereket, amelyek a futó nginx példányból kinyerhető. Ezt a hosszú karaktersorozatot használjuk a fordítás során.
# nginx -V 2>&1 | grep '^configure arguments:'
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_v3_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -flto=auto -ffat-lto-objects -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -fstack-protector-strong -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -march=x86-64-v2 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
Be kell lépni az nginx forráskódjának könyvtárába és el kell végezni a konfigurálást. A configure utasítás paramétere a korábban kiolvasott argumentumlista. Ehhez kell hozzáadni a modulhoz kapcsolódó plusz két kapcsolót.
* --with-compat
* --add-dynamic-module=/usr/local/src/ngx_http_geoip2_module-3.4
Az --add-dynamic-module= paramétere a használni kívánt modul forráskódjának könyvtára.
# cd /usr/local/src/nginx-1.29.5
# ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_v3_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -flto=auto -ffat-lto-objects -fexceptions -g -grecord-gcc-switches -pipe -Wall -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -Wp,-D_GLIBCXX_ASSERTIONS -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -fstack-protector-strong -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -march=x86-64-v2 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --with-compat --add-dynamic-module=/usr/local/src/ngx_http_geoip2_module-3.4
...
Configuration summary
+ using threads
+ using system PCRE2 library
+ using system OpenSSL library
+ using system zlib library
nginx path prefix: "/etc/nginx"
nginx binary file: "/usr/sbin/nginx"
nginx modules path: "/usr/lib64/nginx/modules"
nginx configuration prefix: "/etc/nginx"
nginx configuration file: "/etc/nginx/nginx.conf"
nginx pid file: "/var/run/nginx.pid"
nginx error log file: "/var/log/nginx/error.log"
nginx http access log file: "/var/log/nginx/access.log"
nginx http client request body temporary files: "/var/cache/nginx/client_temp"
nginx http proxy temporary files: "/var/cache/nginx/proxy_temp"
nginx http fastcgi temporary files: "/var/cache/nginx/fastcgi_temp"
nginx http uwsgi temporary files: "/var/cache/nginx/uwsgi_temp"
nginx http scgi temporary files: "/var/cache/nginx/scgi_temp"
Modul fordítása.
# make modules
...
make[1]: Leaving directory '/usr/local/src/nginx-1.29.5'
Az elkészült modul a /usr/local/src/nginx-1.29.5/objs alatt található ngx_http_geoip2_module.so néven. Ezt a modult kell az nginx szerverre másolni és betölteni.
==== GeoIP2 modul beállítása ====
Az NGINX szerveren engedélyezni kell a modult, majd el kell végezni a konfigurációt. A modul helye a /usr/share/nginx/modules, vagy a /usr/lib64/nginx/modules könyvtár.
Be kell tölteni a modult. Ehhez a /etc/nginx/nginx.conf fájl elejére be kell illeszteni a megfelelő load_module bejegyzést.
load_module modules/ngx_http_geoip2_module.so;
user nginx;
worker_processes auto;
...
A http szakaszban meg kell adni a geoip2 beállításokat. Ezt legegyszerűbben a /etc/nginx/conf.d könyvtárban elhelyezett drop-in fájl segítségével tudjuk megtenni.
# cat > /etc/nginx/conf.d/10-maxminddb.conf <<'EOF'
geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {
$geoip2_country_code country iso_code;
$geoip2_country_name country names en;
}
geoip2 /usr/share/GeoIP/GeoLite2-City.mmdb {
$geoip2_city_name city names en;
}
EOF
A default szerver szakaszban érdemes egy teszt location-t beállítani.
...
location /geoip-test {
add_header X-Country-Code $geoip2_country_code always;
add_header X-Country-Name $geoip2_country_name always;
return 200 "Country: $geoip2_country_name ($geoip2_country_code)\nCity: $geoip2_city_name\n";
}
...
Az nginx újraindítása után tesztelhető a működés:
$ curl https://server/geoip-test
Country: Hungary (HU)
City: Budapest