Nginx配置文件实现AWStats静态页面

开发 前端
Nginx配置文件一直以来都需要我们不断的学习,那么我们如何才Nginx配置文件中实现AWStats静态页面的效果呢?下面就会向大家详细的介绍。

Nginx配置文件在有些时候需要我们不断的更新,虽然AWStats现在已经支持输出静态网页形式的分析结果,但是页面布局没有用Perl生成的动态网页方便,而且用自带的用Perl编写的转换工具稍微有点慢,在流量不大的情况下,还是在Perl+Fast CGI上运行更舒服一些。

首先要参考之前的文章,建立好PERL+FCGI的运行环境,这步准备工作就够费劲的。

然后开始切入正题:

1. 下载***版的AWStats,基本就是Perl包,所以没必要用apt-get,解压缩到/usr/local/awstats下

wget http://prdownloads.sourceforge.net/awstats/awstats-6.95.tar.gz
tar -xzf awstats-6.95.tar.gz
mv awstats-6.95 /usr/local/awstats

2. 创建一个存放awstats分析数据的目录

mkdir /var/lib/awstats
chown www-data /var/lib/awstats //这是为了让awstats页面上能直接刷新***数据

3. 自动配置awstats

cd /usr/local/awstats/tools
perl awstats_configure.pl

除了***步因为是Ngin服务器的关系,所以要选none,其他基本按照提示选默认值

4. 手工编辑Nginx配置文件

1) 修改LogFile路径

LogFile = “/var/log/Nginx/access.log”

如果是压缩格式的日志,可以用LogFile = “zcat /var/log/Nginx/%YYYY-24%MM-24%DD-24.gz|"。这里用zcat是因为其使用管道输出,对系统资源消耗比较小,千万不要忘了***的管道操作符!

假设原来/etc/Nginx/Nginx.conf中关于log部分是如此定义的:(要小心各个变量之间必须添加的空格,不能少,否则awstats就不认了)

log_format main ‘$remote_addr $remote_user [$time_local] “$request” $status ‘
‘$host $body_bytes_sent $gzip_ratio “$http_referer” ‘
‘”$http_user_agent” “$http_x_forwarded_for”‘;

很容易知道,对应awstats配置文件中,LogFormat应该设置为
LogFormat= “%host %logname %time1 %methodurl %code %host_r %bytesd %gzipratio %refererquot %uaquot %otherquot”

***一个选%otherquot是应为$http_x_forwarded_for在AWstats 6.95中还不认识

3) 将AllowToUpdateStatsFromBrowser=1,便于浏览器中即时刷新数据

5. 更新Awstats的分析记录,测试一下刚才的配置

/usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=yousite.com

如果一切正常,应该看到类似以下的结果:

Create/Update database for config “/etc/awstats/awstats.yoursite.com.conf” by AWStats version 6.95 (build 1.943)
From data in log file “/var/log/Nginx/access.log”…
Phase 1 : First bypass old records, searching new record…
Searching new records from beginning of log file…
Phase 2 : Now process new records (Flush history on disk after 20000 hosts)…
Jumped lines in file: 0
Parsed lines in file: 1234
Found 0 dropped records,
Found 0 corrupted records,
Found 0 old records,
Found 1234 new qualified records.

6. 修改logrotate.d下的Nginx配置文件,在每天切割日志前,更新awstats状态

  1. /var/log/nginx/*.log {  
  2. daily  
  3. missingok  
  4. rotate 7  
  5. compress  
  6. delaycompress  
  7. notifempty  
  8. create 640 www-data www-data  
  9. dateext  
  10. sharedscripts  
  11. prerotate  
  12. /usr/local/awstats/wwwroot/cgi-bin/awstats.pl -update -config=yoursite.com  
  13. sleep 59  
  14. endscript  
  15. postrotate  
  16. if [ -f /var/run/nginx.pid ]; then  
  17. kill -USR1 `cat /var/run/nginx.pid`  
  18. fi  
  19. endscript  

7. 接下来是最关键的NGINX配置文件

 

  1. #AWStatus Perl CGI server  
  2. server {  
  3. listen 80;  
  4. server_name awstats.yoursite.com;  
  5. access_log /var/log/nginx/awstats.log main;  
  6. error_log /var/log/nginx/awstats_error.log; #这可以为fail2ban留下记录  
  7. root /usr/local/awstats/wwwroot;  
  8. auth_basic “Restricted”;  
  9. auth_basic_user_file /etc/nginx/awstatus.pass;  
  10. location = / {  
  11. rewrite ^ /awstats.pl?config=freshventure.info;  
  12. }  
  13. location ~ .*(\.cgi|\.pl?)$ {  
  14. gzip off; #gzip makes scripts feel slower since they have to complete before getting gzipped  
  15. root /usr/local/awstats/wwwroot/cgi-bin;  
  16. fastcgi_pass 127.0.0.1:8000;  
  17. fastcgi_index awstats.pl;  
  18. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;  
  19. include fastcgi_params;  
  20. }  

 

好了,测试一下http://awstats.yoursite.com,和前文一样,输入密码后,这次看到的应该就awstats的界面了

8. 如果需要配置静态页面,则可以在logrotate中替换掉上面的awstats.pl, 换成

/usr/local/awstats/tools/awstats_buildstaticpages.pl -update -config=yoursite.com -lang=cn -dir=/usr/local/awstats/wwwroot -awstatsprog=/usr/local/awstats/wwwroot/cgi-bin/awstats.pl。

如果需要更频繁的更新访问情况,则可以把这句命令加入到crontab中,例如crontab -e -uwww-data。

然后nginx配置文件相应调整为显示静态网页就可以了。

【编辑推荐】

  1. nginx缓存的缺点和优点专家评价
  2. nginx缓存如何避免传统缓存的错误
  3. nginx php如何解决502 Bad Gateway错误
  4. nginx proxy反向代理的十五大技术特点
  5. nginx配置文件如何验证是否准确
责任编辑:张浩 来源: 互联网
相关推荐

2014-06-09 10:36:00

2010-03-25 18:31:03

Nginx配置文件

2010-02-22 15:59:48

2020-04-09 13:23:29

Nginx配置文件模板

2010-03-29 09:23:00

2010-03-25 17:46:27

nginx配置文件

2010-03-30 18:04:45

Nginx http服

2010-03-30 14:22:01

Nginx静态文件

2013-10-31 11:08:15

2010-02-06 14:00:05

Linux Nginx

2010-01-13 10:48:49

Centos设置

2010-03-29 10:26:06

Nginx配置文件

2022-04-28 09:46:20

Nginx文件Linux

2011-01-13 16:27:26

Linux配置文件

2011-01-19 14:00:21

2010-12-28 16:35:32

Outlook 配置文

2010-12-27 14:59:31

Outlook 配置文

2013-10-31 16:17:45

日志分析Awstats实战Nginx

2009-06-17 14:10:30

Spring配置文件

2009-09-22 10:23:15

Hibernate配置
点赞
收藏

51CTO技术栈公众号