首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Nginx日志添加访客地域信息

Nginx日志添加访客地域信息

作者头像
陳斯托洛夫斯記
发布2022-10-27 14:45:38
发布2022-10-27 14:45:38
56200
代码可运行
举报
文章被收录于专栏:XBDXBD
运行总次数:0
代码可运行

效果

安装geoip模块

代码语言:javascript
代码运行次数:0
运行
复制
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx.pid \
--user=nginx \
--group=nginx \
--with-stream \
--with-threads \
--with-file-aio \
--with-http_v2_module \
--with-http_mp4_module \
--with-http_sub_module  \
--with-http_ssl_module \
--with-http_geoip_module

下载IP数据库

代码语言:javascript
代码运行次数:0
运行
复制
# 下载国家数据库
wget https://dl.miyuru.lk/geoip/maxmind/country/maxmind.dat.gz
gzip -d maxmind.dat.gz
mv maxmind.dat /usr/local/nginx/geoip/GeoCountry.dat

# 下载城市数据库
wget https://dl.miyuru.lk/geoip/maxmind/city/maxmind.dat.gz
gzip -d maxmind.dat.gz
mv maxmind.dat /usr/local/nginx/geoip/GeoCity.dat

添加环境变量配置文件

代码语言:javascript
代码运行次数:0
运行
复制
vim /usr/local/nginx/conf/geoip.conf

fastcgi_param GEOIP_COUNTRY_CODE $geoip_country_code;
fastcgi_param GEOIP_COUNTRY_CODE3 $geoip_country_code3;
fastcgi_param GEOIP_COUNTRY_NAME $geoip_country_name;
fastcgi_param GEOIP_CITY_COUNTRY_CODE $geoip_city_country_code;
fastcgi_param GEOIP_CITY_COUNTRY_CODE3 $geoip_city_country_code3;
fastcgi_param GEOIP_CITY_COUNTRY_NAME $geoip_city_country_name;
fastcgi_param GEOIP_REGION $geoip_region;
fastcgi_param GEOIP_CITY $geoip_city;
fastcgi_param GEOIP_POSTAL_CODE $geoip_postal_code;
fastcgi_param GEOIP_CITY_CONTINENT_CODE $geoip_city_continent_code;
fastcgi_param GEOIP_LATITUDE $geoip_latitude;
fastcgi_param GEOIP_LONGITUDE $geoip_longitude;

修改nginx配置

代码语言:javascript
代码运行次数:0
运行
复制
# 添加IP数据库
http {
    ...
    geoip_country /usr/local/nginx/geoip/GeoCountry.dat;
    geoip_city /usr/local/nginx/geoip/GeoCity.dat;

    include geoip.conf;
    ...
}

# 修改日志格式
vim /etc/nginx/nginx.conf
...
http {
...
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"'
                      '$geoip_country_name $geoip_region_name $geoip_city';
...
}
...

vim /etc/nginx/conf.d/defaults.conf
server {
...
    access_log /var/log/nginx/access.log main;
    error_log /var/log/nginx/error.log;
...
}

我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=1melm7zbxp9z9

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 安装geoip模块
  • 下载IP数据库
  • 添加环境变量配置文件
  • 修改nginx配置
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档