Home > Operation and Maintenance > Nginx > How Nginx and GeoIP modules read the geographical information of IP

How Nginx and GeoIP modules read the geographical information of IP

WBOY
Release: 2023-05-14 15:10:06
forward
1783 people have browsed it

linux installation geoip

yum install nginx-module-geoip
Copy after login

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";
Copy after login

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
Copy after login

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";
 }



....
 }
}
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template