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

分类
教程
标签
经验分享

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

GeoIP 模块安装

1git clone https://github.com/leev/ngx_http_geoip2_module.git 2 3sudo yum -y install libmaxminddb0 libmaxminddb-dev mmdb-bin 4 5./configure --add-module=/path/to/ngx_http_geoip2_module 6make 7make 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
1# 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 中找到。