Home >Operation and Maintenance >Nginx >How Nginx and GeoIP modules read the geographical information of IP
linux installation geoip
yum install nginx-module-geoip
http_geoip_module usage scenario
1. Difference between domestic and foreign http access rules
2 , Differentiate http access rules between domestic cities and regions
yum After installation, find the installed module file
If nginx is installed with yun, it is usually installed in the /etc/nginx/modules/ directory
Note: If nginx is not installed by yum but compiled and installed from source code, you need to re-install and compile nginx and add this module. Then there is no need to add this module manually.
Manually add the module
The module loaded in the header of the nginx.conf configuration file is at the same level as http
load_module "modules/ngx_http_geoip_module.so"; load_module "modules/ngx_stream_geoip_module.so";
Because geoip is Based on maxmind, a database file is provided to read regional information, so you need to download the regional file of IP.
This database is binary and cannot be opened with a text editor. It requires the above geoip library to read.
wget http://geolite.maxmind.com/download/geoip/database/geolitecountry/geoip.dat.gz #国家的地域ip wget http://geolite.maxmind.com/download/geoip/database/geolitecity.dat.gz #城市的地域ip
Then decompress
load_module "modules/ngx_http_geoip_module.so"; load_module "modules/ngx_stream_geoip_module.so"; ....... http{ geoip_country /etc/nginx/geoip/geoip.dat; #加载国家ip geoip_city /etc/nginx/geoip/geolitecity.dat; #加载城市ip ......... server { ...... location / { #判断如果不是中国的就返回403; if ($geoip_country_code != cn) { return 403; } } #返回国家城市信息 location /myip { default_type text/plain; return 200 "$remote_addr $geoip_country_name $geoip_country_code $geoip_city"; } .... } }
and then visit your ip address/myip to return the country and city information where the ip is located.
The above is the detailed content of How Nginx and GeoIP modules read the geographical information of IP. For more information, please follow other related articles on the PHP Chinese website!