The Nginx and GeoIP modules read the location information method where IP is located
- 2020-05-17 07:46:47
- OfStack
GeoIP Linux installation
yum install nginx-module-geoip
http_geoip_module usage scenarios
1. Distinguish between domestic and foreign HTTP access rules
2. Make HTTP access rules for domestic urban areas
After installing yum, find the installed module files
If nginx is installed with yun, it is usually installed under the /etc/nginx/modules/ directory
Note: if nginx is not installed by yum but is installed by source code compilation and installation, nginx needs to be reinstalled and compiled once, then you don't need to manually add this module.
Manual input module
Loading the module and http at the head of the nginx.conf configuration file is at the same level
load_module "modules/ngx_http_geoip_module.so";
load_module "modules/ngx_stream_geoip_module.so";
Because GeoIP provides database files based on MaxMind to read the geographic information, you need to download the geographic files of ip.
This database is in base 2 and cannot be opened with a text editor. You need the GeoIP library above to read it.
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz # Region of country IP
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz # Urban area IP
Then unpack
load_module "modules/ngx_http_geoip_module.so";
load_module "modules/ngx_stream_geoip_module.so";
.......
http{
geoip_country /etc/nginx/geoip/GeoIP.dat; # Load state IP
geoip_city /etc/nginx/geoip/GeoLiteCity.dat; # Load the city IP
.........
server
{
......
location / {
# Judge if it is not Chinese and return 403;
if ($geoip_country_code != CN) {
return 403;
}
}
# Return to country city information
location /myip {
default_type text/plain;
return 200 "$remote_addr $geoip_country_name $geoip_country_code $geoip_city";
}
....
}
}
Then visit your IP address /myip to return information about the country and city where IP lives.