So Tired!_! for our good life, must to do something. let's do it!

8二/12

GoAccess,Apache/Nginx 日志统计分析软件

发布在 Nick Xu

什么是GoAccess?

GoAccess是运行在Unix系统终端,开源、实时分析Apache日志(其实也能够分析Nginx日志)并且能够动态呈现的软件。它为系统管理员提供了一个快速、有价值的HTTP统计,动态的可视化的服务器报告。

它是做什么以及如何工作?

从本质上讲,GoAccess的工作方式是,它将解析众所周知的Apache访问日志文件 access log,GoAccess通过解析日志收集的数据,将会显示它的控制台或某个终端。收集到的信息或生成的报告将显示给在视觉、交互窗口的系统管理员用户。报告包括:

  • 综合统计
  • 访客排行榜
  • 请求文件排行榜
  • 请求的静态文件排行榜,如图片、JS文件、CSS文件等
  • 访问来源
  • 404页面
  • 访问者的操作系统
  • 访问者的浏览器
  • 浏览器和蜘蛛(搜索爬虫)
  • 主机,反向DNS,IP位置
  • HTTP状态码
  • 推介网站
  • 搜索关键词
  • 显示时不同的配色方案
  • 无限制日志文件的大小(决定了GoAccess的打开速度)

日志文件格式

GoAccess可以解析Apache的日志格式,通用日志格式(CLF)和组合日志格式(XLF/ ELF),包括虚拟主机. 它也能够解析Nginx日志. (如果你按照Apache日志的格式配置了Nginx日志)

GoAccess主页 http://goaccess.prosoftcorp.com/

安装方法

1、安装GoAccess需要一些系统支持库

Ncurses 提供字符终端处理库,包括面板和菜单
GLib C语言的函数库(大于2.0的版本)
GeoIP 通过IP,定位他的经纬度,国家/地区,省市,甚至街道等位置信息。(如果不需要这个不是必须的,配置时候去掉这个选项 –enable-geoip)

CentOS下yum安装

yum install ncurses-devel
yum install glib2-devel

Ubuntu下apt安装

apt-get install libglib2.0-dev
apt-get install libncursesw5-dev

2、安装GoAccess

CentOS下安装

wget http://sourceforge.net/projects/goaccess/files/0.4.2/goaccess-0.4.2.tar.gz
tar zxvf goaccess-0.4.2.tar.gz
cd goaccess-0.4.2
./configure --enable-utf8
make
make install

Ubuntu下安装

apt-get install goaccess

GoAccess使用介绍

启动参数介绍:

  • -b 流量消耗统计,如果想要提高解析速度,不要开启这个选项。缺省值为不开启此选项
  • -f 日志文件路径。
  • -s HTTP 状态码报告,为了能够更好的解析日志,选项被默认关闭。
  • -e 指定IP地址的访问统计。
  • -a 通过代理的请求统计。

菜单介绍

  • F1 帮助菜单
  • F5 刷新
  • q 退出当前口窗、菜单、或是当前查看的选项
  • o 打开当前的选项、菜单
  • c 改变窗口配色(目前只有两种 默认和绿色)
  • SHIFT + TAB 从当前选定模块向后切换
  • RIGHT 打开当前选中模块,查看详细信息
  • s 通过日期排序,只会在访问请求模块起作用
  • S 通过点击次数排序,只会在访问请求模块起作用
  • / 查看详细信息的窗口进行搜索
  • n 通过/进行查找后,查找下个匹配的内容的位置,如果没有则在窗口底部显示“search hit BOTTOM”
  • t 在查看详细信息窗口,移动指针到最顶部
  • b 在查看详细信息窗口,移动指针到最底部

使用实例

  • 最简单、快速的使用方式
goaccess -f /data1/logs/blog.wuwangwo.net/access.log
  • 产生完整、全面统计数据的使用方式
goaccess -f /data1/logs/blog.wuwangwo.net/access.log -a -s -b
  • 通过管道的方式将更多access log 通过GoAccess解析
zcat access.log.*.gz | goaccess

或者

zcat -f access.log* | goaccess
  • 另外一种通过管道使用GoAccess解析日志的方式
sed -n '/05/Dec/2010/,$ p' access.log | goaccess -s -b
  • 低优先级运行GoAccess的方式
nice -n 19 goaccess -f access.log -s -a -b
  • 如果你只把GoAccess安装到了一台服务器上
ssh user@server 'cat /var/log/apache2/access.log' | goaccess -s -a -b

注意事项

  1. 每一个详细信息窗口,只显示300条数据
  2. GoAccess 通过管道解析日志时将禁用实时解析的功能

总结

GoAceess 不是很大的一款软件却有着强大的功能即使软件可能还有些BUG,软件的更多介绍信息请查看GoAccess的官方网站«./GoAccess»,当前版本的官方手册页面 Man Page

8二/12

Nginx Block And Deny IP Address OR Network Subnets

发布在 Nick Xu

How do I block or deny access based on the host name or IP address of the client visiting website under nginx web server?

Nginx comes with a simple module called ngx_http_access_module to allow or deny access to IP address. The syntax is as follows:

deny IP;
deny subnet;
allow IP;
allow subnet;
# block all ips
deny    all;
# allow all ips
allow    all;
Note rules are checked in the order of their record to the first match.

How Do I Configure Nginx To Block IPs?

Edit nginx.conf file, enter (note my nginx path is set to /usr/local/nginx/, replace this according to your setup):
# cd /usr/local/nginx/conf/
# vi nginx.conf

Add the following line in http section:

## Block spammers and other unwanted visitors  ##
include blockips.conf;
Save and close the file. Finally, create blockips.conf in /usr/local/nginx/conf/, enter:
# vi blockips.conf

Append / add entries as follows:

deny 1.2.3.4;
deny 91.212.45.0/24;
deny 91.212.65.0/24;

Save and close the file. Test the config file, enter:
# /usr/local/nginx/sbin/nginx -t

Sample outputs:

the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
configuration file /usr/local/nginx/conf/nginx.conf test is successful
Reload the new config, enter:
# /usr/local/nginx/sbin/nginx -s reload

How Do I Deny All and Allow Only Intranet/LAN IPs?

Edit config file as follows:

location / {
# block one workstation
deny    192.168.1.1;
# allow anyone in 192.168.1.0/24
allow   192.168.1.0/24;
# drop rest of the world
deny    all;
}
Granted access to network 192.168.1.0/24 with the exception of the address 192.168.1.1.

How Do I Customize HTTP 403 Forbidden Error Messages?

Create a file called error403.html in default document root, enter:
# cd /usr/local/nginx/html
# vi error403.html

<html>
<head><title>Error 403 - IP Address Blocked</title></head>
<body>
Your IP Address is blocked. If you this an error, please contact webmaster with your IP at webmaster@example.com
</body>
</html>

If SSI enabled, you can display the client IP easily from the html page itself:

Your IP Address is <!--#echo var="REMOTE_ADDR" --> blocked.
Save and close the file. Edit your nginx.conf file, enter:
# vi nginx.conf

# redirect server error pages to the static page
error_page   403  /error403.html;
location = /error403.html {
root   html;
}
Save and close the file. Reload nginx, enter:
# /usr/local/nginx/sbin/nginx -s reload

See also:
Nginx Shell Script To Block Spamhaus Lasso Drop Spam IP Address

   
site
site