nginx日志可视化

总结 分享  收藏
0 / 33

1. 安装依赖

yum install glib2 glib2-devel GeoIP-devel ncurses-devel zlib zlib-devel -y wget https://github.com/maxmind/geoip-api-c/releases/download/v1.6.11/GeoIP-1.6.11.tar.gz tar -zvxf GeoIP-1.6.11.tar.gz cd GeoIP-1.6.11 ./configure make && make install

2. 安装 goaccess

wget https://tar.goaccess.io/goaccess-1.3.tar.gz tar -xzvf goaccess-1.3.tar.gz cd goaccess-1.3 ./configure --enable-utf8 --enable-geoip=legacy make && make install

3. 校对 nginx 和 goaccess 日志格式

查看你的 nginx 日志格式 cat /usr/local/nginx/conf/nginx.conf '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"'

'$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'

4. 让你安装的 goaccess 插件了解你的 nginx 日志格式

使用如下方式获取日志格式: sh nginx2goaccess.sh '日志格式'

sh nginx2goaccess.sh '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'

得到日志格式 得到的结果,在 goaccess-1.3/config 下面创建一个 nginxlog.conf:

- Generated goaccess config:

time-format %T date-format %d/%b/%Y log_format %h - %^ [%d:%t %^] "%r"

5. 输出结果

./goaccess -f /usr/local/nginx/logs/access.log -p config/nginxlog.conf -o /usr/local/soft/report.html (报告内容阔以生成中文) LANG="zh_CN.UTF-8" bash -c "./goaccess -f /usr/local/nginx/logs/access.log -p config/nginxlog.conf -o /usr/local/soft/report.html"(<====输出路径可自行定义)