Nginx 使用 GeoIP 模块区分用户地区

摘要:
$geoip _国家_名称;-国家的全称,如“俄罗斯联邦”、“美国”$geoap_地区-地区的名称(类似于省、地区、州、行政区、联邦

检查 GeoIP 是否安装

首先需要确认当前安装的 Nginx 是否安装了 GeoIP 模块

1
2
3
4
5
6
$ nginx -V
nginx version: nginx/1.12.2
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --user=nginx --group=nginx --with-http_geoip_module --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-mail --with-mail_ssl_module --with-file-aio

如果版本信息中包含 --with-http_geoip_module,则说明已经支持该模块,如果不支持请往下看

安装 GeoIP

首先安装依赖

1
$ yum -y install zlib zlib-devel

安装 GeoIP

1
2
3
4
5
6
$ wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz
$ tar -zxvf GeoIP.tar.gz
$ cd GeoIP-1.4.8
$ ./configure
$ make
$ make install

使用ldconfig将库索引到系统中

1
2
$ echo '/usr/local/lib' > /etc/ld.so.conf.d/geoip.conf
$ ldconfig

检查库是否加载成功

1
2
3
4
5
6
$ ldconfig -v | grep GeoIP

libGeoIPUpdate.so.0 -> libGeoIPUpdate.so.0.0.0
libGeoIP.so.1 -> libGeoIP.so.1.4.8
libGeoIPUpdate.so.0 -> libGeoIPUpdate.so.0.0.0
libGeoIP.so.1 -> libGeoIP.so.1.5.0

将 GeoIP 模块编译到 Nginx 中

根据你当前 Nginx 的安装参数带上 --with-http_geoip_module 重新编译

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$ ./configure --user=nginx --group=nginx 
--with-http_geoip_module
--with-http_ssl_module
--with-http_realip_module
--with-http_addition_module
--with-http_sub_module
--with-http_dav_module
--with-http_flv_module
--with-http_mp4_module
--with-http_gunzip_module
--with-http_gzip_static_module
--with-http_random_index_module
--with-http_secure_link_module
--with-http_stub_status_module
--with-mail
--with-mail_ssl_module
--with-file-aio
$ make && make install

或者重新安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$ wget https://nginx.org/download/nginx-1.12.2.tar.gz
$ tar zxvf nginx-1.12.2.tar.gz
$ cd nginx-1.12.2
$ ./configure --user=nginx --group=nginx
--with-http_geoip_module
--with-http_ssl_module
--with-http_realip_module
--with-http_addition_module
--with-http_sub_module
--with-http_dav_module
--with-http_flv_module
--with-http_mp4_module
--with-http_gunzip_module
--with-http_gzip_static_module
--with-http_random_index_module
--with-http_secure_link_module
--with-http_stub_status_module
--with-mail
--with-mail_ssl_module
--with-file-aio
$ make && make install

使用 GeoIP

首先查看本地是否已有 GeoIP 数据库

1
2
3
4
$ cd /usr/local/share/GeoIP
$ ll
-rw-r--r--. 1 root root 1183408 Mar 31 06:00 GeoIP.dat
-rw-r--r--. 1 root root 20539238 Mar 27 05:05 GeoLiteCity.dat

如果没有这两个库,则手动下载

1
2
3
4
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gzip GeoLiteCity.dat.gz
wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz
gzip GeoIP.dat.gz

将库地址配置到 nginx.conf 中这个位置

1
2
3
4
5
6
7
8
9
10
11
12
13
http{
...
geoip_country /usr/local/share/GeoIP/GeoIP.dat;
geoip_city /usr/local/share/GeoIP/GeoLiteCity.dat;
server {
location / {
root /www;
if( $geo_country_code = CN ){
root /www/zh;
}
}
}
}

其他参数

  • $geoip_country_code; - 两个字母的国家代码,如:”RU”, “US”。
  • $geoip_country_code3; - 三个字母的国家代码,如:”RUS”, “USA”。
  • $geoip_country_name; - 国家的完整名称,如:”Russian Federation”, “United States”。
  • $geoip_region - 地区的名称(类似于省,地区,州,行政区,联邦土地等),如:”30”。 30代码就是广州的意思
  • $geoip_city - 城市名称,如”Guangzhou”, “ShangHai”(如果可用)。
  • $geoip_postal_code - 邮政编码。
  • $geoip_city_continent_code。
  • $geoip_latitude - 所在维度。
  • $geoip_longitude - 所在经度。

source: https://wxnacy.com/2018/04/01/nginx-geoip/

免责声明:文章转载自《Nginx 使用 GeoIP 模块区分用户地区》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇[labuladong算法小抄]手把手带你刷二叉树(第一期)最大矩形下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Dubbo与Nginx微服务架构

Dubbo的负载均衡已经是服务层面的了,和nginx的负载均衡还在http请求层面完全不同。至于二者哪个优秀,当然没办法直接比较。 涉及到负载均衡就涉及到你的业务,根据业务来选择才是最适合的。 dubbo具备了server注册,发现、路由、负载均衡的功能,在所有实现了这些功能的服务治理组件中,个人觉得dubbo还是略微笨重了,因为它本身是按照j2EE范畴所...

配置nginx 反向代理

新项目集成公司统一的SSO登陆,登陆域名aaa。前端启动服务localhost:10002,自动跳转到登陆地址,登陆成功后会进行跨域访问。 解决方案:nginx 反向代理1.安装nginx:   brew install nginx 如果遇到update Homebrew..., control+C终止当前进程,开始下一进程安装。 2.安装成功后,启动su...

【Linux】【二】linux 压缩文件(txt)、查看压缩文件内容、解压缩文件、

通过Xshell 压缩文件、解压缩文件 gzip  tools.txt        压缩【tools.txt】文件 zcat  tools.txt.gz        查看压缩文件【tools.txt.gz】内容 gunzip  tools.txt.gz      解压文件【tools.txt.gz】 cat  tools.txt         查看...

openresty使用笔记(一)

背景介绍 游戏经过一段时间的运营,发现了原来的设计缺陷太多,所以决定重新设计架构。使用到nginx作为核心并通过lua+redis设计实现自己的负载分配方案。先看看下面这张简单的架构图吧~ 从图上看,结构是非常简单的。主要通过编写lua分配策略使游戏房间内的人数尽量坐满。 技术实现思路 要编写lua插件通常需求编译nginx使其支持,如果嫌麻烦可以直接上...

记一次通过nginx反代网站请求总是超时的问题

问题描述 公司网站一个页面通过点击“导出数据”来进行数据的导出,后台是通过sql语句进行查询然后再导出为excel文件,因为需要查询数据过多,所以执行起来较慢,每次在等待导出一分钟之后提示网站请求超时。 解决思路 一开始查看了tomcat的连接超时时长,发现时间为20000ms,所以不是tomcat的问题,因为后台是通过nginx反向代理到tomcat,所...

使用goaccess对Nginx日志简单分析

接上篇使用 Linux命 令来分析。使用命令太麻烦,既不快捷又不直观,市面上有不少现成的工具可以使用,比如 ELK。但 ELK 太重了,这里使用的是比较轻量的 goaccess。 接收日志 goaccess 是直接对日志文件进行分析,首先要获得日志文件。nginx 原生支持将日志发送到远程 syslog , 参见官方文档。 1 access_log sy...