nginx切割日志并使用awstats分析
#!/bin/bash #function:cut nginx log files for lnmp v0.5 and v0.6 #author: http://lnmp.org #set the path to nginx log files log_files_path="/usr/local/nginx/logs/" log_files_dir=${log_files_path}history/$(date -d "yesterday" +"%Y")/$(date -d "yesterday" +"%m") #set nginx log files you want to cut log_files_name=(access error) #set the path to nginx. nginx_sbin="/usr/local/nginx/sbin/nginx" #Set how long you want to save save_days=365 ############################################ #Please do not modify the following script # ############################################ mkdir -p $log_files_dir log_files_num=${#log_files_name[@]} #cut nginx log files for((i=0;i<$log_files_num;i++));do mv ${log_files_path}${log_files_name[i]}.log ${log_files_dir}/${log_files_name[i]}_$(date -d "yesterday" +"%Y%m%d").log done #delete 365 days ago nginx log files find $log_files_path -mtime +$save_days -exec rm -rf {} \; $nginx_sbin -s reload
凌晨执行日志切割:
crontab -e 00 00 * * * /bin/bash /usr/local/nginx/sbin/cut_nginx_logs.sh
日志切割好了之后,就可以使用awstats分析日志了,安装awstats,下载后解压到/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 ##进入目录 执行配置 <code>cd</code> <code>/usr/local/awstats/tools <code>.</code><code>/awstats_configure</code><code>.pl</code></code>
将会有如下一些提示:
-----> Check for web server install Enter full 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
回车之后下一个选项
Your web server config file(s) could not be found. You will need to setup your web server manually to declare AWStats script as a CGI, if you want to build 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 a new AWStats config/profile file (required if first install) [y/N] ?
#这里选Y,创建一个新的配置文件
-----> Define config file name to create What is the name of your web site or profile analysis ? Example: www.mysite.com Example: demo Your web site, virtual server or profile name: >akii.org #这里输入你要分析的域名,或是随便一个你易记的配置名并回车
接下来要定义你的配置文件存放的路径,可用默认
-----> Define config file path In which directory do you plan to store your config file(s) ? Default: /etc/awstats Directory path to store config file(s) (Enter for default): > #直接回车,使用默认路径/etc/awstats
回车后的提示
-----> Create config file '/etc/awstats/awstats.akii.org.conf' Config file /etc/awstats/awstats.akii.org.conf created. -----> Add update process inside a scheduler Sorry, configure.pl does not support automatic add to cron yet. You can do it manually by adding the following command to your cron: /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=akii.org Or if you have several config files and prefer having only one command: /usr/local/awstats/tools/awstats_updateall.pl now Press ENTER to continue... #按回车继续 A SIMPLE config file has been created: /etc/awstats/awstats.akii.org.conf You should have a look inside to check and change manually main parameters. You can then manually update your statistics for 'yuyuanchun.com' with command: > perl awstats.pl -update -config=akii.org You can also build static report pages for 'akii.org' with command: > perl awstats.pl -output=pagetype -config=akii.org Press ENTER to finish... #回车完成配置文件的创建
完成配置文件的创建后,我们还要修改一下。因为我们是按天切割的日志,切割完成后交由awstats去分析。并不是让awstats去分时正在时时增长的也就是正在被写入的日志,这样的好处是不至于遗漏数据,并且分析已经切割完成的日志,更不用担心会有冲突。坏处是我一天切割一次日志,你要等第二天才能看昨天的一些详细数据。
修改/etc/awstats/awstats.akii.org.conf,执行:
vi /etc/awstats/awstats.akii.org.conf
找到
LogFile="/var/log/httpd/mylog.log"
修改为
LogFile="/home/www/logs/%YYYY-24/%MM-24/%DD-24/akii.org_access.log"
如果你的日志路径和我的不一样,请修改成对应的日志文件名。以上的完整路径是切割后保存的nginx日志文件。其中%YYYY-24/%MM-24/%DD-24表示年月日都减去24小时,也就是昨天的日志目录。修改完成后按:wq保存退出。
接下来可以测试一下awstats分析日志了(前提是你已经有了切割过的日志,没有的话可以先退行一下切割日志的脚本/root/cut_log.sh)
首先,还要创建一个awstats用于记录数据的目录
mkdir -p /var/lib/awstats
然后运行awstats的wwwroot目录中的awatsts.pl来测试一下
/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=akii.org
你如果看到类似下面的提示就说明配置文件都正确了。
Create/Update database for config "/etc/awstats/awstats.akii.org.conf" by AWStats version 7.0 (build 1.964) From data in log file "/home/www/logs/2010/07/24/akii.org_access.log"... Phase 1 : First bypass old records, searching new record... Direct access after last parsed record (after line 43260) Jumped lines in file: 43260 Found 43260 already parsed records. Parsed lines in file: 0 Found 0 dropped records, Found 0 comments, Found 0 blank records, Found 0 corrupted records, Found 0 old records, Found 0 new qualified records
统计分析完成后,结果还在 Awstats 的数据库中。在 Apache 上,可以直接打开 Perl 程序的网页查看统计。 但本文开始时已经提到,Nginx 对 Perl 支持并不好,所以我们要换个方法,利用 awstats 的工具将统计的结果生成静态文件,具体的步骤如下:
- 首先在 webroot 目录下创建一个文件夹。例:/home/www/awstats
- 写一个脚本,定期执行让 Awstats 把静态页面生成到该目录中
先生成存放awstats生成的静态文件的目录,我这里用的是/home/www/awstats
mkdir -p /home/www/awstats
我们来写一个脚本
vim /root/awstats.sh
然后输入以下内容
#!/bin/bash mkdir -p /home/www/awstats/akii.org /usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=akii.org -lang=cn -dir=/home/www/awstats/akii.org -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl
上述命令的具体意思如下:
- /usr/local/awstats/tools/awstats_buildstaticpages.pl Awstats 静态页面生成工具
- -update -config=akii.org 更新配置项
- -lang=cn 语言为中文
- -dir=/home/www/awstats 统计结果输出目录
- -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl Awstats 日志更新程序路径。
然后在你的nginx的配置文件中,在你想要安置awstats或默认的ip或域名的server段中,加入关于awstats和icon的两个目录配置。
如一个完整案例:
server { listen 80; server_name localhost; root /home/www; index index.html; location ~ ^/awstats/ { # awstats 静态页面目录 root /home/www/awstats; autoindex on; #可以目录浏览你的多个域名的目录用于分析 index index.html; access_log off; } location ~ ^/icon/ { # 图标目录 root /usr/local/awstats/wwwroot; index index.html; access_log off; } }
接下来可以测试一下脚本是否可以正确执行
还是别忘了给它可执行权限
chmod +x /root/awstats.sh /root/awstats.sh
如果你看到它生成了一堆网页,那就说明成功了。
输出信息部分例如
Launch update process : "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=akii.org -update -configdir= ...... Build keywords page: "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=akii.org -staticlinks -lang=cn -output=keywords Build errors404 page: "/usr/local/awstats/wwwroot/cgi-bin/awstats.pl" -config=akii.org -staticlinks -lang=cn -output=errors404 20 files built. Main HTML page is 'awstats.akii.org.html'.
然后可以把它加入自动运行了。
同时也要加上密码保护
nginx可以为网站或目录甚至特定的文件设置密码认证。密码必须是crypt加密的。可以用apache的htpasswd来创建密码。
格式为:htpasswd -b -c site_pass username password
site_pass为密码文件。放在同nginx配置文件同一目录下,当然你也可以放在其它目录下,那在nginx的配置文件中就要写明绝对地址或相对当前目录的地址。
如果你输入htpasswd命令提示没有找到命令时,你需要安装httpd.如centos是yum install httpd
如果是为了给网站加上认证,可以直接将认证语句写在nginx的配置server段中。
如果是为了给目录加上认证,就需要写成目录形式了。同时,还要在目录中加上php的执行,否则php就会被下载而不执行了。
针对目录的认证,在一个单独的location中,并且在该location中嵌套一个解释php的location,否则php文件不会执行并且会被下载。auth_basic在嵌套的location之后。
server { listen 80; server_name www.akii.org akii.org; root /www/akii; index index.html index.htm index.php; location ~ ^/admin/.* { location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; } auth_basic "auth"; auth_basic_user_file /usr/local/nginx/conf/vhost/auth/admin.pass; } location ~ .php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; } location ~ /\.ht { deny all; } access_log /logs/akii.org_access.log main; }
这里有一个细节,就是location ~ ^/admin/.* {…} 保护admin目录下的所有文件。如果你只设了/admin/ 那么直接输入/admin/index.php还是可以访问并且运行的。 ^/admin/.* 意为保护该目录下所有文件。当然,只需要一次认证。并不会每次请求或每请求一个文件都要认证一下。
下载: cut_nginx_logs
本文出自weicms.net,转载时请注明出处及相应链接。
本文永久链接: http://www.weicms.net/2012/12/20/cut_nginx_logs.html