Nginx 模块介绍
1 :GeoIP
官网地址:http://nginx.org/en/docs/http/ngx_http_geoip_module.html
1.1 YUM安装需要使用官网 nginx.repo
vi /etc/yum.repos.d/nginx.repo
[nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [nginx-mainline] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/ gpgcheck=1 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true
# yum search nginx-module
nginx-mod-http-geoip.x86_64 : Nginx HTTP geoip module
1.2 安装
#yum install nginx-module-geoip
1.3 加载模块
/usr/lib64/nginx/modules/
load_module "modules/ngx_http_geoip_module.so";
load_module "modules/ngx_stream_geoip_module.so";
1.4 下载IP数据库解压
国家地域IP信息:GeoIP.dat
城市地域IP信息:GeoLiteCity.dat
1.5 加载IP数据库 (Context:http)如有反代需添加proxy
http{
geoip_country /etc/nginx/geoip/GeoIP.dat;
geoip_city /etc/nginx/geoip/GeoLiteCity.dat;
# geoip_proxy 192.168.0.0/24;
# geoip_proxy_recursive on;
...
server
{
...
# Check IP-infomation
location /ipinfo {
default_type text/plain;
return 200 "$http_x_forwarded_for $remote_addr $geoip_country_name $geoip_country_code $geoip_country_code3 $geoip_city";
}
1.6 访问测试
http:/serverip/ipinfo
23.111.188.5 192.168.0.20 United States US USA Centreville