专栏首页运维小白11.22 访问日志不记录静态文件

11.22 访问日志不记录静态文件

访问日志不记录指定类型的文件目录概要

  • 网站大多元素为静态文件,如图片、css、js等,这些元素可以不用记录
  • 把虚拟主机配置文件改成如下:
 <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 "logs/123.com-access_log" combined env=!img
</VirtualHost>
  • 重新加载配置文件 -t, graceful
  • mkdir /data/wwwroot/www.123.com/images //创建目录,并在这目录下上传一个图片
  • curl -x127.0.0.1:80 -I 123.com/images/123.jpg
  • tail /usr/local/apache2.4/logs/123.com-access_log

访问日志不记录指定类型的文件

  1. 浏览器可以查看到页面元素,按 F12 查看页面元素内容,它默认定位在Network。如果不是在Network,就去选择下Network,然后刷新下网页,会看到如图
  1. 会看到右侧出现很多的元素,每一行都表示一个元素,这些请求,这些访问都会记录到访问日志里面去的,但实际上没有必须记录一些图片、css、js等元素,而这些请求都会记录到日志文件里,就会导致每天的日志访问量很大,对之后的运维很不方便,所以就有了需求,对于静态的元素的uri 不进行记录
  2. 排除图片、css、js等,拷贝配置文件到服务器里面
  • 需要拷贝的配置文件
 <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 "logs/123.com-access_log" combined env=!img
</VirtualHost>
  1. 打开虚拟主机配置文件
  • vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
  • 未更改前
<VirtualHost *:80>
    DocumentRoot "/data/wwwroot/abc.com"
    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
</VirtualHost>
  • 更改后的配置文件,在 ErrorLog 下一行插入
