|
| 1 | +# GoAccess 安装和配置 |
| 2 | + |
| 3 | +## 官网资料 |
| 4 | + |
| 5 | +- 一般用于 Apache, Nginx 的 Log 分析 |
| 6 | +- 官网:<https://goaccess.io/> |
| 7 | +- 官网下载(201807 最新版本 1.2):<https://goaccess.io/download> |
| 8 | +- 官网 Github:<https://github.com/allinurl/goaccess> |
| 9 | +- 国内中文站:<https://goaccess.cc/> |
| 10 | + |
| 11 | + |
| 12 | +## 安装(CentOS 7.4) |
| 13 | + |
| 14 | +1. 安装依赖包 |
| 15 | + |
| 16 | +``` |
| 17 | +yum install -y ncurses-devel |
| 18 | +wget http://geolite.maxmind.com/download/geoip/api/c/GeoIP.tar.gz |
| 19 | +tar -zxvf GeoIP.tar.gz |
| 20 | +cd GeoIP-1.4.8/ |
| 21 | +./configure |
| 22 | +make && make install |
| 23 | +``` |
| 24 | + |
| 25 | +2. 安装 GoAccess |
| 26 | + |
| 27 | +``` |
| 28 | +wget http://tar.goaccess.io/goaccess-1.2.tar.gz |
| 29 | +tar -xzvf goaccess-1.2.tar.gz |
| 30 | +cd goaccess-1.2/ |
| 31 | +./configure --enable-utf8 --enable-geoip=legacy |
| 32 | +make && make install |
| 33 | +``` |
| 34 | + |
| 35 | +## 配置 |
| 36 | + |
| 37 | +- 假设你 nginx 安装在:`/usr/local/nginx` |
| 38 | +- 假设你 nginx 的 log 输出到:`/var/log/nginx` |
| 39 | +- 修改 `vim /usr/local/nginx/conf/nginx.conf` 指定 nginx 的日志格式 |
| 40 | + |
| 41 | +``` |
| 42 | +
|
| 43 | +http { |
| 44 | + charset utf8; |
| 45 | +
|
| 46 | + log_format main '$remote_addr - $remote_user [$time_local] "$request" ' |
| 47 | + '$status $body_bytes_sent "$http_referer" ' |
| 48 | + '"$http_user_agent" "$http_x_forwarded_for" "$request_time"'; |
| 49 | +
|
| 50 | + access_log /var/log/nginx/access.log main; |
| 51 | + error_log /var/log/nginx/error.log; |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +- 停止 nginx:`/usr/local/nginx/sbin/nginx -s stop` |
| 56 | +- 备份旧的 nginx log 文件:`mv /var/log/nginx/access.log /var/log/nginx/access.log.20180702back` |
| 57 | +- 启动 nginx:`/usr/local/nginx/sbin/nginx` |
| 58 | +- 创建 GoAccess 配置文件:`vim /etc/goaccess_log_conf_nginx.conf` |
| 59 | + |
| 60 | +``` |
| 61 | +time-format %T |
| 62 | +date-format %d/%b/%Y |
| 63 | +log_format %h - %^ [%d:%t %^] "%r" %s %b "%R" "%u" "%^" %^ %^ %^ %T |
| 64 | +``` |
| 65 | + |
| 66 | + |
| 67 | +## 使用 |
| 68 | + |
| 69 | +#### 手动生成当前统计页面 |
| 70 | + |
| 71 | +``` |
| 72 | +goaccess -f /var/log/nginx/access.log -p /etc/goaccess_log_conf_nginx.conf -o /usr/local/nginx/report/index.html |
| 73 | +``` |
| 74 | + |
| 75 | +- 更多参数用法: |
| 76 | + |
| 77 | +``` |
| 78 | +时间分布图上:按小时展示数据: |
| 79 | +goaccess -f /var/log/nginx/access.log -p /etc/goaccess_log_conf_nginx.conf -o /usr/local/nginx/report/index.html --ignore-crawlers --hour-spec=min |
| 80 | +
|
| 81 | +
|
| 82 | +时间分布图上:按分钟展示数据: |
| 83 | +goaccess -f /var/log/nginx/access.log -p /etc/goaccess_log_conf_nginx.conf -o /usr/local/nginx/report/index.html --ignore-crawlers --hour-spec=hour |
| 84 | +
|
| 85 | +
|
| 86 | +不显示指定的面板 |
| 87 | +goaccess -f /var/log/nginx/access.log -p /etc/goaccess_log_conf_nginx.conf -o /usr/local/nginx/report/index.html --ignore-crawlers --hour-spec=min \ |
| 88 | + --ignore-panel=VISITORS \ |
| 89 | + --ignore-panel=REQUESTS \ |
| 90 | + --ignore-panel=REQUESTS_STATIC \ |
| 91 | + --ignore-panel=NOT_FOUND \ |
| 92 | + --ignore-panel=HOSTS \ |
| 93 | + --ignore-panel=OS \ |
| 94 | + --ignore-panel=BROWSERS \ |
| 95 | + --ignore-panel=VIRTUAL_HOSTS \ |
| 96 | + --ignore-panel=REFERRERS \ |
| 97 | + --ignore-panel=REFERRING_SITES \ |
| 98 | + --ignore-panel=KEYPHRASES \ |
| 99 | + --ignore-panel=STATUS_CODES \ |
| 100 | + --ignore-panel=REMOTE_USER \ |
| 101 | + --ignore-panel=GEO_LOCATION |
| 102 | +
|
| 103 | +我一般只留下几个面板(排除掉不想看的面板,因为使用 --enable-panel 参数无法达到这个目的) |
| 104 | +goaccess -f /var/log/nginx/access.log -p /etc/goaccess_log_conf_nginx.conf -o /usr/local/nginx/report/index.html --ignore-crawlers --hour-spec=min \ |
| 105 | + --ignore-panel=VISITORS \ |
| 106 | + --ignore-panel=REQUESTS_STATIC \ |
| 107 | + --ignore-panel=NOT_FOUND \ |
| 108 | + --ignore-panel=OS \ |
| 109 | + --ignore-panel=VIRTUAL_HOSTS \ |
| 110 | + --ignore-panel=REFERRERS \ |
| 111 | + --ignore-panel=KEYPHRASES \ |
| 112 | + --ignore-panel=REMOTE_USER \ |
| 113 | + --ignore-panel=GEO_LOCATION |
| 114 | +``` |
| 115 | + |
| 116 | +#### 方便执行命令创建脚本 |
| 117 | + |
| 118 | +- `vim goaccess_report_by_min.sh` |
| 119 | + |
| 120 | +``` |
| 121 | +goaccess -f /var/log/nginx/access.log -p /etc/goaccess_log_conf_nginx.conf -o /usr/local/nginx/report/index.html --ignore-crawlers --hour-spec=min \ |
| 122 | + --ignore-panel=VISITORS \ |
| 123 | + --ignore-panel=REQUESTS_STATIC \ |
| 124 | + --ignore-panel=NOT_FOUND \ |
| 125 | + --ignore-panel=OS \ |
| 126 | + --ignore-panel=VIRTUAL_HOSTS \ |
| 127 | + --ignore-panel=REFERRERS \ |
| 128 | + --ignore-panel=KEYPHRASES \ |
| 129 | + --ignore-panel=REMOTE_USER \ |
| 130 | + --ignore-panel=GEO_LOCATION |
| 131 | +``` |
| 132 | + |
| 133 | +- `vim goaccess_report_by_hour.sh` |
| 134 | + |
| 135 | +``` |
| 136 | +goaccess -f /var/log/nginx/access.log -p /etc/goaccess_log_conf_nginx.conf -o /usr/local/nginx/report/index.html --ignore-crawlers --hour-spec=hour \ |
| 137 | + --ignore-panel=VISITORS \ |
| 138 | + --ignore-panel=REQUESTS_STATIC \ |
| 139 | + --ignore-panel=NOT_FOUND \ |
| 140 | + --ignore-panel=OS \ |
| 141 | + --ignore-panel=VIRTUAL_HOSTS \ |
| 142 | + --ignore-panel=REFERRERS \ |
| 143 | + --ignore-panel=KEYPHRASES \ |
| 144 | + --ignore-panel=REMOTE_USER \ |
| 145 | + --ignore-panel=GEO_LOCATION |
| 146 | +``` |
| 147 | + |
| 148 | +#### 实时生成统计页面 |
| 149 | + |
| 150 | +- 我个人看法是:一般没必要浪费这个性能,需要的时候执行下脚本就行了。 |
| 151 | +- 官网文档:<https://goaccess.io/man#examples>,查询关键字:**REAL TIME HTML OUTPUT** |
| 152 | + |
| 153 | +``` |
| 154 | +goaccess -f /var/log/nginx/access.log -p /etc/goaccess_log_conf_nginx.conf -o /usr/local/nginx/report/index.html --real-time-html --daemonize |
| 155 | +``` |
| 156 | + |
| 157 | +## 资料 |
| 158 | + |
| 159 | +- <https://www.fanhaobai.com/2017/06/go-access.html> |
| 160 | +- <https://www.imydl.tech/lnmp/32.html> |
0 commit comments