nginx日志格式设定,去掉如下行#号

#vim /usr/local/nginx/conf/nginx.conf
 log_format access '$remote_addr -$remote_user [$time_local] "$request" '
 '$status $body_bytes_sent"$http_referer" '
 '"$http_user_agent"$http_x_forwarded_for';
 access_log log/access.log main;

参数说明:

 $remote_addr 与$http_x_forwarded_for 用以记录客户端的ip地址;
 $remote_user :用来记录客户端用户名称;
 $time_local :用来记录访问时间与时区;
 $request :用来记录请求的url与http协议;
 $status :用来记录请求状态;成功是200,
 $body_bytes_s ent :记录发送给客户端文件主体内容大小;
 $http_referer :用来记录从那个页面链接访问过来的;
 $http_user_agent :记录客户毒啊浏览器的相关信息;

nginx日志切割脚本

#vim /usr/local/nginx/nginx_log.sh
 #!/bin/bash
 logs_path="/usr/local/nginx/logs/" #设置日志文件存放目录
 pid_path="/usr/local/nginx/logs/nginx.pid" #设置pid文件
 mv${logs_path}access.log ${logs_path}access_$(date -d "yesterday" +"%Y%m%d").log
 #重命名日志文件,将access.log切割为昨天的日志文件,因为要在每天零点切割,所有日志文件格式是前一天的日期,如access_20130728.log
 kill-USR1 `cat${pid_path}` #向nginx主进程发信号重新打开日志

安装和配置awstats

wget http://awstats.sourceforge.net/files/awstats-7.0.tar.gz
tar -xvf awstats-7.0.tar.gz
mv awstats-7.0 /usr/local/awstats
chown -R root:root /usr/local/awstats
chmod -R =rwX /usr/local/awstats
chmod +x /usr/local/awstats/tools/\*.pl
chmod +x /usr/local/awstats/wwwroot/cgi-bin/\*.pl 执行 tools 目录中的 awstats_configure.pl 配置向导,创建一个新的统计
cd /usr/local/awstats/tools
./awstats_configure.pl
将会有如下一些提示:
-----> Running OS detected: Linux, BSD orUnix
----->Check for web server install
Enterfull config file path of your Web server.
Example:/etc/httpd/httpd.conf
Example:/usr/local/apache2/conf/httpd.conf
Example:c:\Program files\apache group\apache\conf\httpd.conf Config file path ('none' to skip web server setup):
>none #这里添none并回车,因为我们没有使用apache
回车之后下一个选项:
Yourweb server config file(s) could not be found.
Youwill need to setup your web server manually to declare AWStats
script as a CGI, if you want tobuild reports dynamically. See AWStats setup documentation (file docs/index.html)
----->Update model config file '/usr/local/awstats/wwwroot/cgi-bin/awstats.model.conf' File awstats.model.conf updated. ----->Need to create a new config file ? Do you want me to build anew AWStats config/profilefile (required if first install) [y/N] ? y #这里选Y,创建一个新的配置文件 -----> Define config file name to create
What is the name of your web site or profile analysis ?
Example: www.mysite.com
Example: demo
Yourweb site, virtual server or profile name:
>nginx #这里输入你要分析的域名,或是随便一个你易记的配置名并回车 ----->Define config file path In which directory do you plan to store your config file(s) ?
Default: /etc/awstats
Directorypath to store config file(s) (Enter fordefault):
> #直接回车,定义你的配置文件存放的路径,使用默认路径/etc/awstats ----->Create config file '/etc/awstats/awstats.nginx.conf'
Configfile /etc/awstats/awstats.nginx.conf created.
-----> Add updateprocess inside a scheduler Sorry, configure.pl does not support automatic addto cron yet.
You can do it manually by adding the following command to yourcron: /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=nginx
Or if you have several config files and prefer having only onecommand: /usr/local/awstats/tools/awstats_updateall.pl now
Press ENTER to continue...#按回车继续
A SIMPLE config file has been created: /etc/awstats/awstats.nginx.conf You should have a lookinside to check and change manually main parameters.
Youcan then manually update your statistics for'yuyuanchun.com' withcommand: > perl awstats.pl -update -config=nginx
Youcan also build static report pages for'nginx' with command:> perl awstats.pl -output=pagetype -config=nginx
PressENTER to finish... #回车完成配置文件的创建

默认会生成一个名为awstats.nginx.conf配置文件在/etc/awstats/目录下,修改该配置文件的日志位置

vim /etc/awstats/awstats.nginx.conf
LogFile="/usr/local/nginx/logs/access\_%YYYY-0%MM-0%DD-24.log"
这里是对应上面Nginx日志切割所生成的目录存放位置,注意awstats的年月日格式,-24表示昨天的日志,-0表示当前的
分析的执行顺序是:
 Nginx 产生日志 –> 日志切割 –> Nginx 继续产生日志 –> 另存切割日志 –> 交由Awstats统计 –> 生成结果

日志说明

