11.22 访问日志不记录静态文件 11.23 访问日志切割 11.24 静态元素过期时间
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了11.22 访问日志不记录静态文件 11.23 访问日志切割 11.24 静态元素过期时间相关的知识,希望对你有一定的参考价值。
- 11.22 访问日志不记录静态文件 - 11.23 访问日志切割 - 11.24 静态元素过期时间 - 扩展 1. apache日志记录代理IP以及真实客户端IP http://ask.apelearn.com/question/960 2. apache只记录指定URI的日志 http://ask.apelearn.com/question/981 3. apache日志记录客户端请求的域名 http://ask.apelearn.com/question/1037 4. apache 日志切割问题 http://ask.apelearn.com/question/566 # 11.22访问日志不记录静态文件 - 浏览器可以查看到页面元素,按F12查看页面元素内容 因为页面很多小图片之类的元素,没有必要去记录这些静态的元素,因为每个请求都去记录这些日志,会导致一天下来日志的量很大,一天下来几个G ,所以没有必要记录这些东西,有必要把这些排除掉, - 所有就有了需求,对于静态的元素的uri 不进行记录 - 修改虚拟主机配置文件,在配置文件错误日志下添加下一些变量 - 先打开虚拟主机配置文件 ``` [[email protected] ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf ServerName abc.com ServerAlias www.abc.com www.123.com ErrorLog "logs/abc.com-error_log" CustomLog "logs/abc.com-access_log" common </VirtualHost> <VirtualHost *:80> DocumentRoot "/data/wwwroot/111.com" ServerName 111.com ServerAlias www.example.com 2111.com.cn #<Directory /data/wwwroot/111.com> # <FilesMatch 123.php> # AllowOverride AuthConfig # AuthName "111.com user auth" # AuthType Basic # AuthUserFile /data/.htpasswd # require valid-user #</FilesMatch> #</Directory> <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} !^111.com$ RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L] </IfModule> ErrorLog "logs/111.com-error_log" CustomLog "logs/111.com-access_log" combined "/usr/local/apache2.4/conf/extra/httpd-vhosts.conf" 53L, 1707C 51,5 92% ``` - 插入一些变量 ``` <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} !^111.com$ RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L] </IfModule> ErrorLog "logs/111.com-error_log" SetEnvIf Request_URI ".*\.gif$" img SetEnvIf Request_URI ".*\.jpg$" img SetEnvIf Request_URI ".*\.png$" img SetEnvIf Request_URI ".*\.bmp$" img SetEnvIf Request_URI ".*\.swf$" img SetEnvIf Request_URI ".*\.js$" img SetEnvIf Request_URI ".*\.css$" img CustomLog "logs/111.com-access_log" combined env=!img </VirtualHost> "/usr/local/apache2.4/conf/extra/httpd-vhosts.conf" 60L, 1996C 58,5 底端 ``` - 定义了一个环境,如果这个请求 标记 正则带有gif jpg png bmp swf js css 这些为img - env=!img 表示只要是符合这些条件的 请求,都不会记录到这个日志里 env 非img 类型的都进行日志记录,保存配置文件 - 检测,重新加载配置,来访问下 ``` [[email protected] ~]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [[email protected] ~]# /usr/local/apache2.4/bin/apachectl graceful [[email protected] ~]# curl -x127.0.0.1:80 111.com/alskdjflks.jpg -I HTTP/1.1 404 Not Found Date: Tue, 10 Oct 2017 12:35:31 GMT Server: Apache/2.4.27 (Unix) PHP/7.1.6 Content-Type: text/html; charset=iso-8859-1 [[email protected] ~]# tail /usr/local/apache2.4/logs/111.com-access_log 127.0.0.1 - - [09/Oct/2017:00:20:06 +0800] "HEAD HTTP://111.com/ HTTP/1.1" 200 - 127.0.0.1 - aming [09/Oct/2017:00:21:36 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 200 - 127.0.0.1 - aming [09/Oct/2017:00:22:38 +0800] "GET HTTP://111.com/123.php HTTP/1.1" 200 7 127.0.0.1 - aming [09/Oct/2017:00:26:45 +0800] "GET HTTP://111.com/123.php HTTP/1.1" 200 7 192.168.202.131 - - [09/Oct/2017:20:29:12 +0800] "GET HTTP://111.com/ HTTP/1.1" 200 7 192.168.202.131 - - [09/Oct/2017:20:32:46 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 200 - 192.168.202.131 - - [09/Oct/2017:20:37:18 +0800] "HEAD http://111.com/123.php HTTP/1.1" 403 - 192.168.202.131 - - [09/Oct/2017:20:41:18 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - 192.168.202.131 - - [09/Oct/2017:22:01:53 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29.0" 192.168.202.131 - - [09/Oct/2017:22:02:25 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29.0" [[email protected] ~]# ``` - 查看日志,发现最新的几条日志里面都没有进行记录,发现只要包含jgp,png 的都不会记录 回到配置文件,改动配置,重新测试看看是否会记录 - 再来重新执行curl 改为png 试下 ``` [[email protected] ~]# curl -x127.0.0.1:80 111.com/alskdjflks -I HTTP/1.1 404 Not Found Date: Tue, 10 Oct 2017 12:38:51 GMT Server: Apache/2.4.27 (Unix) PHP/7.1.6 Content-Type: text/html; charset=iso-8859-1 [[email protected] ~]# tail /usr/local/apache2.4/logs/111.com-access_log 127.0.0.1 - aming [09/Oct/2017:00:21:36 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 200 - 127.0.0.1 - aming [09/Oct/2017:00:22:38 +0800] "GET HTTP://111.com/123.php HTTP/1.1" 200 7 127.0.0.1 - aming [09/Oct/2017:00:26:45 +0800] "GET HTTP://111.com/123.php HTTP/1.1" 200 7 192.168.202.131 - - [09/Oct/2017:20:29:12 +0800] "GET HTTP://111.com/ HTTP/1.1" 200 7 192.168.202.131 - - [09/Oct/2017:20:32:46 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 200 - 192.168.202.131 - - [09/Oct/2017:20:37:18 +0800] "HEAD http://111.com/123.php HTTP/1.1" 403 - 192.168.202.131 - - [09/Oct/2017:20:41:18 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - 192.168.202.131 - - [09/Oct/2017:22:01:53 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29.0" 192.168.202.131 - - [09/Oct/2017:22:02:25 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29.0" 127.0.0.1 - - [10/Oct/2017:20:38:51 +0800] "HEAD HTTP://111.com/alskdjflks HTTP/1.1" 404 - "-" "curl/7.29.0" [[email protected] ~]# curl -x127.0.0.1:80 111.com/alskdjflks.png -I HTTP/1.1 404 Not Found Date: Tue, 10 Oct 2017 12:41:15 GMT Server: Apache/2.4.27 (Unix) PHP/7.1.6 Content-Type: text/html; charset=iso-8859-1 [[email protected] ~]# tail /usr/local/apache2.4/logs/111.com-access_log 127.0.0.1 - aming [09/Oct/2017:00:21:36 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 200 - 127.0.0.1 - aming [09/Oct/2017:00:22:38 +0800] "GET HTTP://111.com/123.php HTTP/1.1" 200 7 127.0.0.1 - aming [09/Oct/2017:00:26:45 +0800] "GET HTTP://111.com/123.php HTTP/1.1" 200 7 192.168.202.131 - - [09/Oct/2017:20:29:12 +0800] "GET HTTP://111.com/ HTTP/1.1" 200 7 192.168.202.131 - - [09/Oct/2017:20:32:46 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 200 - 192.168.202.131 - - [09/Oct/2017:20:37:18 +0800] "HEAD http://111.com/123.php HTTP/1.1" 403 - 192.168.202.131 - - [09/Oct/2017:20:41:18 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - 192.168.202.131 - - [09/Oct/2017:22:01:53 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29.0" 192.168.202.131 - - [09/Oct/2017:22:02:25 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29.0" 127.0.0.1 - - [10/Oct/2017:20:38:51 +0800] "HEAD HTTP://111.com/alskdjflks HTTP/1.1" 404 - "-" "curl/7.29.0" [[email protected] ~]# curl -x127.0.0.1:80 111.com/alskdjflks -I HTTP/1.1 404 Not Found Date: Tue, 10 Oct 2017 12:41:26 GMT Server: Apache/2.4.27 (Unix) PHP/7.1.6 Content-Type: text/html; charset=iso-8859-1 [[email protected] ~]# tail /usr/local/apache2.4/logs/111.com-access_log 127.0.0.1 - aming [09/Oct/2017:00:22:38 +0800] "GET HTTP://111.com/123.php HTTP/1.1" 200 7 127.0.0.1 - aming [09/Oct/2017:00:26:45 +0800] "GET HTTP://111.com/123.php HTTP/1.1" 200 7 192.168.202.131 - - [09/Oct/2017:20:29:12 +0800] "GET HTTP://111.com/ HTTP/1.1" 200 7 192.168.202.131 - - [09/Oct/2017:20:32:46 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 200 - 192.168.202.131 - - [09/Oct/2017:20:37:18 +0800] "HEAD http://111.com/123.php HTTP/1.1" 403 - 192.168.202.131 - - [09/Oct/2017:20:41:18 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - 192.168.202.131 - - [09/Oct/2017:22:01:53 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29.0" 192.168.202.131 - - [09/Oct/2017:22:02:25 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29.0" 127.0.0.1 - - [10/Oct/2017:20:38:51 +0800] "HEAD HTTP://111.com/alskdjflks HTTP/1.1" 404 - "-" "curl/7.29.0" 127.0.0.1 - - [10/Oct/2017:20:41:26 +0800] "HEAD HTTP://111.com/alskdjflks HTTP/1.1" 404 - "-" "curl/7.29.0" [[email protected] ~]# ``` - 上传一张图片 ``` [[email protected] ~]# cd /data/wwwroot/111.com/ [[email protected] 111.com]# ls 123.php index.php [[email protected] 111.com]# rz [[email protected] 111.com]# ls 123.php baidu.png index.php ``` - 我们来请求下,这次不可能是404了,因为这图片本身就是存在的 ``` [[email protected] 111.com]# curl -x127.0.0.1:80 111.com/baidu.png -I HTTP/1.1 200 OK Date: Tue, 10 Oct 2017 12:46:53 GMT Server: Apache/2.4.27 (Unix) PHP/7.1.6 Last-Modified: Tue, 10 Oct 2017 12:44:26 GMT ETag: "e7a-55b30aad1fe80" Accept-Ranges: bytes Content-Length: 3706 Content-Type: image/png [[email protected] 111.com]# ``` - 再去windows上面 访问下111.com/baidu.png 再来查看下日志,发现根本就没有发现baidu.png 图片访问的日志 还是和上次一样的日志文件 ``` [[email protected] 111.com]# tail /usr/local/apache2.4/logs/111.com-access_log 127.0.0.1 - aming [09/Oct/2017:00:22:38 +0800] "GET HTTP://111.com/123.php HTTP/1.1" 200 7 127.0.0.1 - aming [09/Oct/2017:00:26:45 +0800] "GET HTTP://111.com/123.php HTTP/1.1" 200 7 192.168.202.131 - - [09/Oct/2017:20:29:12 +0800] "GET HTTP://111.com/ HTTP/1.1" 200 7 192.168.202.131 - - [09/Oct/2017:20:32:46 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 200 - 192.168.202.131 - - [09/Oct/2017:20:37:18 +0800] "HEAD http://111.com/123.php HTTP/1.1" 403 - 192.168.202.131 - - [09/Oct/2017:20:41:18 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - 192.168.202.131 - - [09/Oct/2017:22:01:53 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29.0" 192.168.202.131 - - [09/Oct/2017:22:02:25 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29.0" 127.0.0.1 - - [10/Oct/2017:20:38:51 +0800] "HEAD HTTP://111.com/alskdjflks HTTP/1.1" 404 - "-" "curl/7.29.0" 127.0.0.1 - - [10/Oct/2017:20:41:26 +0800] "HEAD HTTP://111.com/alskdjflks HTTP/1.1" 404 - "-" "curl/7.29.0" [[email protected] 111.com]# ``` - 如果把配置文件改下 把里面env=!img 删掉 再来试下 ``` </IfModule> ErrorLog "logs/111.com-error_log" SetEnvIf Request_URI ".*\.gif$" img SetEnvIf Request_URI ".*\.jpg$" img SetEnvIf Request_URI ".*\.png$" img SetEnvIf Request_URI ".*\.bmp$" img SetEnvIf Request_URI ".*\.swf$" img SetEnvIf Request_URI ".*\.js$" img SetEnvIf Request_URI ".*\.css$" img CustomLog "logs/111.com-access_log" combined </VirtualHost> :wq ``` - 检查 ,重新加载 访问下,再来看看日志文件 ``` [[email protected] 111.com]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [[email protected] 111.com]# /usr/local/apache2.4/bin/apachectl graceful [[email protected] 111.com]# curl -x127.0.0.1:80 111.com/baidu.png -I HTTP/1.1 200 OK Date: Tue, 10 Oct 2017 12:54:16 GMT Server: Apache/2.4.27 (Unix) PHP/7.1.6 Last-Modified: Tue, 10 Oct 2017 12:44:26 GMT ETag: "e7a-55b30aad1fe80" Accept-Ranges: bytes Content-Length: 3706 Content-Type: image/png [[email protected] 111.com]# tail /usr/local/apache2.4/logs/111.com-access_log 127.0.0.1 - aming [09/Oct/2017:00:26:45 +0800] "GET HTTP://111.com/123.php HTTP/1.1" 200 7 192.168.202.131 - - [09/Oct/2017:20:29:12 +0800] "GET HTTP://111.com/ HTTP/1.1" 200 7 192.168.202.131 - - [09/Oct/2017:20:32:46 +0800] "HEAD HTTP://111.com/123.php HTTP/1.1" 200 - 192.168.202.131 - - [09/Oct/2017:20:37:18 +0800] "HEAD http://111.com/123.php HTTP/1.1" 403 - 192.168.202.131 - - [09/Oct/2017:20:41:18 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - 192.168.202.131 - - [09/Oct/2017:22:01:53 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29.0" 192.168.202.131 - - [09/Oct/2017:22:02:25 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29.0" 127.0.0.1 - - [10/Oct/2017:20:38:51 +0800] "HEAD HTTP://111.com/alskdjflks HTTP/1.1" 404 - "-" "curl/7.29.0" 127.0.0.1 - - [10/Oct/2017:20:41:26 +0800] "HEAD HTTP://111.com/alskdjflks HTTP/1.1" 404 - "-" "curl/7.29.0" 127.0.0.1 - - [10/Oct/2017:20:54:16 +0800] "HEAD HTTP://111.com/baidu.png HTTP/1.1" 200 - "-" "curl/7.29.0" [[email protected] 111.com]# ``` - 再把配置文件改回来,检查配置文件,重新加载配置文件, - 再访问baidu.png 最新日志就不会生成出来 - [x] - 为什么要不去记录这些图片?这些日志, - 如果不去做限制,每次个请求都包含图片,每次请求都生成日志文件,这个日志文件会变的越来越大,浪费磁盘空间,消耗磁盘io, 这些日志文件根本就没有意义,平时看日志是看请求过哪些页面而不是看请求过哪些图片 ,所有有必要做一个限制 # 11.23 访问日志切割 - 如果一个日志不停的往文件里去写,总有一天会把磁盘写满,如果一天1g ,100天就100个g,对于访问日志来讲,不停地在后面追加,对于它来讲,不需要打开文件,也不会影响说明效率,对于我们来讲,磁盘空间满了是很危险的事情,也不应该让这样的事情发生,所以有必要每天把日志做一个切割,比如每天凌晨 把昨天的日志归档,今天开始又有一个新的日志,超过30天 或者60天的日志 拷贝走,这样磁盘空间永远也不会满, - 怎么实现日志切割? - 把虚拟主机配置文件改成如下: ``` <VirtualHost *:80> DocumentRoot "/data/wwwroot/www.123.com" ServerName www.123.com ServerAlias 123.com SetEnvIf Request_URI ".*\.gif$" img SetEnvIf Request_URI ".*\.jpg$" img SetEnvIf Request_URI ".*\.png$" img SetEnvIf Request_URI ".*\.bmp$" img SetEnvIf Request_URI ".*\.swf$" img SetEnvIf Request_URI ".*\.js$" img SetEnvIf Request_URI ".*\.css$" img CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/123.com-access_%Y%m%d.log 86400" combined env=!img </VirtualHost> ``` - 重新加载配置文件 -t, graceful ls /usr/local/apache2.4/logs - 首先打开虚拟主机配置文件 ``` <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} !^111.com$ RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L] </IfModule> ErrorLog "logs/111.com-error_log" SetEnvIf Request_URI ".*\.gif$" img SetEnvIf Request_URI ".*\.jpg$" img SetEnvIf Request_URI ".*\.png$" img SetEnvIf Request_URI ".*\.bmp$" img SetEnvIf Request_URI ".*\.swf$" img SetEnvIf Request_URI ".*\.js$" img SetEnvIf Request_URI ".*\.css$" img CustomLog "logs/111.com-access_log" combined env=!img </VirtualHost> "/usr/local/apache2.4/conf/extra/httpd-vhosts.conf" 60L, 1997C 58,5 ``` - 添加变量 - %Y%m%d.log,日志 以%Y年,%m月,%d日来进行命名 最后的86400单位是秒,也就是一天的时间,每一天0点后开始以年月日新命名创建一个日志 - CustomLog “|/usr/local/apache2.4/bin/rotatelogs -l logs/111.com-access_%Y%m%d.log 86400” combined env=!img 因为是新创建,需要新的访问以后 日志目录才会新创建一个以今天日期命名的文件 ``` <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} !^111.com$ RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L] </IfModule> ErrorLog "logs/111.com-error_log" SetEnvIf Request_URI ".*\.gif$" img SetEnvIf Request_URI ".*\.jpg$" img SetEnvIf Request_URI ".*\.png$" img SetEnvIf Request_URI ".*\.bmp$" img SetEnvIf Request_URI ".*\.swf$" img SetEnvIf Request_URI ".*\.js$" img SetEnvIf Request_URI ".*\.css$" img CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/111.com-access_%Y%m%d.log86400" combined env=!img </VirtualHost> :wq ``` - 重新检查、加载配置文件 , ``` [[email protected] 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf [1]+ 已停止 vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf [[email protected] 111.com]# date 2017年 10月 10日 星期二 21:14:40 CST [[email protected] 111.com]# fg vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf [[email protected] 111.com]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [[email protected] 111.com]# /usr/local/apache2.4/bin/apachectl graceful [[email protected] 111.com]# ``` - 在这个目录下并没有新的日志文件生成 ``` [[email protected] 111.com]# /usr/local/apache2.4/bin/apachectl graceful [[email protected] 111.com]# ls /usr/local/apache2.4/logs/ 111.com-access_log abc.com-access_log access_log httpd.pid 111.com-error_log abc.com-error_log error_log [[email protected] 111.com]# ``` - 用crul 访问下111.com/123.php 再看下最新的日志文件 ``` [[email protected] 111.com]# ls /usr/local/apache2.4/logs/ 111.com-access_log 111.com-error_log abc.com-access_log abc.com-error_log access_log error_log httpd.pid [[email protected] 111.com]# curl -x127.0.0.1:80 111.com/123.php 123.php[[email protected] 111.com]# ls /usr/local/apache2.4/logs/ 111.com-access_log 123.com-access_20171010.log abc.com-error_log error_log 111.com-error_log abc.com-access_log access_log httpd.pid [[email protected] 111.com]# [[email protected] 111.com]# ls /usr/local/apache2.4/logs/123.com-access_20171010.log /usr/local/apache2.4/logs/123.com-access_20171010.log [[email protected] 111.com]# cat !$ cat /usr/local/apache2.4/logs/123.com-access_20171010.log 127.0.0.1 - - [10/Oct/2017:21:44:03 +0800] "GET HTTP://111.com/123.php HTTP/1.1" 200 7 "-" "curl/7.29.0" [[email protected] 111.com]# ``` - 再来访问下图片文件,只不过png模式的不会记录在日志文件里,加个png1 这样就会记录日志了 ``` [[email protected] 111.com]# curl -x127.0.0.1:80 111.com/123.php.png1 -I HTTP/1.1 404 Not Found Date: Tue, 10 Oct 2017 13:48:37 GMT Server: Apache/2.4.27 (Unix) PHP/7.1.6 Content-Type: text/html; charset=iso-8859-1 [[email protected] 111.com]# cat /usr/local/apache2.4/logs/123.com-access_20171010.log 127.0.0.1 - - [10/Oct/2017:21:44:03 +0800] "GET HTTP://111.com/123.php HTTP/1.1" 200 7 "-" "curl/7.29.0" 127.0.0.1 - - [10/Oct/2017:21:48:37 +0800] "HEAD HTTP://111.com/123.php.png1 HTTP/1.1" 404 - "-" "curl/7.29.0" [[email protected] 111.com]# ``` - 只要到凌晨得时候就会生成新的日志,当然还要做一个任务计划,比如把超过1个月 2个月的日志删掉,如果不删 ,和有没有切割没有意义,如果不删除旧的文件,相当于写了多个文件,也会生成多个日志文件把磁盘写满, - 总结:做日志切割日志的目的是为了防止 把磁盘写满,再一个是更方便的管理日志,每天一个,每天一个,这样非常的有规律,查找的时候也很方便,比如像查看俩天前 访问日志的量,有了切割就会方便茶轴, - 写一个任务计划,指定超过多少大小的日志文件删掉。 # 11.24 配置静态元素过期时间 - 什么叫静态元素呢,比如访问的那些图片,css js jgp png - 当你用浏览器去访问一个网站的时候,这个网站里所有的静态文件,比如说某一个小图片,它的样式啊,它的一些js,样式其实就是css 涉及到一些前端的知识,说说图片 - 当浏览器访问网站的时候,网站的元素,这里的若干图片,浏览器会默认的把文件缓存到本地,把它叫做临时的一个文件,临时的一个缓存目录,缓存多久了,这个是浏览器里定义的,如果你不去定义,它也不会把缓存文件清空 - 它为什么把这些图片放在本地,缓存的目的就是为了第二次访问的时候,不用再去加载和下载这些图片提高网页的访问速度,节省带宽,以便提高用户体验 - 但是长时间的缓存,如果页面的某些小地方有了改动,因为有缓存在,也不会重新去下载。 可以使用浏览器自带的F12键去进行比对,第一次访问一个图片的时候是200的状态码,第二次访问的时候,就是304,证明图片没有改动,所以他不会重新进行下载缓存进行操作,添加静态元素过期时间的配置 - 首先打开虚拟主机配置文件 ``` <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} !^111.com$ RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L] </IfModule> ErrorLog "logs/111.com-error_log" SetEnvIf Request_URI ".*\.gif$" img SetEnvIf Request_URI ".*\.jpg$" img SetEnvIf Request_URI ".*\.png$" img SetEnvIf Request_URI ".*\.bmp$" img SetEnvIf Request_URI ".*\.swf$" img SetEnvIf Request_URI ".*\.js$" img SetEnvIf Request_URI ".*\.css$" img CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/123.com-access_%Y%m%d.log 86400" combined env=!img </VirtualHost> "/usr/local/apache2.4/conf/extra/httpd-vhosts.conf" 59L, 2051C 59,1 底端 ``` - 更改配置文件内容 ``` <IfModule mod_rewrite.c> RewriteEngine on RewriteCond %{HTTP_HOST} !^111.com$ RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L] </IfModule> <IfModule mod_expires.c> ExpiresActive on ExpiresByType image/gif "access plus 1 days" ExpiresByType image/jpeg "access plus 24 hours" ExpiresByType image/png "access plus 24 hours" ExpiresByType text/css "now plus 2 hour" ExpiresByType application/x-javascript "now plus 2 hours" ExpiresByType application/javascript "now plus 2 hours" ExpiresByType application/x-shockwave-flash "now plus 2 hours" ExpiresDefault "now plus 0 min" </IfModule> ErrorLog "logs/111.com-error_log" SetEnvIf Request_URI ".*\.gif$" img SetEnvIf Request_URI ".*\.jpg$" img :wq ``` - 先检查下有没有语法错误, - 还需要看下expaire 这么模块有没有打开 ``` [[email protected] 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf [[email protected] 111.com]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [[email protected] 111.com]# /usr/local/apache2.4/bin/apachectl -M |grep expire [[email protected] 111.com]# ``` - 明显没有,还需要编辑主配置文件 搜索expi ``` [[email protected] 111.com]# vim /usr/local/apache2.4/conf/httpd.conf #LoadModule ratelimit_module modules/mod_ratelimit.so LoadModule reqtimeout_module modules/mod_reqtimeout.so #LoadModule ext_filter_module modules/mod_ext_filter.so #LoadModule request_module modules/mod_request.so #LoadModule include_module modules/mod_include.so LoadModule filter_module modules/mod_filter.so #LoadModule substitute_module modules/mod_substitute.so #LoadModule sed_module modules/mod_sed.so LoadModule mime_module modules/mod_mime.so LoadModule log_config_module modules/mod_log_config.so #LoadModule log_debug_module modules/mod_log_debug.so #LoadModule logio_module modules/mod_logio.so LoadModule env_module modules/mod_env.so #LoadModule expires_module modules/mod_expires.so LoadModule headers_module modules/mod_headers.so #LoadModule unique_id_module modules/mod_unique_id.so LoadModule setenvif_module modules/mod_setenvif.so LoadModule version_module modules/mod_version.so #LoadModule remoteip_module modules/mod_remoteip.so #LoadModule proxy_module modules/mod_proxy.so #LoadModule proxy_connect_module modules/mod_proxy_connect.so #LoadModule proxy_ftp_module modules/mod_proxy_ftp.so #LoadModule proxy_http_module modules/mod_proxy_http.so #LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so #LoadModule proxy_scgi_module modules/mod_proxy_scgi.so #LoadModule proxy_fdpass_module modules/mod_proxy_fdpass.so #LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so 已查找到文件结尾,再从开头继续查找 ``` - 找到#LoadModule expires_module modules/mod_expires.so 把井号去掉 打开这个模块 ``` LoadModule expires_module modules/mod_expires.so LoadModule headers_module modules/mod_headers.so #LoadModule unique_id_module modules/mod_unique_id.so LoadModule setenvif_module modules/mod_setenvif.so LoadModule version_module modules/mod_version.so #LoadModule remoteip_module modules/mod_remoteip.so #LoadModule proxy_module modules/mod_proxy.so #LoadModule proxy_connect_module modules/mod_proxy_connect.so #LoadModule proxy_ftp_module modules/mod_proxy_ftp.so #LoadModule proxy_http_module modules/mod_proxy_http.so #LoadModule proxy_fcgi_module modules/mod_proxy_fcgi.so #LoadModule proxy_scgi_module modules/mod_proxy_scgi.so #LoadModule proxy_fdpass_module modules/mod_proxy_fdpass.so #LoadModule proxy_wstunnel_module modules/mod_proxy_wstunnel.so :wq ``` - 再检查语法,重新加载 再来看下 ``` [[email protected] 111.com]# vim /usr/local/apache2.4/conf/httpd.conf [[email protected] 111.com]# /usr/local/apache2.4/bin/apachectl -t Syntax OK [[email protected] 111.com]# /usr/local/apache2.4/bin/apachectl graceful [[email protected] 111.com]# /usr/local/apache2.4/bin/apachectl -M |grep expire expires_module (shared) [[email protected] 111.com]# ``` - 模块有了 ,现在来做实验 ``` [[email protected] 111.com]# curl -x127.0.0.1:80 111.com/baidu.png -I HTTP/1.1 200 OK Date: Tue, 10 Oct 2017 14:25:22 GMT 这个是当前时间 Server: Apache/2.4.27 (Unix) PHP/7.1.6 Last-Modified: Tue, 10 Oct 2017 12:44:26 GMT ETag: "e7a-55b30aad1fe80" Accept-Ranges: bytes Content-Length: 3706 Cache-Control: max-age=86400 Expires: Wed, 11 Oct 2017 14:25:22 GMT 这个是过期时间 Content-Type: image/png [[email protected] 111.com]# ``` - Date: Tue, 10 Oct 2017 14:25:22 GMT 这个是当前时间 ,这个Expires: Wed, 11 Oct 2017 14:25:22 GMT 这个是过期时间 - 结果表明,png图片的请求多了一个缓存时间 时间为86400秒和当前时间正好差了一个24小时。 - 一般只要是公司的网站,都会设置这个过期时间 ### 扩展 1. apache日志记录代理IP以及真实客户端IP http://ask.apelearn.com/question/960 默认情况下log日志格式为: ``` LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined 其中%h 是记录访问者的IP,如果在web的前端有一层代理,那么这个%h其实就是代理机器的IP,这不是我们想要的。在这种情况下, %{X-FORWARDED-FOR}i 字段会记录客户端真实的IP。所以log日志改为: LogFormat "%h %{X-FORWARDED-FOR}i %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined ``` 2. apache只记录指定URI的日志 http://ask.apelearn.com/question/981 我的需求是,把类似请求 www.aaa.com/aaa/... 这样的请求才记录日志。 在httpd.conf 或者 相关的虚拟主机配置文件中添加 ``` SetEnvIf Request_URI "^/aaa/.*" aaa-request CustomLog "|/usr/local/apache/bin/rotatelogs -l /usr/local/apache/logs/aaa-access_%Y%m%d.log 86400" combined env=aaa-request ``` 这样就可以了。这个原理和不记录图片等静态访问的日志(http://www.lishiming.net/thread-561-1-1.html)是一样的。 3. apache日志记录客户端请求的域名 http://ask.apelearn.com/question/1037 正常情况下,根本就没有必要记录这一项,毕竟咱们大都根据虚拟主机来设置相应的访问日志,但也有个别的情况,比如 ServerName *.abc.com 这样泛解析的形式,所以有必要记录一下用户请求的域名到底是哪个。 而apache的LogFormat 中正好有一项值满足了这个需求。即 %V 这里是大写的V ,小写的v 记录的是咱们在虚拟主机中设置的ServerName ,这个的确是没有必要记录的。 4. apache 日志切割问题 http://ask.apelearn.com/question/566 apache的日志是可以自动切割的。 方法一: 使用 cronolog 为每一天建立一个新的日志 ``` CustomLog "|bin/cronolog logs/access_%Y%m%d.log" combined 也可以按小时 CustomLog "|bin/cronolog logs/access_%Y%m%d%h.log" combined 方法二:使用 rotatelogs 每一天记录一个日志 CustomLog "|bin/rotatelogs -l logs/access_%Y%m%d.log 86400" combined 每小时 CustomLog "|bin/rotatelogs -l logs/access_%Y%m%d%H.log 3600" combined 再看apache rotatelogs语法 rotatelogs [ -l ] logfile [ rotationtime [ offset ]] | [ filesizeM ] ``` 选项 -l 使用本地时间代替GMT时间作为时间基准。注意:在一个改变GMT偏移量(比如夏令时)的环境中使用-l会导致不可预料的结果。所以一定要加上-l 否则出现的日志时间和实际时间是相差8小时的。 logfile 它加上基准名就是日志文件名。如果logfile中包含”%”,则它会被视为用于strftime()的格式字符串;否则它会被自动加上以秒为单位的”.nnnnnnnnnn”后缀。这两种格式都表示新的日志开始使用的时间。 rotationtime 日志文件滚动的以秒为单位的间隔时间。 offset 相对于UTC的时差的分钟数。如果省略,则假定为”0″并使用UTC时间。比如,要指定UTC时差为”-5小时”的地区的当地时间,则此参数应为”-300″。 filesizeM 指定以filesizeM文件大小滚动,而不是按照时间或时差滚动。
以上是关于11.22 访问日志不记录静态文件 11.23 访问日志切割 11.24 静态元素过期时间的主要内容,如果未能解决你的问题,请参考以下文章
11.22 访问日志不记录静态文件11.23 访问日志切割11.24 静态元素过期时间
11.22 访问日志不记录静态文件 11.23 访问日志切割 11.24 静态元素过期时间
11.22 访问日志不记录静态文件11.23 访问日志切割11.24 静态元素过期时间
11.22 访问日志不记录静态文件 11.23 访问日志切割 11.24 静态元素过期时间