GoAccess实时分析Nginx日志

GoAccess 是一个基于终端的实时 Web 日志分析仪。用 C 语言编写,它是快速,互动的,并以优雅而直观的方式显示日志。它提供了各种 Web 日志文件的支持,包括 Apache,Nginx,Caddy,Amazon S3 和 CloudFront,仅提及几个。它可以以 HTML 格式,JSON 渲染结果,还可以生成 CSV 报告。

1. 目录准备

  • 存放goaccess.conf配置文件
mkdir -p /usr/local/goaccess/data
  • 存放goaccess生成分析html
mkdir -p /usr/local/goaccess/html

2. 启动nginx

  • nginx中添加配置,监听7891端口
    server {
   
        listen      7891;
        server_name  dev.lettin.cn 114.55.42.227;
        location / {
   
            root   /usr/share/nginx/html;
            index  index.html;
        }
    }
  • 启动nginx并挂载目录
docker run -d --net=host --name=nginx -v /usr/local/goaccess/html:/usr/share/nginx/html --restart=always nginx
找到日志nginx文件

我使用的docker方式运行nginx,所用是前台启动,nginx输出在控制台,需要找到docker 容器的日志文件(nginx为容器名):

docker inspect --format='{
   {.LogPath}}' nginx

注意:每次docker run都会生成新的容器,日志文件路径会改变
在这里插入图片描述

3. 安装GoAccess(docker版)

goaccess.conf配置准备

/usr/local/goaccess/data目录下
goaccess.conf配置文件下载地址:https://github.com/allinurl/goaccess/blob/master/config/goaccess.conf

  • 需要修改的内容如下
time-format %H:%M:%S

date-format %d/%b/%Y

log-format %h %^ %^ [%d:%t %^] "%r" %^ %s %b "%R" "%u" "%^" "%T"
  • 下面是我配好的(复制可用
######################################
# Time Format Options (required)
######################################
#
# The hour (24-hour clock) [00,23]; leading zeros are permitted but not required.
# The minute [00,59]; leading zeros are permitted but not required.
# The seconds [00,60]; leading zeros are permitted but not required.
# See `man strftime` for more details
#
# The following time format works with any of the
# Apache/NGINX's log formats below.
#
time-format %H:%M:%S
#
# Google Cloud Storage or
# The time in microseconds since the Unix epoch.
#
#time-format %f

# Squid native log format
#
#time-format %s

######################################
# Date Format Options (required)
######################################
#
# The date-format variable followed by a space, specifies
# the log format date containing any combination of regular
# characters and special format specifiers. They all begin with a
# percentage (%) sign. See `man strftime`
#
# The following date format works with any of the
# Apache/NGINX's log formats below.
#
date-format %d/%b/%Y
#
# AWS | Amazon CloudFront (Download Distribution)
# AWS | Elastic Load Balancing
# W3C (IIS)
#
#date-format %Y-%m-%d
#
# Google Cloud Storage or
# The time in microseconds since the Unix epoch.
#
#date-format %f

# Squid native log format
# Caddy
#
#date-format %s

######################################
# Date/Time Format Option
######################################
#
# The datetime-format variable followed by a space, specifies
# the log format date and time containing any combination of regular
# characters and special format specifiers. They all begin with a
# percentage (%) sign. See `man strftime`
#
# This gives the ability to get the timezone from a request and
# convert it to another timezone for output. See --tz=<timezone> in
# the man page.
#
#datetime-format %d/%b/%Y:%H:%M:%S %z

######################################
# Log Format Options (required)
######################################
#
# The log-format variable followed by a space or \t for
# tab-delimited, specifies the log format string.
#
# NOTE: If the time/date is a timestamp in seconds or microseconds
# %x must be used instead of %d & %t to represent the date & time.

# NCSA Combined Log Format
#log-format %h %^[%d:%t %^] "%r" %s %b "%R" "%u"
log-format {
   "log":"%h %^[%d:%t %^] \""%m %U %H %s %b "%R" "%u"}

# NCSA Combined Log Format with Virtual Host
#log-format %v:%^ %h %^[%d:%t %^] "%r" %s %b "%R" "%u"

# Common Log Format (CLF)
#log-format %h %^[%d:%t %^] "%r" %s %b

# Common Log Format (CLF) with Virtual Host
#log-format %v:%^ %h %^[%d:%t %^] "%r" %s %b

# W3C
#log-format %d %t %h %^ %^ %^ %^ %r %^ %s %b %^ %^ %u %R

# Squid native log format
#log-format %^ %^ %^ %v %^: %x.%^ %~%L %h %^/%s %b %m %U

# AWS | Amazon CloudFront (Download Distribution)
#log-format %d\t%t\t%^\t%b\t%h\t%m\t%^\t%r\t%s\t%R\t%u\t%^

# Google Cloud Storage
#log-format "%x","%h",%^,%^,"%m","%U","%s",%^,"%b","%D",%^,"%R","%u"

# AWS | Elastic Load Balancing
#log-format %dT%t.%^ %^ %h:%^ %^ %T %^ %^ %^ %s %^ %b "%r" "%u"

# AWSS3 | Amazon Simple Storage Service (S3)
#log-format %^[%d:%t %^] %h %^"%r" %s %^ %b %^ %L %^ "%R" "%u"

# Virtualmin Log Format with Virtual Hos
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
GoAccess是一款基于日志分析的开源工具,可以帮助用户实时监控和分析服务器日志,以便更好地了解服务器的状态和性能。下面是GoAccess分析nginx日志的详细步骤: 1. 安装GoAccess 在Linux系统中,可以通过包管理器安装GoAccess,例如: ``` sudo apt-get install goaccess ``` 2. 收集nginx日志nginx的配置文件中,可以设置日志的格式和路径。例如: ``` log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; ``` 这里采用的是默认的nginx日志格式,日志文件路径为`/var/log/nginx/access.log`。 3. 分析nginx日志 使用GoAccess分析nginx日志非常简单,只需要在终端输入以下命令: ``` goaccess /var/log/nginx/access.log ``` 然后就会显示一个实时的控制台窗口,展示nginx日志的各种统计信息,例如: - 请求的响应时间 - 客户端地理位置 - 请求的URL和HTTP方法 - 访问来源和搜索关键字 - 响应状态码和响应大小 还可以使用GoAccess的各种选项来自定义分析结果的格式和内容,例如: - `--date-format`:指定日期格式 - `--log-format`:指定日志格式 - `--output`:将结果输出到文件中 例如,要将结果输出到HTML文件中,可以使用以下命令: ``` goaccess /var/log/nginx/access.log -o report.html --log-format=COMBINED ``` 这将把分析结果保存到`report.html`文件中,并使用默认的`COMBINED`日志格式。 总之,GoAccess是一款非常强大和易于使用的日志分析工具,可以帮助我们更好地了解服务器的状态和性能,从而进行优化和改进。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值