current position:Home>Nginx enables geoip2 module to realize national city identification - the road to dream

Nginx enables geoip2 module to realize national city identification - the road to dream

2022-06-24 08:44:31The road to dream

The latest version is recommended , Some data will change

#  install  api  library  1.6.0 edition 

tar -zxf libmaxminddb-1.6.0.tar.gz

cd libmaxminddb-1.6.0

./configure && sudo make && sudo make install

echo /usr/local/lib  >> /etc/ld.so.conf.d/local.conf

ldconfig

#  Download module 

wget  https://github.com/leev/ngx_http_geoip2_module/archive/refs/tags/3.4.tar.gz

#  compile nginx

./configure --prefix=/usr/local/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --add-module=/data/geoip2/ngx_http_geoip2_module-3.4

make  

make install

#  Configuration example 

#http modular 
# Identify the IP, This is not recommended 
#realip Value : if x-forward-for Not empty , Then take the first ip; Otherwise take remote_addr
map $http_x_forwarded_for $realip {
        ~^(\d+\.\d+\.\d+\.\d+) $1;
        default $remote_addr;
}
#http modular 
# introduce geoip2 Configuration file for , See the following section for the contents of the configuration file 
include /etc/nginx/conf.d/geoip2.conf;
#location modular 
# Put city and country information into header
#geoip2 The related variables are defined in the following section 
proxy_set_header Country_Code $geoip2_country_code;
proxy_set_header City_Code $geoip2_city_name_en;


#  add to geoip2 Basic configuration 

 decompression  GeoLite2-City_20220111.tar.gz GeoLite2-Country_20220111.tar.gz2 File , hold 2 individual .mmdb Format file into any directory ( Such as /data/geo2_db/)


# To configure geoip2 The configuration file  /etc/nginx/conf.d/geoip2.conf
geoip2 /data/geo2_db/GeoLite2-Country.mmdb {
         auto_reload 5m;
         $geoip2_metadata_country_build metadata build_epoch;
         # Country code 
         $geoip2_country_code source=$realip country iso_code;
         # Country English name 
         $geoip2_country_name_en source=$realip country names en;
         # Chinese name of the country 
         $geoip2_country_name_cn source=$realip country names zh-CN;
}
geoip2 /data/geo2_db/GeoLite2-City.mmdb {
      $geoip2_metadata_city_build metadata build_epoch;
      # English name of the city , Mostly Pinyin , There is repetition 
      $geoip2_city_name_en source=$realip city names en;
      # Chinese name of the city , Some cities do not have Chinese names 
      $geoip2_city_name_cn source=$realip city names zh-CN;
      # City id,maxmaind  In the database id, Non international standards 
      $geoip2_data_city_code source=$realip city geoname_id;
}


#   test 

# Add tests location, Return the geographical location to 
location = /geo {
        default_type text/plain;
       # return 200 "haha";
        return 200 'countryCode:$geoip2_country_code\n countryNameEn: $geoip2_country_name_en\n countryNameCn: $geoip2_country_name_cn\n cityNameEn: $geoip2_city_name_en\n cityNameCn: $geoip2_city_name_cn\n cityCode: $geoip2_data_city_code\n';
    }

# test nginx Whether the configuration is correct 
sudo /usr/local/nginx/sbin/nginx -t
# If there is no problem, it will output 
#nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
#nginx: configuration file /etc/nginx/nginx.conf test is successful

# start-up nginx
sudo /usr/local/nginx/sbin/nginx

# # No domain name can be used host test 
curl -H "X-Forwarded-For: 124.72.237.255,42.248.50.9,202.5.56.71" your.domain.com/geo

#nginx  Configuration example 

#user nobody;
worker_processes  2;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

load_module modules/ngx_http_geoip2_module.so;


events {
worker_connections  4096;
}


