检查 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/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
2018-06-17 windows上使用的免费连接linux终端xshell6,xftp6下载