# "LogFile" contains the web, ftp or mail server logfile to analyze.
#You can also use tags inthis filename if you need a dynamic file name
#depending on date or time (Replacement is made by AWStats at the beginning
#ofits execution). This is available tags :
# %YYYY-n is replaced with 4 digits year we were n hours ago
# %YY-n is replaced with 2 digits year we were n hours ago
# %MM-n is replaced with 2 digits month we were n hours ago
# %MO-n is replaced with 3 letters month we were n hours ago
# %DD-n is replaced with day we were n hours ago
# %HH-n is replaced with hour we were n hours ago
# %NS-n is replaced with number of seconds at 00:00 since 1970
# %WM-n is replaced with the week number in month (1-5)
# %Wm-n is replaced with the week number in month (0-4)
# %WY-n is replaced with the week number in year (01-52)
# %Wy-n is replaced with the week number in year (00-51)
# %DW-n is replaced with the day number in week (1-7, 1=sunday)
#use n=24if you need (1-7, 1=monday)
# %Dw-n is replaced with the day number in week (0-6, 0=sunday)
#use n=24if you need (0-6, 0=monday)
#Use 0for n if you need current year, month, day, hour...
n表示时间间隔,我这里就是分析距离当前今天24小时前(昨日)的日志,改成48就是前两天的日志

创建一个awstats用于记录数据的目录

mkdir -p /var/lib/awstats
然后运行awstats的wwwroot目录中的awatsts.pl来测试一下
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=nginx

出现报错:

Fromdata in log file "/usr/local/nginx/logs/access_20131111.log"...
Error:Couldn't open server log file "/usr/local/nginx/logs/access_20131111.log" : 没有那个文件或目录
解决办法:
touch /usr/local/nginx/logs/access_20131111.log
再次执行
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=nginx
如果看到类似下面的提示就说明配置文件都正确了
Create/Updatedatabase for config "/etc/awstats/awstats.nginx.conf" by AWStats version 7.0 (build1.964) From data
in log file "/home/wwwlogs/access.log_20130727"... Phase 1 : First bypass old records, searching newrecord...
Directaccess after last parsed record (after line 43260) Jumped lines in file: 43260 Found 43260already parsed records.
Parsedlines in file: 0
Found0 dropped records,
Found0 comments,
Found0 blank records,
Found0 corrupted records,
Found0 old records,
Found0new qualified records

Nginx 对 Perl 支持并不好,所以这里利用 awstats 的工具将统计的结果生成静态文件

首先在 webroot 目录下创建一个文件夹。例:/home/www/awstats
mkdir -p /home/www/awstats

生成静态文件:

/usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=nginx -lang=cn-dir=/home/www/awstats/ -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
运行脚本如果出现生成一堆网页类似为正确
Launchupdate process : "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=nginx -update -configdir= ......
Buildkeywords page: "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=nginx -staticlinks
-lang=cn-output=keywords Build errors404 page: "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl"
-config=nginx-staticlinks -lang=cn -output=errors404 20 files built.
Main HTML page is'awstats.nginx.html'. /usr/local/awstats/tools/awstats_buildstaticpages.pl Awstats静态页面生成工具
-update -config=nginx 更新配置项
-lang=cn 语言为中文
-dir=/home/www/awstats 统计结果输出目录
-awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl Awstats 日志更新程序路径

修改nginx配置文件,添加日志分析结果站点

#vim /usr/local/nginx/conf/nginx.conf
在最末尾添加如下行
server
       {
               listen       8080;
               server_name localhost;
               index index.html index.htmindex.php default.html default.htm default.php;
               root  /home/www/awstats;               location ~ ^/awstats/ {    #html静态页面目录
                root   /home/www/awstats;
                index  index.html;
                access_log off;
                               }                location ^~ /icon/ {    #图标目录
                root   /usr/local/awstats/wwwroot;
                index  index.html;
                access_log off;
                               }              autoindex on;        #开启目录浏览功能
               access_log off;
       }

重新加载nginx

ln -s /usr/local/nginx/sbin/nginx /usr/bin/
nginx –t #检查nginx配置文件是否有错误
nginx –s reload #加载

添加定时任务,每晚切割,分析nginx日志

# vim /usr/local/nginx/awstats.sh
#!/bin/bash
/bin/bash /usr/local/nginx/nginx_log.sh
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl-update -config=nginx
/usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=nginx -lang=cn-dir=/home/www/awstats/ -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl #chmod +x /usr/local/nginx/awstats.sh
#crontab -e
01 00 * * * /usr/local/nginx/awstats.sh

这样访问http://192.168.233.210:8080 就可以看到统计结果静态页面了。

