preface
Recently, we set up A set of AB sites (not Acfun and Bilibili, AB site: the AB site in the article refers to the same domain name, which can return two different resources). The clients mainly promote Google and FaceBook. A site is for the examination and platform inspection, and B site is the target site to promote Japan. Japanese users who visit www.abc.com see station B, and non-Japanese users who visit www.abc.com see station A.
I came up with three solutions and finally decided to use Nginx+GeoIP2
- Square root Nginx + GeoIP2
- You can get information about the country and city that requested the IP
- Allows developers to make various personalized Nginx configurations for the requested IP
- The geographic location of the requested IP can be passed to the PHP program via php-fpm
- MaxMind free database (geolite2-country-mmDB + geolite2-city.mmdb) is updated regularly to complete a perfect closed loop
- Founded in 2002, MaxMind is reliable
- * The use of IP identification interface: stable need to charge (can not guarantee 100% high availability: frequency limit, response time, interface anomalies and other factors), free cannot guarantee stability, interface is far from the local stability of GeoLite data
- Cloudflare charges slightly more, domestic CloudXNS has turned off its free service.
TIPS:
- Most of the Internet is the old version of GeoIP, which is no longer applicable. GeoIP relies on MaxMind’S IP data and needs frequent updates (automatic scripts update regularly).
- Friends who are interested in deploying and not lazy can skip right to the last docker experience
Author environment (2020.05.19)
- CentOS7.2
- Libmaxminddb 1.3.2
- Nginx 1.14.2
Install GeoIP2 dependencies
- The official making
- Official Download address
Wget $$tar ZXVF - https://github.com/maxmind/libmaxminddb/releases/download/1.3.2/libmaxminddb-1.3.2.tar.gz Libmaxminddb - 1.3.2. Tar. Gz $cdLibmaxminddb -1.3.2 $./configure && make && make install $echo /usr/local/lib >> /etc/ld.so.conf.d/local.conf
$ ldconfig
Copy the code
Download the GeoIP data
- Official Download address
- Github download address
$ git clone https://github.com/ar414-com/nginx-geoip2
$ cd nginx-geoip2
$ tar -zxvf GeoLite2-City_20200519.tar.gz
$ mv ./GeoLite2-City_20200519/GeoLite2-City.mmdb /usr/share/GeoIP/
$ tar -zxvf GeoLite2-Country_20200519.tar.gz
$ mv ./GeoLite2-Country_20200519/GeoLite2-Country.mmdb /usr/share/GeoIP/
$ # /usr/share/geoip = /usr/ share/geoip
$ # Nginx configuration requires data paths
Copy the code
Compile the GeoIP2 module into Nginx
Download the GeoIP2 module
$ cd ~
$ git clone https://github.com/ar414-com/nginx-geoip2
Copy the code
Compile to an installed Nginx
Nginx -v > –prefix=/ WWW /server/nginx
2. Add the previous compilation parameters, and then add the module parameters of Geoip2
–add-module=/root/nginx-geoip2/ngx_http_geoip2_module
$ cd/www/server/nginx $ ./configure --user=www --group=www \ --prefix=/www/server/nginx \ --with-openssl=/www/server/nginx/src/openssl \ --add-module=/www/server/nginx/src/ngx_devel_kit \ --add-module=/www/server/nginx/src/lua_nginx_module \ --add-module=/www/server/nginx/src/ngx_cache_purge \ --add-module=/www/server/nginx/src/nginx-sticky-module --add-module=/www/server/nginx/src/nginx-http-concat \ --with-http_stub_status_module --with-http_ssl_module \ --with-http_v2_module --with-http_image_filter_module \ --with-http_gzip_static_module --with-http_gunzip_module \ --with-stream --with-stream_ssl_module --with-ipv6 \ --with-http_sub_module --with-http_flv_module \ --with-http_addition_module --with-http_realip_module \ --with-http_mp4_module --with-ld-opt= -wl, -e --with-pcre= pcl-8.40 \ --with-ld-opt=-ljemalloc \ --add-module=/root/nginx-geoip2/ngx_http_geoip2_module $ make $ rm-f /www/server/nginx/sbin/nginx.old
$ mv /www/server/nginx/sbin/nginx /www/server/nginx/sbin/nginx.old
$ cp ./objs/nginx /www/server/nginx/sbin/nginx
$ make upgrade
$ Check whether the installation is successful
$ nginx -V
Copy the code
Reinstall Nginx
Simple installation, easy to test
$wget https://nginx.org/download/nginx-1.14.2.tar.gz $tar ZXVF nginx - 1.14.2. Tar. Gz $cd/configure --user= WWW --group= WWW --prefix=/ WWW /server/nginx \ --add-module=/root/nginx-geoip2/ngx_http_geoip2_module $ make && make installCopy the code
Using GeoIP2
http{
...
geoip2 /usr/local/share/GeoIP/GeoLite2-Country.mmdb {
$geoip2_country_code country iso_code;
}
map $geoip2_country_code $is_jp_country {
default no;
JP yes;
}
server {
listen 80;
server_name localhost;
Add a response header for easy debugging
add_header country $geoip2_country_code;
location / {
set $rootpath html/a;
if ($is_jp_country = no) {
set $rootpath html/b;
}
add_header rootpath $rootpath;
add_header country $geoip2_country_code;
root $rootpath; index index.html index.htm; }}}Copy the code
The docker experience
Interested and lazy friends can be directly uploaded by the author of the mirror experience
Access to the mirror
$ docker pull ar414/nginx-geoip2
Copy the code
run
$ docker run -it -d -p 80:80 -p 443:443 --rm ar414/nginx-geoip2
Copy the code