给博客开启geoip2服务,用于屏蔽一些国家或地区访问,查看本机IP详细信息等等。主要是,安装libmaxminddb模块,Nginx编译geoip2模块,配置Nginx,下面是详细过程。
安装libmaxminddb模块
首先是libmaxminddb的GitHub:https://github.com/maxmind/libmaxminddb
这里不要直接git clone
,因为俺发现master有bug,可以到Latest release里下载编译。
wget -c https://github.com/maxmind/libmaxminddb/releases/download/1.4.2/libmaxminddb-1.4.2.tar.gz -O - | tar -xz cd libmaxminddb-1.4.2 ./configure make make check make install ldconfig
如果以上的步骤没有出错,libmaxminddb就安装好了,可以输入mmdblookup看看是否安装成功。
mmdblookup --version
下载ngx_http_geoip2_module
下载ngx_http_geoip2_module,是为了把插件编译进Nginx中以支持geoip2
GitHub地址:https://github.com/leev/ngx_http_geoip2_module
直接git下载:
git clone --recursive https://github.com/leev/ngx_http_geoip2_module.git
编译Nginx
首先,还是Nginx的官方下载页:
https://nginx.org/en/download.html
主要步骤是打入刚刚下载的ngx_http_geoip2_module模块,下面是编译过程:
#下载并解压最新nginx源码 wget -c https://nginx.org/download/nginx-1.19.0.tar.gz -O - | tar -xz cd nginx-1.19.0 #带插件编译nginx,这一步的路径和插件最好沿用老nginx的,以便无缝升级,编译前应停止老版nginx #nginx -V 查看老nginx路径和插件,复制下内容,然后加上ngx_http_geoip2_module模块就可以编译了 #注意ngx_http_geoip2_module文件夹的路径 ./configure --prefix=$PWD --add-dynamic-module=/root/ngx_http_geoip2_module make #最后一步make install是为了把文件打入指定路径,非必须执行 make install
编译完成后,切换到objs目录,可以看到我们想要的两个库文件:
ngx_http_geoip2_module.so ngx_stream_geoip2_module.so
把这两个文件移动到/usr/lib/nginx/modules路径下:
cd /nginx-1.19.0/objs mv ngx_http_geoip2_module.so /usr/lib/nginx/modules mv ngx_stream_geoip2_module.so /usr/lib/nginx/modules
配置Nginx
配置Nginx前,要先从maxmind的官网上下载GeoLite2 City的数据库,因为geoip2要收费,所以只能下载geolite2,并且需要注册下载。
maxmind的官网:https://dev.maxmind.com/geoip/geoip2/geolite2/
注:maxmind官方目前没有找到免费的ISP库,所以以下操作只基于City和Country库来进行。
主要是下载GeoLite2-City.mmdb
和GeoLite2-Country.mmdb
文件,由于需要注册下载,下面下载链接作为演示用。
#新建geoip文件夹,放置mmdb文件 mkdir /etc/geoip cd /etc/geoip #下载俩个文件并解压,下面下载链接是演示用的,是无效链接,需要正确下载链接可以去官网注册获取链接 wget https://geolite.maxmind.com/download/GeoLite2-City.tar.gz -O - | tar -xz
使用mmdblookup命令在库中搜索试试:
mmdblookup -f /etc/geoip/GeoLite2-City.mmdb -i 141.164.50.53
接下来就是配置nginx.conf文件了。
查看IP地址服务
在nginx.conf中第一行加入下面内容,用来引用ngx_http_geoip2_module模块:
load_module /usr/lib/nginx/modules/ngx_http_geoip2_module.so;
接下来在http代码段中加入以下内容,通过ngx_http_geoip2_module访问ip获得相应的geo信息。
geoip2 /etc/geoip/GeoLite2-City.mmdb { auto_reload 30m; $geoip2_data_city_name source=$real_ip city names zh-CN; } #设置变量和语言 geoip2 /etc/geoip/GeoLite2-Country.mmdb { auto_reload 30m; $geoip2_data_country_name source=$real_ip country names zh-CN; $geoip2_data_country_code country iso_code; }
然后在nginx里的server代码段加入下面内容:
location = /ip { default_type text/plain; set $real_ip $remote_addr; if ($http_x_real_ip != "") { set $real_ip $http_x_real_ip; } return 200 "$real_ip $geoip2_data_country_name $geoip2_data_city_name"; }
重启nginx后,就可以在域名后面加上/ip
来查看自己的IP及地域信息了。
演示地址:https://1kb.day/ip
Linux下IP查询:curl https://1kb.day/ip
当然,你还可以美化一下这个前端:
https://1kb.day/ip.html
限制国家访问
如果需要禁止某些国家访问,比如说中国,只需要在http代码段添加下面内容:
geoip2 /etc/geoip/GeoLite2-Country.mmdb { #下面这行可以直接添加到上面的GeoLite2-Country.mmdb变量框里,上面已经添加,这是单独的示例 $geoip2_data_country_code country iso_code; } map $geoip2_data_country_code $allowed_country { default yes; #默认允许访问 CN no; #禁止 CN 访问 }
然后在server代码段添加下面内容即可:
location / { if ($allowed_country = no) { #判断等于no的国家,这里是阻止中国访问,如果阻止国外访问,只需要把no改成yes就好了 return 404; #直接返回404 } }
其它用法待开发中,最后,提醒一下,如果使用本页中的代码,请删除掉注释,避免出错!
文章评论