nginx要获取到ip地理位置,需要在nginx引用第三方ngx_http_geoip2_module模块,而ngx_http_geoip2_module模块依赖libmaxminddb;另外ip对应的地理位置离线的需要从GeoIP2站点上下载下来;最后在nginx.conf文件中引用ngx_http_geoip2_module模块,配置离线数据库才可以获取地理位置

nginx日志中的效果图
image

一、安装libmaxminddb

#下载包
wget https://github.com/maxmind/libmaxminddb/releases/download/1.7.1/libmaxminddb-1.7.1.tar.gz
tar -zxvf libmaxminddb-1.7.1.tar.gz
cd libmaxminddb-1.7.1/
#安装
./configure
make
make check
make install
#编译安装完成后需要将库的安装位置添加到系统路径中
sh -c "echo /usr/local/lib  >> /etc/ld.so.conf.d/local.conf"
ldconfig

二、nginx添加ngx_http_geoip2_module模块

下载地址:https://github.com/leev/ngx_http_geoip2_module/releases
tar -zxvf ngx_http_geoip2_module-3.4.tar.gz
mv ngx_http_geoip2_module-3.4 /usr/local/src/ngx_http_geoip2_module
#备份原来的编译后的nginx
cp -r /usr/local/nginx /usr/local/nginx_bak20230818
#进入源代码文件目录下重新安装
cd /usr/local/nginx-1.20.2
./configure --prefix=/usr/local/nginx --with-http_ssl_module --add-module=/usr/local/src/ngx_http_geoip2_module
make
#将编译后的nginxc程序复制到/usr/local/nginx/sbin目录下替换原来的nginx程序
cp objs/nginx /usr/local/nginx/sbin/nginx
#查看geoip模块是否添加成功
/usr/local/nginx/sbin/nginx -V

三、下载IP离线数据库,GeoIP2-Country和GeoIP2-City

1. 输入网址进入https://www.maxmind.com/


2. 进入下载页面
image


image


3.注册用户
image


4. 再次进入到刚才注册用户的界面,点击Download Files
image


image


四、nginx配置

引入geoip2

##
# GeoIP Setting
##

# 国家数据库
geoip2 /usr/local/nginx/conf/GeoLite2-Country.mmdb {
    #国家代码
    $geoip2_data_country_code default=China source=$remote_addr country iso_code;
    #国家名称
    $geoip2_data_country_name country names en;
    #洲代码
    $geoip2_data_continent_code continent code;
    #洲名称
    $geoip2_data_continent_name continent names en;
}
# 城市数据库
geoip2 /usr/local/nginx/conf/GeoLite2-City.mmdb {
    #城市名称
    $geoip2_data_city_name city names en;
    #省份代码
    $geoip2_data_province_code subdivisions 0 iso_code;
    #省份名称
    $geoip2_data_province_name subdivisions 0 names en;
}

日志输出ip属地

 log_format  country '$remote_addr "$geoip2_data_country_name" "$geoip2_data_city_name" "$geoip2_data_province_name" "$time_local" "$request" '
                         '$status $body_bytes_sent "$http_referer" '
                         '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  country;

完整配置