http {
include       mime.types;
default_type  application/octet-stream;

log_format  main ' 1234mutouren "$http_x_forwarded_for" - $remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'"$connection" "$request_time"'
' - "$geoip2_country_name_en" "$geoip2_city_name_en" '
' = "$geoip2_longitude" "$geoip2_latitude"' ;
#access_log logs/access.log main;
log_format json_logs escape=json
'{"@timestamp":"$time_iso8601",'
'"host":"$hostname",'
'"server_ip":"$server_addr",'
'"client_ip":"$remote_addr",'
'"remote_user":"$remote_user",'
'"xff":"$http_x_forwarded_for",'
'"domain":"$host",'
'"url":"$uri",'
'"referer":"$http_referer",'
'"upstreamtime":"$upstream_response_time",'
'"responsetime":"$request_time",'
'"request_method":"$request_method",'
'"status":"$status",'
'"size":"$body_bytes_sent",'
'"request_length":"$request_length",'
'"protocol":"$server_protocol",'
'"upstreamhost":"$upstream_addr",'
'"file_dir":"$request_filename",'
'"http_user_agent":"$http_user_agent",'
# Get the English name of the country 
'"geoip2_country_name_en":"$geoip2_country_name_en",'
'"number":"$status",'
# Get the English name of the city 
'"name":"$geoip2_city_name_en",'
# Get latitude and longitude 
'"logLat":"$geoip2_longitude,$geoip2_latitude"'
'}';

sendfile        on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout  300;
client_body_buffer_size 15M;
client_body_temp_path clientpath 3 2;
client_max_body_size 30M;
gzip  off;
gzip_static on;
gzip_min_length 10k;
gzip_buffers 4 16k;
gzip_comp_level 6;
gzip_types *;
gzip_disable "MSIE [1-6]\.";
gzip_vary on;
#  Configuration resolution IP Address , As a way to obtain geographic information IP Address :
map $http_x_forwarded_for $realip {
~^(\d+\.\d+\.\d+\.\d+) $1;
default $remote_addr;
}
#  Configure data files needed for country and city retrieval :
# test  mmdblookup --file /data/app/geo2db/GeoLite2-City.mmdb --ip 202.175.105.131
geoip2 /data/app/geo2db/GeoLite2-Country.mmdb {
auto_reload 5m;
$geoip2_metadata_country_build metadata build_epoch;
# Country code 
$geoip2_country_code source=$realip country iso_code;
# Country English name 
$geoip2_country_name_en source=$realip country names en;
# Chinese name of the country 
$geoip2_country_name_cn source=$realip country names zh-CN;
}
geoip2 /data/app/geo2db/GeoLite2-City.mmdb {
$geoip2_metadata_city_build metadata build_epoch;
# English name of the city , Mostly Pinyin , There is repetition 
$geoip2_city_name_en source=$realip city names en;
# Chinese name of the city , Some cities do not have Chinese names 
$geoip2_city_name_cn source=$realip city names zh-CN;
# City id,maxmaind  In the database id, Non international standards 
$geoip2_data_city_code source=$realip city geoname_id;
# longitude ,longitude
$geoip2_longitude source=$realip location longitude ;
# dimension ,latitude
$geoip2_latitude source=$realip location latitude ;
}

# HTTPS server
#
server {
listen 443 ssl;
server_name XXX.XXX.com;
#       server_name XXX.XXX.com;

ssl_certificate cert/data.com.pem;
ssl_certificate_key cert/data.com.key;

ssl_session_timeout 5m;
ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256::!MD5;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;

access_log /data/log/nginx/access.log json_logs;
location / {
try_files $uri $uri/ /index.html;
root /data/app/pos ;# Static resource directory 
index index.html index.htm;
}
location /api/pos/ {
proxy_pass http://10.50.XXX.XXX:9999/;
proxy_redirect default;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
location /WeChat {
alias /data/app/WeChat ;# Static resource directory 
index cfca.html;
}
}
server {
listen 80;
location /nginx_status {
stub_status on;
access_log off;
}
access_log logs/access.log json_logs;
#  test URI

location = /geo {
    default_type text/plain;
    return 200 'countryCode:$geoip2_country_code\n countryNameEn: $geoip2_country_name_en\n             countryNameCn: $geoip2_country_name_cn\n cityNameEn: $geoip2_city_name_en\n cityNameCn: $geoip2_city_name_cn\n cityCode: $geoip2_data_city_code\n';
}
}
}

#  test 

#curl -H "X-Forwarded-For: 124.72.XXX.255,42.248.50.9,202.5.XX.71" 10.XX.133.159/geo

countryCode:CN
countryNameEn: China
countryNameCn:  China 
cityNameEn: xxzhou
cityNameCn: XX City 
cityCode: 18x0821

 

  Reference material :

Nginx Enable Geoip2, Realize the State 、 City identification - Simple books

Nginx Of geo2 Module to achieve access to geographic information

Set up log aggregation grafana&loki collect Nginx journal _Zz_ Tang Xiaoqi's blog -CSDN Blog _grafana nginx journal

Grafana Display exquisite nginx Access log charts _code Small blog technology blog _51CTO Blog  

Nginx To configure GeoIP Library or directly through modification Logstash Write the log to ES - Let's have a look  

grafana Exhibition ES Medium nginx journal - Map display _ Cong's technology blog _51CTO Blog  

https://www.jb51.net/article/241689.htm 

Nginx GEOIP Statistic dashboard for Grafana | Grafana Labs

grafana+elk monitor nginx | Broken Yuxuan · The Chinese clothes are soft around the fingers  

copyright notice
author[The road to dream],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/175/202206240622199467.html

Random recommended