apache学习笔记(日志切割|静态缓存|防盗链)

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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值