nginx安装GeoIP模块,屏蔽特定地区IP

作者: 麻辣阁 分类: wordpress 发布时间: 2018-12-03 19:18

查看nginx 编译选项, 确认默认是否已经安装 GeoIP模块

 nginx -V

nginx 官方还没有正式支持 GeoIP2, 如果需要支持, 需要自己编译. 如下安装时针对 GeoIP 老版, 按官方的提示, 2019年初会移除提供老版本的数据, 只提供新格式的 GeoIP2数据.

如果没有 geoip相关模块,安装:

yum install nginx-module-geoip
----------------------------------------------------------------------

The GeoIP dynamic modules for nginx have been installed.
To enable these modules, add the following to /etc/nginx/nginx.conf
and reload nginx:

    load_module modules/ngx_http_geoip_module.so;
    load_module modules/ngx_stream_geoip_module.so;

Please refer to the modules documentation for further details:
http://nginx.org/en/docs/http/ngx_http_geoip_module.html
http://nginx.org/en/docs/stream/ngx_stream_geoip_module.html

----------------------------------------------------------------------

按上述安装提示, 编辑 /etc/nginx/nginx.conf, 添加 GeoIP相关的行,重启 nginx:

user  nginx;
worker_processes  1;

# GeoIP
load_module modules/ngx_http_geoip_module.so;
load_module modules/ngx_stream_geoip_module.so;

参考:

安装geoipupdate

yum groupinstall 'Development Tools'
yum install libcurl-devel zlib-devel
cd ~
git clone https://github.com/maxmind/geoipupdate
cd geoipupdate
./bootstrap
./configure
make
make install

安装后检查一下:

which geoipupdate
/usr/local/bin/geoipupdate
/usr/local/bin/geoipupdate -V
geoipupdate 3.1.1

配置

参考 https://dev.maxmind.com/geoip/geoipupdate/, vi /etc/GeoIP.conf

# The following AccountID and LicenseKey are required placeholders.
# For geoipupdate versions earlier than 2.5.0, use UserId here instead of AccountID.
AccountID 0
LicenseKey 000000000000

# Include one or more of the following edition IDs:
# * GeoLite2-City - GeoLite 2 City
# * GeoLite2-Country - GeoLite2 Country
# For geoipupdate versions earlier than 2.5.0, use ProductIds here instead of EditionIDs.
EditionIDs GeoLite2-City GeoLite2-Country

执行更新:

/usr/local/bin/geoipupdate -d /usr/share/GeoIP/

/usr/share/GeoIP/ 目录下会多两个文件:

GeoLite2-City.mmdb
GeoLite2-Country.mmdb

发表评论

电子邮件地址不会被公开。 必填项已用*标注