简述
使用nginx采用GeoIP库来实现IP的判断。
GeoIP库可以获取一个IP的国家和城市等等信息,由 maxmind 提供。提供免费版本与收费版本。并且每月第一周的周二都会更新(更新后下载可自行替换字典)。
GeoIP不仅可以用nginx来做屏蔽与国际化,还可以与php,c,java,c# 等语言使用。
GeoIP库有GeoIP与GeoIP2。GeoIP为旧版本(不推荐使用), GeoIP2新版本(推荐使用)。且GeoLite2是GeoIP2的免费版。见: https://dev.maxmind.com/zh-hans/
使用
1. http_geoip_module 模块
因为要用到 http_geoip_module 模块,系统自带的 nginx 一般不带这个模块,所以要下载 nginx 源代码后自行编译(安装不作为重点,这里将简单描述,更详细的安装过程请参考目录中的安装过程)。
$ wget http://nginx.org/download/nginx-0.9.6.tar.gz$ tar zxvf nginx-0.9.6.tar.gz$ cd nginx-0.9.6$ # 如果已有nginx还是先根据已装的库做更新,具体方法百度$ ./configure --without-http_empty_gif_module --with-poll_module--with-http_stub_status_module --with-http_ssl_module--with-http_geoip_module$ make; make install
2. 安装 GeoIP 库
$ wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz$ tar -zxvf GeoIP.tar.gz$ cd GeoIP-1.4.6$ ./configure$ make; make install
库自动安装到 /usr/local/lib 下,所以这个目录需要加到动态链接配置里以便运行相关程序的时候能自动绑定到这个 GeoIP 库:
$ echo '/usr/local/lib' >/etc/ld.so.conf.d/geoip.conf$ ldconfig
3. 下载 IP 数据库
MaxMind 提供了免费的 IP 地域数据库(GeoIP.dat),不过这个数据库文件是二进制的,需要用 GeoIP 库来读取,所以除了GeoIP 外,还要下载 GeoIP.dat 文件。
可前往下载最新的二进制dat库 https://dev.maxmind.com/zh-hans/geoip/legacy/geolite/

$ wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz$ gunzip GeoIP.dat.gz
4. 配置ginx
# 一个最简单的例子http {geoip_country /home/vpsee/GeoIP.dat;fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3;fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;}server {listen 9999;server_name localhost;location / {if ($geoip_country_code != CN) {return 403;}}}
1. ngx_http_geoip2_module 模块
因为要用到 ngx_http_geoip2_module 模块,系统自带的 nginx 一般不带这个模块,所以要下载 nginx 源代码后自行编译(安装不作为重点,这里将简单描述,更详细的安装过程请参考目录中的安装过程)。
$ # 获取ngx_http_geoip2_module$ cd /usr/local/src$ # 如果安装过git命令可直接使用,无git命令可先下载后上传到指定文件夹也可。$ git clone --recursive https://github.com/leev/ngx_http_geoip2_module$ wget http://nginx.org/download/nginx-0.9.6.tar.gz$ tar zxvf nginx-0.9.6.tar.gz$ cd nginx-0.9.6$ # 如果已有nginx还是先根据已装的库做更新,具体方法百度$ ./configure --without-http_empty_gif_module --with-poll_module--with-http_stub_status_module--with-http_ssl_module--add-module=/usr/local/src/ngx_http_geoip2_module$ make; make install
2. 安装 libmaxminddb 依赖库
需要先安装libmaxminddb依赖库。libmaxminddb是一个C库文件,用于读取MaxMind DB文件,包括MaxMind下的GeoIP2数据文件。
$ cd /usr/local/src$ # 如果安装过git命令可直接使用,无git命令可先下载后上传到指定文件夹也可。$ git clone --recursive https://github.com/maxmind/libmaxminddb$ cd libmaxminddb$ ./bootstrap$ ./configure$ make$ make install$ sh -c "echo /usr/local/lib >> /etc/ld.so.conf.d/local.conf"$ ldconfig
3. 下载 IP 数据库
MaxMind 提供了免费的 IP 地域数据库,这个数据库是二进制的,不能用文本编辑器打开,需要上面的 GeoIP 库来读取:
可前往下载最新的二进制MaxMind DB库:
https://dev.maxmind.com/zh-hans/geoip/geoip2/geolite2-%e5%bc%80%e6%ba%90%e6%95%b0%e6%8d%ae%e5%ba%93/
$ wget http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz$ gunzip GeoLite2-Country.mmdb.gz
4. 配置nginx
# 一个最简单的例子http {#国家geoip2 /usr/local/nginx/conf/GeoLite2-Country.mmdb {$geoip2_data_country_code default=US country iso_code;$geoip2_data_country_name country names en;}#城市geoip2 /usr/local/nginx/conf/GeoLite2-City.mmdb {$geoip2_data_city_name default=London city names en;# subdivisions这里有个数组,所以写法有别于city$geoip2_data_province_name subdivisions 0 names en;$geoip2_data_province_isocode subdivisions 0 iso_code;}}server {listen 9999;server_name localhost;location / {if ($geoip2_data_country_code != CN) {return 403;}}}
