前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Tengine编译GeoIP2

Tengine编译GeoIP2

作者头像
用户6792968
发布2022-08-30 12:11:47
6090
发布2022-08-30 12:11:47
举报
文章被收录于专栏:fred 随笔fred 随笔

前言

nginx geoip geoip2 模块,集成了最新的免费 maxmind geoip mmdb 数据,可以使用nginx去获取访问IP具体归属国家地区,或者根据地区去进行流量分发功能

部署教程

1. 安装 libmaxminddb
代码语言:javascript
复制
wget https://github.com/maxmind/libmaxminddb/releases/download/1.3.2/libmaxminddb-1.3.2.tar.gz
tar -zxvf libmaxminddb-1.3.2.tar.gz
cd libmaxminddb-1.3.2
./configure && make && make install
echo /usr/local/lib  >> /etc/ld.so.conf.d/local.conf 
ldconfig
2. 下载geoip2数据
代码语言:javascript
复制
git clone https://gitee.com/ZTfred/nginx-geoip2.git
cd nginx-geoip2
tar -zxvf GeoLite2-City_20200519.tar.gz
mv ./GeoLite2-City_20200519/GeoLite2-City.mmdb /usr/share/GeoIP/
tar -zxvf GeoLite2-Country_20200519.tar.gz
mv ./GeoLite2-Country_20200519/GeoLite2-Country.mmdb /usr/share/GeoIP/ 
3. 编译安装
代码语言:javascript
复制
cp -a /usr/local/tengine/  /usr/local/tengine_bak
mv ngx_http_geoip2_module/ /usr/local/tengine/modules/
cd ~/tengine-2.3.3/
# 重新编译nginx,添加geoip2的库,注意将之前的目录全都备份一遍
./configure  --prefix=/usr/local/tengine \
 --sbin-path=/usr/sbin/nginx \
 --modules-path=/usr/local/tengine/modules \
 --conf-path=/usr/local/tengine/conf/nginx.conf \
 --error-log-path=/usr/local/tengine/logs/error.log \
 --http-log-path=/usr/local/tengine/logs/access.log \ 
 --pid-path=/usr/local/tengine/run/nginx.pid \
 --lock-path=/usr/local/tengine/run/nginx.lock \
 --http-client-body-temp-path=/usr/local/tengine/cache/client_temp \ 
 --http-proxy-temp-path=/usr/local/tengine/cache/proxy_temp \
 --http-fastcgi-temp-path=/usr/local/tengine/cache/fastcgi_temp \
 --http-uwsgi-temp-path=/usr/local/tengine/cache/uwsgi_temp \
 --http-scgi-temp-path=/usr/local/tengine/cache/scgi_temp \
 --user=root \
 --group=root \
 --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-http_auth_request_module \
 --with-http_xslt_module=dynamic \
 --with-http_image_filter_module=dynamic \
 --with-http_geoip_module=dynamic \
 --with-threads \
 --with-stream \
 --with-stream_ssl_module \
 --with-stream_ssl_preread_module \
 --with-stream_realip_module \
 --with-stream_geoip_module=dynamic \
 --with-http_slice_module \
 --with-mail \
 --with-mail_ssl_module \
 --with-compat \
 --with-http_v2_module \
 --add-module=modules/ngx_http_upstream_check_module \
 --add-module=modules/ngx_http_upstream_session_sticky_module \
 --with-openssl=/usr/local/openssl \
 --add-module=/usr/local/tengine/modules/ngx_http_geoip2_module
make && make install
4. 修改nginx配置,使其生效
代码语言:javascript
复制
vim  /usr/local/tengine/conf/nginx.conf
# 增加配置信息
    # geoip2 配置
     geoip2 /usr/share/GeoIP/GeoLite2-Country.mmdb {
          $geoip2_country_code country iso_code;
          $geoip2_data_country_name country names en;
        }
     geoip2 /usr/share/GeoIP/GeoLite2-City.mmdb {
          $geoip2_data_city_code default=all city names en;
          $geoip2_data_province_name subdivisions 0 names en;
          $geoip2_data_province_isocode subdivisions 0 iso_code;
        }

# 修改日志配置,增加geoip变量
    log_format main '$remote_addr–$remote_user $geoip2_data_country_name-$geoip2_data_province_name-$geoip2_data_city_code  $host $uri $request_time  [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for $upstream_addr $upstream_response_time $upstream_status ';
# 重启nginx
pkill nginx
nginx
4. 浏览器访问网站,查看日志信息
image.png
image.png
代码语言:javascript
复制
tail -f grafana_alialili.log

60.221.102.187–- China-Shanxi-Linfen  grafana.alialili.cn /public/build/grafanaPlugin.6839ffb2aed352838f3a.js.map 3.651  [10/Feb/2022:17:04:39 +0800] GET /public/build/grafanaPlugin.6839ffb2aed352838f3a.js.map HTTP/2.0 200 10875 - Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.82 Safari/537.36 - 10.1.0.4:3000 0.004 200
5. 扩展(只允许部分地区访问网站)
代码语言:javascript
复制
vim grafana.conf
# 修改tengine配置,增加
location / {
 ...
    if ($geoip2_data_city_code != "Linfen"){
       return 403; 
 ...
}
image.png
image.png
image.png
image.png
代码语言:javascript
复制
#  禁止多个地区访问
vim  /usr/local/tengine/conf/nginx.conf
# 增加配置信息
    map $geoip2_data_city_code $is_banner_city {
        default yes;
        Beijing no;
	Linfen yes;
    }
vim /usr/local/tengine/conf/vhost/grafana.conf
# 增加
    if ($is_banner_city = no ){
        return 403;
   }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. 安装 libmaxminddb
  • 2. 下载geoip2数据
  • 3. 编译安装
  • 4. 修改nginx配置,使其生效
  • 4. 浏览器访问网站,查看日志信息
  • 5. 扩展(只允许部分地区访问网站)
相关产品与服务
Grafana 服务
Grafana 服务(TencentCloud Managed Service for Grafana,TCMG)是腾讯云基于社区广受欢迎的开源可视化项目 Grafana ,并与 Grafana Lab 合作开发的托管服务。TCMG 为您提供安全、免运维 Grafana 的能力,内建腾讯云多种数据源插件,如 Prometheus 监控服务、容器服务、日志服务 、Graphite 和 InfluxDB 等,最终实现数据的统一可视化。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档