宝塔面板使用ngx_http_geoip2_module

首先去github下载ngx_http_geoip2_module该模块,然后解压,放到路径

然后宝塔安装的时候选择编译安装,添加模块,设置--add-module=ngx_http_geoip2_module解压的路径即可,其它什么都不用做

等宝塔安装好nginx后,命令行执行nginx -V如果看到ngx_http_geoip2_module模块的话就说明安装成功了,不需要在nginx主配置文件里面设置load_module modules/ngx_http_geoip2_module.so;

下面我们来配置nginx,使它可以得到ip地址信息:
主配置文件:

    # http://your.fucking.hostname/?ip=171.217.41.60 如果传递ip,会使用该ip
    # 否则使用客户端的ip
    map $arg_ip $target_ip {
        default $remote_addr;
        ~^(?!(10|172\.(1[6-9]|2\d|3[0-1])|192\.168)\.)\d+\.\d+\.\d+\.\d+$ $arg_ip;
    }
    
    geoip2 /www/wwwroot/maxmind/GeoLite2-ASN.mmdb {
        $geoip2_data_asn source=$target_ip autonomous_system_number;  
        $geoip2_data_isp source=$target_ip autonomous_system_organization; 
    }
    
    geoip2 /www/wwwroot/maxmind/GeoLite2-Country.mmdb {
        $geoip2_data_country_name source=$target_ip country names zh-CN;
        
    }
    
    geoip2 /www/wwwroot/maxmind/GeoLite2-City.mmdb {
        $geoip2_data_city_name source=$target_ip city names zh-CN;
        $geoip2_data_province source=$target_ip subdivisions 0 names zh-CN;
    }

需要说明的是,上面三个数据库需要自己去maxmind下载

再配置单个网站的配置文件:

    location / {
        # add_header X-City $geoip2_data_city_name;
        
        default_type application/json;  
        add_header Content-Type "application/json; charset=utf-8";  

        return 200 "{
            \"client_ip\":\"$remote_addr\",  
            \"target_ip\":\"$target_ip\",
            \"country\":\"$geoip2_data_country_name\", 
            \"province\":\"$geoip2_data_province\", 
            \"city\":\"$geoip2_data_city_name\", 
            \"asn\":$geoip2_data_asn, 
            \"isp\":\"$geoip2_data_isp\"
        }";  
    }

经过上面的配置,正常来说访问该网站是可以得到一个json响应的:

{
            "client_ip":"xxx.xxx.xxx.xxx",  
            "target_ip":"zzz.zzz.zzz.zzz",
            "country":"中国", 
            "province":"浙江省", 
            "city":"乐清", 
            "asn":4567, 
            "isp":"Chinanet"
}

命令行快速获取ip信息:

mmdblookup --file /www/wwwroot/maxmind/GeoLite2-City.mmdb --ip xxx.xxx.xxx.xxx city names zh-CN
mmdblookup --file /www/wwwroot/maxmind/GeoLite2-City.mmdb --ip xxx.xxx.xxx.xxx subdivisions 0 names zh-CN

如果想要快速获得ip地址信息,可以用:

http://ip.taobao.com/outGetIpInfo?ip=xxx.xxx.xxx.xxx&accessKey=alibaba-inc
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容