apache日志切割
apache日志格式
配置文件中的设置
vim /usr/local/apache2/conf/httpd.conf
<IfModule log_config_module>
#
# The following directives define some format nicknames for use with
# a CustomLog directive (see below).
#
#-----------------------------------------------------
LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined
LogFormat "%h %l %u %t "%r" %>s %b" common
#-----------------------------------------------------
<IfModule logio_module>
# You need to enable mod_logio.c to use %I and %O
LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i" %I %O" combinedio
</IfModule>
#
# The location and format of the access logfile (Common Logfile Format).
# If you do not define any access logfiles within a <VirtualHost>
# container, they will be logged here. Contrariwise, if you *do*
# define per-<VirtualHost> access logfiles, transactions will be
# logged therein and *not* in this file.
#
CustomLog "logs/access_log" common
#
# If you prefer a logfile with access, agent, and referer information
# (Combined Logfile Format) you can use the following directive.
#
#CustomLog "logs/access_log" combined
</IfModule>
虚拟主机 配置 虚拟主机的日志名以及路径 包括切割
vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
...
<VirtualHost *:80>
DocumentRoot "/data/www"
ServerName www.aaa.com
ServerAlias www.bbb.com
ErrorLog "logs/aaa.com-error_log"
CustomLog "|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/aaa.com-access_%Y%m%d_log 86400" combined
</VirtualHost>
...
配置完之后 重启服务
[root@tyrr logs]# apachectl -t
Syntax OK
[root@tyrr logs]# apachectl restart
[root@tyrr logs]# ls
aaa.com-access_20170305_log aaa.com-access_log aaa.com-error_log access_log error_log httpd.pid
Apache 不记录指定文件类型的日志
修改虚拟主机配置文件
[root@tyrr logs]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
...
<VirtualHost *:80>
DocumentRoot "/data/www"
ServerName www.aaa.com
ServerAlias www.bbb.com
ErrorLog "logs/aaa.com-error_log"
SetEnvIf Request_URI ".*.gif$" image-request
SetEnvIf Request_URI ".*.jpg$" image-request
SetEnvIf Request_URI ".*.png$" image-request
SetEnvIf Request_URI ".*.bmp$" image-request
SetEnvIf Request_URI ".*.bmp$" image-request
SetEnvIf Request_URI ".*.swf$" image-request
SetEnvIf Request_URI ".*.js$" image-request
SetEnvIf Request_URI ".*.css$" image-request
CustomLog "|/usr/local/apache2/bin/rotatelogs -l /usr/local/apache2/logs/aaa.com-access_%Y%m%d_log 86400" combined env=!image-request
</VirtualHost>
保存退出 检查语法错误 重启 测试查看
[root@tyrr logs]# apachectl -t
Syntax OK
[root@tyrr logs]# apachectl restart
Apache 配置静态缓存
修改虚拟主机配置文件
[root@tyrr logs]# vim /usr/local/apache2/conf/extra/httpd-vhosts.conf
...
<Ifmodule mod_expires.c>
ExpiresActive on
ExpiresByType image/gif "access plus 1 hours"
ExpiresByType image/jpeg "access plus 1 hours"
ExpiresByType image/png "access plus 1 hours"
ExpiresByType image/css "access plus 1 hours"
ExpiresByType application/x-javascript "access plus 1 hours"
ExpiresByType application/x-shockwave-flash "access plus 1 hours"
ExpiresDefault "now plus 0 min"
</IfModule>
保存退出 检查语法错误 重启 测试查看
[root@tyrr logs]# apachectl -t
Syntax OK
[root@tyrr logs]# apachectl restart
[root@tyrr logs]# curl -x127.0.0.1:80 'http://www.bbb.com/static/image/common/logo.png' -I
HTTP/1.1 200 OK
Date: Sat, 04 Mar 2017 23:05:41 GMT
Server: Apache/2.2.32 (Unix) PHP/5.4.36
Last-Modified: Tue, 31 May 2016 03:08:36 GMT
ETag: "442b8-1149-5341ab0597500"
Accept-Ranges: bytes
Content-Length: 4425
Cache-Control: max-age=3600
Expires: Sun, 05 Mar 2017 00:05:41 GMT
Content-Type: image/png
可以看到 Cache-Control: max-age=3600 即缓存时间
apache配置静态防盗链
...
SetEnvIfNoCase Referer "^http://.*.aaa.com" local_ref
<filesmatch ".(txt|doc|jpg|js|css|gif|png)">
Order Allow,Deny
Allow from env=local_ref
</filesmatch>
SetEnvIfNoCase Referer "^http://.*.aaa.com" local_ref
将自己的网站添加进白名单白名单
保存退出 检查语法错误 重启
[root@tyrr logs]# apachectl -t
Syntax OK
[root@tyrr logs]# apachectl restart