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:31【The road to dream】
- geoip2_module https://github.com/leev/ngx_http_geoip2_module/releases
- libmaxminddb(api library ) https://github.com/maxmind/libmaxminddb/releases
- DB https://www.maxmind.com/en/account/login Select after login
Download Databases
Downloadable
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
Grafana Display exquisite nginx Access log charts _code Small blog technology blog _51CTO Blog
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
The sidebar is recommended
- On BOM and DOM (3): DOM node operation - element style modification and DOM content addition, deletion, modification and query
- Embrace cloud native, Tencent releases TCSs container security service!
- Is there any way for FC in react+ts to set the component of function keyword
- How to write the V-model of vue2/3 custom components?
- Error using vuescoll!
- When HTML accesses the parent directory, the file content cannot be displayed normally in the browser after reading
- How to embed tabelau in the front page of react
- Vue component communication data does not update rendering
- It is very convenient to use ECs to build a drawing bed and hexo one click deployment
- How does webstorm set the bootstrap auto prompt
guess what you like
Nginx cross domain understanding, simulation and solution
Front page exercise
How to hide the value in El form item in Vue and set the value of input box to be read-only
Springboot+vue to realize campus second-hand Mall (graduation design I)
Node. JS installation
Summary of 2022 blue team HW elementary interview questions
Screenshot of angular page (scroll bar displays completely)
JavaScript object field splicing
How to repair garbled code in HTML
Display CSV as a table in JavaScript (simple example)
Random recommended
- How to read files using JavaScript
- Example of JavaScript FileReader API processing files
- Springboot+vue project tourism information recommendation system [source code open source]
- Ultra wideband pulse positioning scheme, UWB precise positioning technology, wireless indoor positioning application
- Elementui used tabs to step on the pit
- Ant Design Vue - remove the input component border and the border blue shadow effect when getting focus
- Get httponly protected cookies across sub domains through XSS
- Coding specification - Vue project
- Code writing specification - Javascript
- Code specification - HTML
- Code writing specification - CSS
- CSS Notes 6
- Quickly set up PgSQL for serverless
- 404405 troubleshooting after Vue uses proxy packaging
- Vue3 TS learning
- What is the charm of python, which is warmly pursued by countless programmers?
- HTML Basics
- CSS Foundation
- CSS (PC side) three methods of traditional page layout
- Inline element, block element, inline block element
- 19、 Lua garbage collection of enterprise rapid development platform spring cloud+spring boot+mybatis+elementui
- Detailed introduction to render props and high-level component hoc in react tutorial
- Introduction to high order components (HOC) in react tutorial
- Front end and back end love and hate -- sequel
- When Vue adds on-demand loading to the UI library and starts the project, an error is reported in babel-preset-es2015
- Select in element uses typescript to define initial data, which has no effect
- Vue video player of Vue player plug-in
- React configuration component path reference @ to represent SRC root path
- Use less and CSS modules in react scaffold Engineering
- JavaScript plug-in download instructions and introduction of the web front end, NPM, install, save, and require
- Plug in download instructions and introduction of Vue in the web front end, NPM, install, save
- Summary of front-end common interview questions
- JavaScript automatically adds tag UTF-8 encoding
- Summary of three for loop statements in front-end JavaScript
- Vue hook function
- Monitor page rotation in Vue, and display in full screen on echarts mobile terminal
- Webpack learning notes series 07- how it works
- Webpack learning notes series 06- Packaging Optimization
- Webpack learning notes series 08-hmr hot update
- [Vue] understanding and thinking of new features based on Vue 3