区域 进入相应页面"/>
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下载地址
.3.2/libmaxminddb-1.3.2.tar.gz
ngx_http_geoip2_module下载地址
/
GeoLite2-Country_20230912 需要到官网上注册后下载GeoIP2 Web Services | MaxMind
在哪里下载,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 ^ / redirect;
}
if ($geoip2_data_country_code != CN) {
rewrite ^ / redirect;
}
}
更多推荐
nginx geoip2 根据城市区域 进入相应页面
发布评论