Server and Web Integrator
Kloxo-MR logo
6.5.0 or 7.0.0
Click for "How to install"
Kloxo-MR is open-source.
Donate and or Sponsorship always welcome.
Click to:
Click Here
Please login or register. 2024-06-18, 09:04:07

Author Topic: How to configure nginx geoIP logs  (Read 15264 times)

0 Members and 1 Guest are viewing this topic.

Offline Spacedust

  • Super Grand Master
  • ****
  • Posts: 4,050
  • Karma: +1/-0
    • View Profile
How to configure nginx geoIP logs
« on: 2021-07-15, 21:54:47 »
Install nginx-module-geoip:

Code: [Select]
yum -y install nginx-module-geoip
Download GeoIP files:

Code: [Select]
mkdir /etc/nginx/geoip;
cd /etc/nginx/geoip;
wget https://mailfud.org/geoip-legacy/GeoIP.dat.gz
wget https://mailfud.org/geoip-legacy/GeoIPCity.dat.gz
gunzip GeoIP.dat.gz;
gunzip GeoIPCity.dat.gz

Add script to update GeoIP files:

Code: [Select]
cd ~;
wget https://mailfud.org/geoip-legacy/geoip_update.sh;

Change lines:

Code: [Select]


Code: [Select]

Add a line to /var/spool/cron/root:

Code: [Select]
* 3 * * * sh /root/geoip_update.sh
You can run the script manually to check if all is working fine:

Code: [Select]
sh /root/geoip_update.sh
It should respond with something like this:

Code: [Select]
2021-07-15 22:36:59 URL:https://mailfud.org/geoip-legacy/GeoIP.dat.gz [1076511/1076511] -> "GeoIP.dat.gz" [1]
GeoIP.dat is up to date
2021-07-15 22:37:00 URL:https://mailfud.org/geoip-legacy/GeoIPCity.dat.gz [17027045/17027045] -> "GeoIPCity.dat.gz" [1]
GeoIPCity.dat is up to date

Add in /etc/nginx/nginx.conf line on top and copy the file to /opt/configs/nginx/etc/conf/custom.nginx.conf:

Code: [Select]
load_module "modules/ngx_http_geoip_module.so";
and also at the end before "include" these lines:

Code: [Select]
geoip_country /etc/nginx/geoip/GeoIP.dat; # the country IP database
geoip_city /etc/nginx/geoip/GeoIPCity.dat; # the city IP database

Add the following lines to /etc/nginx/fastcgi_params and copy the file to /opt/configs/nginx/etc/conf/custom.fastcgi_params before # PHP only line:

Code: [Select]
### SET GEOIP Variables ###
fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3;
fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;

fastcgi_param GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;
fastcgi_param GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;
fastcgi_param GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;
fastcgi_param GEOIP_REGION $geoip_region;
fastcgi_param GEOIP_CITY $geoip_city;
fastcgi_param GEOIP_POSTAL_CODE $geoip_postal_code;
fastcgi_param GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;
fastcgi_param GEOIP_LATITUDE $geoip_latitude;
fastcgi_param GEOIP_LONGITUDE $geoip_longitude;

Change the following lines in /etc/nginx/conf.d/~lxcenter.conf and copy the file to /opt/configs/nginx/etc/conf.d:

Code: [Select]
   log_format main '$remote_addr - $remote_user [$time_local] "$request" '
        '$status $body_bytes_sent "$http_referer" '
        '"$http_user_agent" "$http_x_forwarded_for"';


Code: [Select]
  log_format main '$remote_addr - $remote_user [$time_local] "$request" '
        '$status $body_bytes_sent "$http_referer" '
        '"$http_user_agent" "$http_x_forwarded_for" '
        '"$geoip_country_name" "$geoip_country_code" ';

Finally restart nginx with:

Code: [Select]
service nginx restart
Now you can try checking your logs and you will something like this. At the end you will notice country and country code is listed e.g. "United Kingdom" "GB":

Code: [Select] - - [15/Jul/2021:21:49:15 +0200] "GET /p3700/Clipboard02.jpg HTTP/1.1" 200 71231 "https://vi.vipr.ebaydesc.com/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36" "-" "United Kingdom" "GB"
If you use IPv6 on your server you need to also use GeoIPv6 and GeoIPCityv6.
« Last Edit: 2021-07-15, 22:41:21 by Spacedust »


MRatWork Affiliates:    BIGRAF(R) Inc.    House of LMAR    EFARgrafix

Page created in 0.071 seconds with 17 queries.

web stats analysis