Nginx配置GeoIP2模块支持IP鉴权
1. 卸载旧版本并安装依赖包
$ yum -y remove geoipupdate geoip
$ yum -y install mercurial unix2dos dos2unix gcc gcc-c++ perl-ExtUtils-Embed readline-devel pcre pcre-devel openssl openssl-devel perl perl-devel curl libxml2 libxml2-dev libxslt-devel gd-devel zlib-devel gperftools redhat-rpm-config.noarch
2. 在https://github.com/maxmind/geoipupdate/releases下载对应的rpm文件,
$ rpm -i geoipupdate_4.6.0_linux_amd64.rpm
完成安装后,可通过geoipupdate -V 查看版本
3. 更新/etc/GeoIP.conf文件中的AccountID和LicenseKey,执行geoipupdate,完成数据文件的下载
下载的数据文件默认在/usr/share/GeoIP目录,包含GeoLite2-ASN.mmdb,GeoLite2-City.mmdb,GeoLite2-Country.mmdb文件
4. 安装libmaxminddb
$ git clone --recursive https://github.com/maxmind/libmaxminddb
$ cd libmaxminddb
$ ./bootstrap
$ ./configure
$ make
$ make check
$ make install
编辑/etc/ld.so.conf中加入一行 /usr/local/lib
$ ldconfig
-----或者使用下面的tar包方式-------------------
$ wget https://github.com/maxmind/libmaxminddb/releases/download/1.5.2/libmaxminddb-1.5.2.tar.gz
$ tar xzvf libmaxminddb-1.5.2.tar.gz
$ cd libmaxminddb-1.5.2
$ ./configure
$ make
$ make check
$ make install
编辑/etc/ld.so.conf中加入一行 /usr/local/lib
$ ldconfig
如果以上的步骤没有出错,我们的libmaxminddb就安装好了。我们可以输入mmdblookup看看是否安装成功。
$ mmdblookup --version
mmdblookup version 1.5.2
5. 下载ngx_http_geoip2_module的源码
下载链接:https://github.com/leev/ngx_http_geoip2_module/releases
$ wget https://codeload.github.com/leev/ngx_http_geoip2_module/tar.gz/3.3/ngx_http_geoip2_module-3.3.tar.gz
$ tar xzvf ngx_http_geoip2_module-3.3.tar.gz
6. 需要获得与当前nginx版本相同的源码
$ nginx -v
nginx version: nginx/1.14.0
$ wget http://nginx.org/download/nginx-1.14.0.tar.gz
$ tar xzvf nginx-1.14.0.tar.gz
$ cd nginx-1.14.0
7. 确认当前nginx的编译参数
$ nginx -V
nginx version: nginx/1.14.0 (Ubuntu)
built with OpenSSL 1.1.0g 2 Nov 2017
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-mcUg8N/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module
在最后面加上一个编译参数:
--add-dynamic-module=/home/ngx_http_geoip2_module-3.3
$ ./configure --with-cc-opt='-g -O2 -fdebug-prefix-map=/build/nginx-mcUg8N/nginx-1.14.0=. -fstack-protector-strong -Wformat -Werror=format-security -fPIC -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -fPIC' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --modules-path=/usr/lib/nginx/modules --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_v2_module --with-http_dav_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_geoip_module=dynamic --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_sub_module --with-http_xslt_module=dynamic --with-stream=dynamic --with-stream_ssl_module --with-mail=dynamic --with-mail_ssl_module --add-dynamic-module=/home/ngx_http_geoip2_module-3.3
$ make
$ make modules
编译完成后,我们切换到objs目录,可以看到我们想要的两个库文件,接下来我们把这两个文件移动到/usr/lib/nginx/modules路径下,方便下一步的操作。
$ cd objs
$ mv ngx_http_geoip2_module.so /usr/lib/nginx/modules
$ mv ngx_stream_geoip2_module.so /usr/lib/nginx/modules
8. 以上操作完成后,可以使用mmdblookup命令在库中搜索试试:
$ mmdblookup -f /usr/share/GeoIP/GeoLite2-City.mmdb -i 114.114.114.114
在nginx.conf中第一行加入
load_module /usr/lib/nginx/modules/ngx_http_geoip2_module.so;
用来引用ngx_http_geoip2_module模块,接下来在http代码块中加入以下代码
geoip2 /var/geoip2/GeoLite2-City.mmdb {
$geoip2_data_city_continent_code continent code;
$geoip2_data_city_continent_name continent names zh-CN;
$geoip2_data_city_country_code country iso_code;
$geoip2_data_city_country_name country names zh-CN;
$geoip2_data_city_region_code subdivisions 0 iso_code;
$geoip2_data_city_region_name subdivisions 0 names zh-CN;
$geoip2_data_city_location_latitude location latitude;
$geoip2_data_city_location_longitude location longitude;
$geoip2_data_city_postal postal code;
$geoip2_data_city_city_name city names en;
}
表示通过ngx_http_geoip2_module根据访问这个ip获得相应的geo信息。
接下来打开fastcgi.conf文件,在文件末尾添加以下代码:
fastcgi_param MM_CONTINENT_CODE $geoip2_data_city_continent_code;
fastcgi_param MM_CONTINENT_NAME $geoip2_data_city_continent_name;
fastcgi_param MM_COUNTRY_CODE $geoip2_data_city_country_code;
fastcgi_param MM_COUNTRY_NAME $geoip2_data_city_country_name;
fastcgi_param MM_REGION_CODE $geoip2_data_city_region_code;
fastcgi_param MM_REGION_NAME $geoip2_data_city_region_name;
fastcgi_param MM_LATITUDE $geoip2_data_city_location_latitude;
fastcgi_param MM_LONGITUDE $geoip2_data_city_location_longitude;
fastcgi_param MM_POSTAL_CODE $geoip2_data_city_postal;
fastcgi_param MM_CITY_NAME $geoip2_data_city_city_name;
这样就可以将geo信息传递到php上了。
配置好以后重载一下nginx
参考链接:
https://www.azio.me/how-to-install-ngx_http_geoip2_module/
https://github.com/maxmind/libmaxminddb/releases/
https://github.com/leev/ngx_http_geoip2_module/
遇到的问题:
[root@nginx objs]# nginx -t
nginx: [emerg] dlopen() "/usr/lib64/nginx/modules/ngx_http_geoip2_module.so" failed (libmaxminddb.so.0: cannot open shared object file: No such file or directory) in /etc/nginx/nginx.conf:13nginx: configuration file /etc/nginx/nginx.conf test failed
第一种可能:module文件版本不一致,需要更新
# rpm -qa|grep nginx
# yum -y remove nginx-mod*
# yum -y install nginx-module-*
或者
第二种可能:在/etc/ld.so.conf中加入/usr/local/lib这一行,再执行ldconfig可以解决此问题。
浙公网安备 33010602011771号