http {
    geoip2 /usr/local/nginx/conf/GeoLite2-Country.mmdb {
        $geoip2_data_country_code default=China source=$remote_addr country iso_code;
        $geoip2_data_country_name country names en;
        $geoip2_data_continent_code continent code;
        $geoip2_data_continent_name continent names en;
    }
    geoip2 /usr/local/nginx/conf/GeoLite2-City.mmdb {
        $geoip2_data_city_name city names en;
        $geoip2_data_province_code subdivisions 0 iso_code;
        $geoip2_data_province_name subdivisions 0 names en;
    }

    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;
    log_format  country '$remote_addr "$geoip2_data_country_name" "$geoip2_data_city_name" "$geoip2_data_province_name" "$time_local" "$request" '
                         '$status $body_bytes_sent "$http_referer" '
                         '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  logs/access.log  country;

记录用、nginx添加http_geoip_module模块

  1. 查看当前nginx已经安装的模块
    /usr/local/nginx/sbin/nginx -V
    image
    发现原来安装的配置是这个--prefix=/usr/local/nginx --with-http_ssl_module
    将原来配置后面再加一个geoip模块 --with-http_geoip_module
    安装命令变成了 ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_geoip_module
  2. nginx添加geoip模块
    #备份原来的编译后的nginx
    cp -r /usr/local/nginx /usr/local/nginx_bak20230818
    #进入源代码文件目录下重新安装
    cd /usr/local/nginx-1.20.2
    ./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_geoip_module
    make
    #将编译后的nginxc程序复制到/usr/local/nginx/sbin目录下替换原来的nginx程序
    cp objs/nginx /usr/local/nginx/sbin/nginx
    #查看geoip模块是否添加成功
    /usr/local/nginx/sbin/nginx -V
    
    image

参考:https://blog.awolon.fun/archives/nginx-record-geoip-data.html
https://github.com/leev/ngx_http_geoip2_module
https://www.pianshen.com/article/39572688425/
https://m.php.cn/faq/557270.html

posted @ 2023-08-18 17:58 tongyongliang 阅读(1390) 评论(0) 推荐(0)
摘要: 一、 性质 把被查找的条目搬移到离树根近一些的地方 亦满足二叉树的性质左子树小于根节点,右子树大于根节点 二、 操作 伸展操作splay(x,rt) 从中树的根节点开始分裂为3棵树分别为中树、左树、右树,中树按值x分裂将大于x的树节点连接到右树上,将小于x的树节点连接到左树上 分裂时如果是一字型的情 阅读全文
posted @ 2022-11-09 15:17 tongyongliang 阅读(56) 评论(0) 推荐(0)
摘要: 一、问题描述 使用IE11时会出现此类问题,chrome没有这种问题 经分析If-None-Match中值传到后台有中文双引号导致验证不通过, 因此需要修改tomcat的源码,将中文双引号替换成英文双引号。 主要参考:升级tomcat导致静态资源报错400_httxiao的博客-CSDN博客_tom 阅读全文
posted @ 2022-10-31 14:20 tongyongliang 阅读(686) 评论(0) 推荐(0)
摘要: 将文件夹下的jar包转为标签,有问题的标签再访问阿里云仓库手动修改 https://developer.aliyun.com/mvn/search public static String doGet(String httpurl) { HttpURLConnection connection = 阅读全文
posted @ 2022-09-12 16:49 tongyongliang 阅读(50) 评论(0) 推荐(0)
摘要: 问题 Given an NxN matrix of 0's and 1's; 0's representing water, and 1's representing land. Find the largest landmass in the area that includes the lake 阅读全文
posted @ 2022-03-26 14:59 tongyongliang 阅读(40) 评论(0) 推荐(0)
摘要: 参考: KMP算法详解-彻底清楚了(转载+部分原创) KMP算法求next,用动态规划思想去理解 求next数组,利用①回溯的方法、②动态规划思路、③最长相等真前后缀减少回溯的方法 典范代码: public int[] getNext(String p){ int[] next = new int[ 阅读全文
posted @ 2021-09-24 08:58 tongyongliang 阅读(38) 评论(0) 推荐(0)
摘要: 参考: 【实现】单点登录原理与简单实现 以及单点登录的三种实现方式 【解决问题】Java根据Session Id获取Session对象 【解决问题】解决session共享后,session监听销毁事件失效 【解决问题】使用spring-session 根据sessionId 删除session 【完 阅读全文
posted @ 2021-08-27 18:40 tongyongliang 阅读(918) 评论(0) 推荐(0)
摘要: 该图是以丈夫的思路从选店到取照的时序图进行绘制 阅读全文
posted @ 2020-07-17 09:33 tongyongliang 阅读(189) 评论(0) 推荐(0)
摘要: 配置文件下载 链接:https://pan.baidu.com/s/1gjl_bDKQf-dhf31sFfDQ_w 密码:hh8o 阅读全文
posted @ 2020-04-05 22:17 tongyongliang 阅读(497) 评论(0) 推荐(0)
摘要: 使用idea一个月了还没有用到导出war,今天突然需要我来部署测试war包,想使用myeclipse的,转念一想太掉价了 废话少说,直接上菜 启动项目 war包就生成在代码文件夹下的out路径下 如果你没有第一步操作我建议你配置一下你的idea 当然还有更好的方法 :直接导入配置文件 步骤1: 阅读全文
posted @ 2020-04-05 22:15 tongyongliang 阅读(601) 评论(0) 推荐(0)
点击右上角即可分享
微信分享提示