[root@hf-01 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

<VirtualHost *:80>
    DocumentRoot "/data/wwwroot/abc.com"
    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"
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>

并保存退出
  1. 相当于定义了一个环境
  • img 是一个标签、一个变量,可以自定义,可以是任意字符
  • 定义”..gif$”、”..jpg$”、”..png$”、”..bmp$”、”..swf$”、”..js$”、”.*.css$”;这些都是 img
  • env=!img 表示 env非 img 类型的都进行日志记录
  1. 在设置好配置文件后,先不加载配置文件,先做一个测验
[root@hf-01 ~]# curl -x127.0.0.1:80 111.com/asdasdsaf.jpg -I
HTTP/1.1 404 Not Found
Date: Thu, 21 Dec 2017 15:35:14 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
Content-Type: text/html; charset=iso-8859-1

[root@hf-01 ~]# 
  1. 在访问这样的请求,不管是404,还是200都会记录日志
[root@hf-01 ~]# !tail
tail -5 /usr/local/apache2.4/logs/111.com-access_log
192.168.202.130 - - [21/Dec/2017:21:51:25 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29.0"
192.168.202.1 - - [21/Dec/2017:21:51:32 +0800] "GET /123.php HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0"
192.168.202.130 - - [21/Dec/2017:21:55:08 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29.0"
192.168.202.1 - - [21/Dec/2017:22:04:27 +0800] "GET /123.php HTTP/1.1" 200 7 "http://ask.apelearn.com/question/17687" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0"
127.0.0.1 - - [21/Dec/2017:23:35:14 +0800] "HEAD HTTP://111.com/asdasdsaf.jpg HTTP/1.1" 404 - "-" "curl/7.29.0"
[root@hf-01 ~]# 
  1. 然后在检查配置文件语法,并重新加载配置文件
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl graceful
[root@hf-01 ~]#
  1. 这时再来curl访问,会发现日志中没有记录
[root@hf-01 ~]# curl -x127.0.0.1:80 111.com/asdasdsaf.jpg -I
HTTP/1.1 404 Not Found
Date: Thu, 21 Dec 2017 15:39:35 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
Content-Type: text/html; charset=iso-8859-1

[root@hf-01 ~]# tail -5 /usr/local/apache2.4/logs/111.com-access_log
192.168.202.130 - - [21/Dec/2017:21:51:25 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29.0"
192.168.202.1 - - [21/Dec/2017:21:51:32 +0800] "GET /123.php HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0"
192.168.202.130 - - [21/Dec/2017:21:55:08 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29.0"
192.168.202.1 - - [21/Dec/2017:22:04:27 +0800] "GET /123.php HTTP/1.1" 200 7 "http://ask.apelearn.com/question/17687" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0"
127.0.0.1 - - [21/Dec/2017:23:35:14 +0800] "HEAD HTTP://111.com/asdasdsaf.jpg HTTP/1.1" 404 - "-" "curl/7.29.0"
[root@hf-01 ~]# 
  1. 只要是gif、jpg、png、bmp、swf、js的都不会在日志中有记录

本地上传图片并访问

  1. 进入到在/data/wwwroot/111.com目录下
[root@hf-01 ~]# cd /data/wwwroot/111.com
[root@hf-01 111.com]# ls
123.php  index.php
  1. 并上传一张图片到该目录下
[root@hf-01 111.com]# rz

[root@hf-01 111.com]# ls
123.php  index.php  QQ111.jpg
[root@hf-01 111.com]# 
  1. 用curl命令查看图片,会发现图片是能访问的
[root@hf-01 111.com]# curl -x127.0.0.1:80 111.com/QQ111.jpg -I
HTTP/1.1 200 OK
Date: Thu, 21 Dec 2017 15:50:24 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
Last-Modified: Mon, 04 Dec 2017 03:56:48 GMT
ETag: "da7f-55f7bb520e000"
Accept-Ranges: bytes
Content-Length: 55935
Content-Type: image/jpeg

[root@hf-01 111.com]# 
  1. 在浏览器访问网页图片,会发现图片是能访问的
  1. 查看日志文件,会发现日志文件中没有记录图片的日志,这个就是因为定义了日志标签
[root@hf-01 111.com]# tail -5 /usr/local/apache2.4/logs/111.com-access_log
192.168.202.130 - - [21/Dec/2017:21:51:25 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29.0"
192.168.202.1 - - [21/Dec/2017:21:51:32 +0800] "GET /123.php HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0"
192.168.202.130 - - [21/Dec/2017:21:55:08 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29.0"
192.168.202.1 - - [21/Dec/2017:22:04:27 +0800] "GET /123.php HTTP/1.1" 200 7 "http://ask.apelearn.com/question/17687" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0"
127.0.0.1 - - [21/Dec/2017:23:35:14 +0800] "HEAD HTTP://111.com/asdasdsaf.jpg HTTP/1.1" 404 - "-" "curl/7.29.0"
[root@hf-01 111.com]# 
  1. 这时去掉虚拟主机配置文件中的env=!img ,在用浏览器浏览图片,在curl命令访问图片
[root@hf-01 111.com]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf

更改后
<VirtualHost *:80>
    DocumentRoot "/data/wwwroot/abc.com"
    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"
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>
  1. 这时再来刷新浏览器访问图片,用curl访问图片
[root@hf-01 111.com]# curl -x127.0.0.1:80 111.com/QQ111.jpg -I
HTTP/1.1 200 OK
Date: Thu, 21 Dec 2017 16:02:57 GMT
Server: Apache/2.4.29 (Unix) PHP/7.1.6
Last-Modified: Mon, 04 Dec 2017 03:56:48 GMT
ETag: "da7f-55f7bb520e000"
Accept-Ranges: bytes
Content-Length: 55935
Content-Type: image/jpeg

[root@hf-01 111.com]# 
  1. 查看日志文件,会发现日志文件有记录存在
[root@hf-01 111.com]# tail -5 /usr/local/apache2.4/logs/111.com-access_log
192.168.202.130 - - [21/Dec/2017:21:51:25 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29.0"
192.168.202.1 - - [21/Dec/2017:21:51:32 +0800] "GET /123.php HTTP/1.1" 200 7 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0"
192.168.202.130 - - [21/Dec/2017:21:55:08 +0800] "HEAD http://111.com/123.php HTTP/1.1" 200 - "-" "curl/7.29.0"
192.168.202.1 - - [21/Dec/2017:22:04:27 +0800] "GET /123.php HTTP/1.1" 200 7 "http://ask.apelearn.com/question/17687" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0"
127.0.0.1 - - [21/Dec/2017:23:35:14 +0800] "HEAD HTTP://111.com/asdasdsaf.jpg HTTP/1.1" 404 - "-" "curl/7.29.0"
[root@hf-01 111.com]# 
  1. 这时在到虚拟主机配置文件中加上env=!img ,并重新加载配置文件,在刷新图片,会看到日志文件中并没有记录日志

日志文件不记录总结

  • 若是不去做一个限制,那么每一次请求都会记录到日志文件,日志文件就会变得越来越大,这样不仅会消耗磁盘空间,还会耗费磁盘i/o,而且这些文件毫无意义,所以需要做一个限制

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • Linux基础(day43)

    11.18 Apache用户认证 httpd的用户认证目录 vim /usr/local/apache2.4/conf/extra/httpd-vhosts.c...

    运维小白
  • 11.18 Apache用户认证

    httpd的用户认证目录 vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //把123.com那个虚...

    运维小白
  • 11.16/11.17 Apache默认虚拟主机

    httpd的默认虚拟主机目录概要 一台服务器可以访问多个网站,每个网站都是一个虚拟主机 概念:域名(主机名)、DNS、解析域名、hosts 任何一个域名解析到这...

    运维小白
  • 【一文看尽200篇干货】2018最新机器学习、NLP、Python教程汇总!

    【新智元导读】本文收集并详细筛选出了一系列机器学习、自然语言处理、Python及数学基础知识的相关资源和教程,数目多达200种!来源既包括斯坦福、MIT等名校,...

    新智元
  • 解决云服务中的多对多分组问题 - 二分图的社区发现算法

    本文介绍一种高效的二分图社区发现算法biLouvain,以云服务中的多对多关系的分组问题为例,分析这类算法的使用方法和效果。

    王录华
  • 提高GitHub访问速度及其他DNS优化

    試毅-思伟
  • Linux基础(day43)

    11.18 Apache用户认证 httpd的用户认证目录 vim /usr/local/apache2.4/conf/extra/httpd-vhosts.c...

    运维小白
  • 11.18 Apache用户认证

    httpd的用户认证目录 vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf //把123.com那个虚...

    运维小白
  • 11.16/11.17 Apache默认虚拟主机

    httpd的默认虚拟主机目录概要 一台服务器可以访问多个网站,每个网站都是一个虚拟主机 概念:域名(主机名)、DNS、解析域名、hosts 任何一个域名解析到这...

    运维小白
  • Padavan 配置v2ray

    参考:https://ntgeralt.blogspot.com/2019/06/padavan-v2ray.html

    超级大猪

扫码关注云+社区

领取腾讯云代金券