Apache访问日志切割和自定义格式

十周四次课(12月21日) 11.22 访问日志不记录静态文件 11.23 访问日志切割 11.24 静态元素过期时间

扩展 apache日志记录代理IP以及真实客户端IP
http://ask.apelearn.com/question/960

apache只记录指定URI的日志
http://ask.apelearn.com/question/981

apache日志记录客户端请求的域名
http://ask.apelearn.com/question/1037

apache 日志切割问题
http://ask.apelearn.com/question/566

修改apache日志不记录图片,js和css访问日志

vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
在CustomLog 上方添加img标记,在添加env=!img 
80>
    DocumentRoot "/data/wwwroot/123.com"
    ServerName 123.com
    ServerAlias www.123.com 1123.com.cn
     .c>
        RewriteEngine on
        RewriteCond %{HTTP_HOST} !^123.com$
        RewriteRule ^/(.*)$ http://www.123.com/$1 [R=301,L]
     
    ErrorLog "logs/123.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/123.com-access_log" combined env=!img

查看日志记录情况
tail /usr/local/apache2.4/logs/abc.com-access_log

apache日志切割

vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

    DocumentRoot "/data/wwwroot/123.com"
    ServerName 123.com
    ServerAlias www.123.com 1123.com.cn
     
        RewriteEngine on
        RewriteCond %{HTTP_HOST} !^123.com$
        RewriteRule ^/(.*)$ http://www.123.com/$1 [R=301,L]
     
    ErrorLog "logs/123.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

-l是已当前系统时间切割,不加-l就是ust美国时间,在中国是cst
86400秒就是一天

浏览器访问网站的图片时会把静态的文件缓存在本地电脑里,这样下次再访问时就不用去远程下载了
配置静态文件过期时间,需要启动expires_module模块
/usr/local/apache2.4/bin/apachectl -M| grep expires_module
vim /usr/local/apache2.4/conf/httpd.conf
LoadModule expires_module modules/mod_expires.so

/usr/local/apache2.4/bin/apachectl -t
/usr/local/apache2.4/bin/apachectl graceful

[root@wwlinux701 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
#
80>
    DocumentRoot "/data/wwwroot/abc.com"
    ServerName abc.com
    ServerAlias www.abc.com www.111.com
    ErrorLog "logs/abc.com-error_log"
    CustomLog "logs/abc.com-access_log" common


80>
    DocumentRoot "/data/wwwroot/123.com"
    ServerName 123.com
    ServerAlias www.123.com 1123.com.cn
# 
 #   
 #      AllowOverride AuthConfig
 #       AuthName "123.com user auth"
 #       AuthType Basic 
 #       AuthUserFile /data/.htpasswd  
 #       require valid-user
 #   
#    
    .c> 
        RewriteEngine on
        RewriteCond %{HTTP_HOST} !^123.com$
        RewriteRule ^/(.*)$ http://www.123.com/$1 [R=301,L]
     
    .c>
     ExpiresActive on  #开关设置打开
     ExpiresByType image/gif  "access plus 1 days"   #1天,单位可以按天或小时
     ExpiresByType image/jpeg "access plus 24 hours"  #24小时
     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"
    
    ErrorLog "logs/123.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

curl -x127.0.0.1:80 123.com/15.png -I
Apache访问日志切割和自定义格式_第1张图片

你可能感兴趣的:(linux学习之路)