nginx geoip2 根据城市区域 进入相应页面

1.创建目录放之后下载的3个压缩文件

1-1 mkdir /etc/nginx && mkdir /etc/nginx/geoip

1-2 cd /etc/nginx/geoip

2. 3个文件我下载到了本地,然后通过scp 上传至/etc/nginx/geoip目录

2-1 scp libmaxminddb-1.7.1.tar.gz root@ip:/etc/nginx/geoip

2-2 scp ngx_http_geoip2_module-3.4.tar.gz root@ip:/etc/nginx/geoip

2-3 scp GeoLite2-Country_20230912.tar.gz root@ip:/etc/nginx/geoip

libmaxminddb下载地址

https://github.com/maxmind/libmaxminddb/releases/download/1.3.2/libmaxminddb-1.3.2.tar.gz

ngx_http_geoip2_module下载地址

https://github.com/leev/ngx_http_geoip2_module/

GeoLite2-Country_20230912 需要到官网上注册后下载GeoIP2 Web Services | MaxMind

https://www.maxmind.com/en/home

在哪里下载,My Account > Download Databases

3. 解压文件3个文件

3-1 tar -zxvf libmaxminddb-1.7.1.tar.gz

3-2 tar -zxvf ngx_http_geoip2_module-3.4.tar.gz

3-1 tar -zxvf GeoLite2-Country_20230912.tar.gz

4. libmaxminddb-1.7.1需要安装及编译

4-1 cd libmaxminddb-1.7.1/

4-2 ./configure && make && make install 在动态链接库的配置里,加入了库文件的位置

4-3 echo /usr/local/lib >> /etc/ld.so.conf.d/local.conf

//更新/etc/ld.so.cache文件

4-4 -ldconfig

5. 此命令检测模块是否可以使用,根据ip显示当前城市信息

5-1 mmdblookup --file /etc/nginx/geoip/GeoLite2-Country_20230912/GeoLite2-Country.mmdb --ip 8.8.8.8

6. 配置nginx

6-1 先到nginx 可执行 nginx -V 查看现有配置,这个配置需要之后加上--add-dynamic-module=/etc/nginx/geoip/ngx_http_geoip2_module-3.4 执行配置 接着到./configure 可执行目录

6-2 ./configure --prefix=/usr/local/nginx --add-dynamic-module=/etc/nginx/geoip/ngx_http_geoip2_module-3.4

6-3 执行 make && make install

6-4 再次执行nginx -V 可以看到最后会有这段 --add-dynamic-module=/etc/nginx/geoip/ngx_http_geoip2_module-3.4

7. 对nginx.conf修改 头部引入模块

7-1 load_module modules/ngx_http_geoip2_module.so;

8. 变量赋值 country iso_code 赋值给$geoip2_data_country_code

8-1 http内部编写

http {

        geoip2 /etc/nginx/geoip/GeoLite2-Country_20230912/GeoLite2-Country.mmdb {                 $geoip2_data_country_code country iso_code;

        }

}

9. #server 里面。我是在自己服务器上测试的,server根据自己的需求和判断进行逻辑编写

9-1

server {

   if ($geoip2_data_country_code = CN) {

        rewrite ^ http://xxx.xxxl.com/ redirect;

   }

   if ($geoip2_data_country_code != CN) {

        rewrite ^ http://xxx.xxx.com/ redirect;

   }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值