awstats日志分析的更多相关文章

  1. awstats 日志分析

    /tmp/awstats/awstats.ezrydel.com.conf LogFile="/usr/local/apache/domlogs/ezrydel.com" php版 ...

  2. AWStats日志分析系统

    1.什么是AWStats AWStats是一款功能强大且功能强大的免费工具,可以图形方式生成高级Web,流媒体,ftp或邮件服务器统计信息.此日志分析器用作CGI或命令行, 并在几个图形网页中显示您的 ...

  3. awstats 日志分析工具linux下的安装和使用

    合并日志文件可以使用 bash 的sort命令: -o log_all access*.log 也可以使用  awstats 提供的 logresolvemerge.pl -showsteps acc ...

  4. astats日志分析系统

    Awstats是一个免费非常简洁而且强大有个性的网站日志分析工具. 功能: 一:访问量,访问次数,页面浏览量,点击数,数据流量等 二:精确到每月.每日.每小时的数据 三:访问者国家 四:访问者IP 五 ...

  5. Linux 日志分析工具之awstats

    一.awstats 是什么 官方网站:AWStats is a free powerful and featureful tool that generates advanced web, strea ...

  6. windows下安装awstats来分析apache的访问日志

    一.啰嗦两句 之前在Windows下用Apache时,也曾经配置过Awstats,然后换了工作,改用Linux+nginx,渐渐把Apache忘记了.又换了工作,又得用Apache,这回版本更新到2. ...

  7. 《Unix/Linux日志分析与流量监控》书稿完成

    <Unix/Linux日志分析与流量监控>书稿完成 近日,历时3年创作的75万字书稿已完成,本书紧紧围绕网络安全的主题,对各种Unix/Linux系统及网络服务日志进行了全面系统的讲解,从 ...

  8. 【Linux】日志分析及管理

    日志的作用   用于记录系统.程序运行中发生的各种事件   eg: [root@localhost ~]# yum install -y httpd [root@localhost ~]# tail ...

  9. 【分享】Nginx日志分析(上)

    在很多时候,我们会非常关注网站的访问量,比如网站的日PV是多少.网站某个功能上线之后点击量是多少,像这些东西都是需要从web容器中的访问日志统计出来的,下面我们看一下如何在nginx中统计网站的访问信 ...

随机推荐

  1. poj2155二维树状数组区间更新

    垃圾poj又交不上题了,也不知道自己写的对不对 /* 给定一个矩阵,初始化为0:两种操作 第一种把一块子矩阵里的值翻转:0->1,1->0 第二种询问某个单元的值 直接累计单元格被覆盖的次 ...

  2. hibernate 多对一(级联)操作

    级联:当你存储一个表的内容想值得相关联的表也存储数据时,可以通过级联来实现(cascade)@Entity@Table(name="t_User")public class Use ...

  3. requirejs模块路径配置问题

    三种情况:一.设置data-main,没配置baseUrl,以data-main的文件为基准:二.设置data-main,配置baseUrl,baseUrl以值以引用require.js的HTML为基 ...

  4. [AH2017/HNOI2017]礼物

    题解: 水题 化简一波式子会发现就是个二次函数再加上一个常数 而只有常数中的-2sigma(xiyi)是随移动而变化的 所以只要o(1)求出二次函数最大值然后搞出sigma(xiyi)就可以了 这个东 ...

  5. 【开源小软件 】Bing每日壁纸 让桌面壁纸保持更新

    发布一个开源小软件,Bing每日壁纸. 该小软件可以自动获取Bing的精美图片设置为壁纸,并且支持随机切换历史壁纸,查看壁纸故事. 欢迎大家下载使用,点star!有问题请留言或者提issue. 开源地 ...

  6. 用VScode代码调试Python

    Python扩展支持许多类型的Python应用程序的调试,包括以下一般功能: 观看窗口 评估表达式 当地人 参数 扩大孩子 断点 条件断点 暂停(进入)正在运行的程序 自定义启动目录 要熟悉这些常规功 ...

  7. js算法初窥06(算法模式03-函数式编程)

    在解释什么是函数式编程之前,我们先要说下什么是命令式编程,它们都属于编程范式的一种.命令式编程其实就是一块一块的代码,其中包括了我们要执行的逻辑或者判断或者一些运算.也就是按部就班的一步一步完成我们所 ...

  8. vue 百度地图实现标记多个maker,并点击任意一个maker弹出对应的提示框信息, (附: 通过多个地址,标记多个marker 的 方法思路)

    通过点击不同筛选条件,筛选出不同企业所在的地点, 根据每个企业的经纬度 在地图上标记多个maker,点击任意一个maker,会弹出infoWindow 信息窗口: 说明:  因每个人写法不同.需求不同 ...

  9. loj#2665. 「NOI2013」树的计数

    目录 题目链接 题解 代码 题目链接 loj#2665. 「NOI2013」树的计数 题解 求树高的期望 对bfs序分层 考虑同时符合dfs和bfs序的树满足什么条件 第一个点要强制分层 对于bfs序 ...

  10. [Beijing wc2012]算不出的算式

    OJ题号:BZOJ2659 思路:数学. 建立平面直角坐标系.在第一象限作直线y=qx/p,易得Σ[kq/p]即为当x<(p/2)时,直线下方(包括直线)的整点数:Σ[kp/q]为当y<( ...