31

nginx日志分析goaccess

 3 years ago
source link: https://zdb.im/archives/173.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

英文网站

http://goaccess.io/

包安装

yum install glib2 glib2-devel GeoIP-devel  ncurses-devel zlib zlib-develyum install gcc -y
yum -y install GeoIP-update
yum install goaccess

#修改/etc/nginx/nginx.conf文件的日志存储格式
log_format  main  '$remote_addr - $remote_user [$time_local] requesthost:"$http_host"; "$request" requesttime:"$request_time"; '
        '$status $body_bytes_sent "$http_referer" - $request_body'                      
        '"$http_user_agent" "$http_x_forwarded_for"';

        
#修改文件/etc/goaccess.conf改成goaccess格式标准对应为
time-format %T
date-format %d/%b/%Y
log-format %h - %^ [%d:%t %^] requesthost:"%v"; "%r" requesttime:"%T"; %s %b "%R" - %^"%u"

#测试生成页面
goaccess -f /var/log/nginx/access.log -c -a>/usr/share/nginx/html/go.html

编译安装

#yum安装依赖包
yum install glib2 glib2-devel GeoIP-devel  ncurses-devel zlib zlib-devel
yum install gcc -y
rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm
yum -y install GeoIP-update


$ wget http://tar.goaccess.io/goaccess-1.2.tar.gz
$ tar -xzvf goaccess-1.2.tar.gz
$ cd goaccess-1.2/
$ ./configure --enable-utf8 --enable-geoip=legacy
$ make
# make install


#修改/etc/nginx/nginx.conf文件的日志存储格式
log_format  main  '$remote_addr - $remote_user [$time_local] requesthost:"$http_host"; "$request" requesttime:"$request_time"; '
        '$status $body_bytes_sent "$http_referer" - $request_body'                      
        '"$http_user_agent" "$http_x_forwarded_for"';

        
#修改文件/usr/local/etc/goaccess.conf改成goaccess格式标准对应为
time-format %T
date-format %d/%b/%Y
log-format %h - %^ [%d:%t %^] requesthost:"%v"; "%r" requesttime:"%T"; %s %b "%R" - %^"%u"


#测试生成页面
goaccess -f /var/log/nginx/access.log -c -a>/usr/share/nginx/html/go.html


#后台实时生成数据到goaccess页面
/usr/local/bin/goaccess /var/log/nginx/access.log -o /usr/share/nginx/html/go.html  --real-time-html &

nginx默认日志格式

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

log_format格式变量:

$remote_addr  #记录访问网站的客户端地址

$remote_user  #远程客户端用户名

$time_local  #记录访问时间与时区

$request  #用户的http请求起始行信息

$status  #http状态码,记录请求返回的状态码,例如:200、301、404等

$body_bytes_sent  #服务器发送给客户端的响应body字节数

$http_referer  #记录此次请求是从哪个连接访问过来的,可以根据该参数进行防盗链设置。

$http_user_agent  #记录客户端访问信息,例如:浏览器、手机客户端等

$http_x_forwarded_for  #当前端有代理服务器时,设置web节点记录客户端地址的配置,此参数生效的前提是代理服务器也要进行相关的x_forwarded_for设置

其他还有:

'"$upstream_addr" "$upstream_status" "$upstream_response_time" "$request_time"'

注意: 仅仅需要依赖 ncurses 模块。

提示: 如果需要使用实时 HTML 报告, 请确保开放 7890 端口。详见这里。

配置选项

GoAccess 拥有多个配置选项。获取完整的最新配置选项列表,请运行:./configure --help

--enable-debug

使用调试标志编译且关闭编译器优化。

--enable-utf8

宽字符支持。依赖 Ncursesw 模块。

--enable-geoip=<legacy|mmdb>

地理位置支持。依赖 MaxMind GeoIP 模块。legacy 将使用原始 GeoIP 数据库。mmdb 将使用增强版 GeoIP2 数据库。

--enable-tcb=<memhash|btree>

Tokyo Cabinet 存储支持。 memhash 将使用 Tokyo Cabinet 的内存哈希数据库。 btree 将使用 Tokyo Cabinet 的磁盘 B+Tree 数据库。

--disable-zlib

禁止在 B+Tree 数据库上使用 zlib 压缩。

--disable-bzip

禁止在 B+Tree 数据库上使用 bzip2 压缩。

--with-getline

使用动态扩展行缓冲区用来解析完整的行请求,否则将使用固定大小(4096)的缓冲区。

--with-openssl

使 GoAccess 与其 WebSocket 服务器之间的通信能够支持 OpenSSL。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK