ACQUIRING IMAGE…

LOG ENTRYCAT / 教程

Nginx中使用GeoIP实现对国内用户的重定向

介绍如何通过 Nginx 的 GeoIP2 模块配合 MaxMind 数据库,实现根据用户地理位置进行重定向。

经验分享

LOADING DATABASE
由于众所周知的原因,国内用户访问站点位置在海外的服务器时,可能会遇到访问速度慢的问题。为了解决这个问题,可以通过Nginx的GeoIP模块实现对国内用户的重定向。
为什么不是CDN?有米我也想用~

GeoIP 模块安装

git clone https://github.com/leev/ngx_http_geoip2_module.git

sudo yum -y install libmaxminddb0 libmaxminddb-dev mmdb-bin

./configure --add-module=/path/to/ngx_http_geoip2_module
make
make install
如果使用的是lnmp一键安装包,可以将 --add-module=/path/to/ngx_http_geoip2_module 放到 lnmp.confNginx_Modules_Options= 后方,之后使用 sudo sh upgrade.sh nginx 即可。

GeoIP 数据库

参考:https://dev.maxmind.com/geoip/updating-databases
  • 首先我们需要去 https://www.maxmind.com/en/home 注册账号
  • https://github.com/maxmind/geoipupdate/releases 下载 geoipupdate 工具
  • 安装到用户工具目录 sudo cp geoipupdate_5.0.0_linux_amd64/geoipupdate /usr/local/bin
  • 创建配置文件 /usr/local/etc/GeoIP.conf
# GeoIP.conf file - used by geoipupdate program to update databases# from http://www.maxmind.comAccountID YOUR_ACCOUNT_ID_HERELicenseKey YOUR_LICENSE_KEY_HEREEditionIDs YOUR_EDITION_IDS_HERE
其中 LicenseKey 可以在 https://www.maxmind.com/en/accounts/current/license-key 中找到。