2018-04-16 Linux学习


11.21 Apache访问日志

访问日志

访问日志记录用户的每一个请求
vim /usr/local/apache2.4/conf/httpd.conf   //搜索 LogFormat
    LogFormat "%h %l %u %t /"%r/" %>s %b /"%{Referer}i/" /"%{User-Agent}i/"" combined
    LogFormat "%h %l %u %t /"%r/" %>s %b" common

修改虚拟主机配置文件如下
<VirtualHost *:80>
    DocumentRoot "/data/wwwroot/111.com"
    ServerName 111.com
    ServerAlias www.exampl.com 2111.com.cn
    ErrorLog "logs/111.com-error_log"
    CustomLog "logs/111.com-access_log" common
</VirtualHost>

重新加载配置文件 -t , graceful

curl -x127.0.0.1:80 -I 111.com

tail /usr/local/apache2.4/logs/111.com-access_log

操作过程

最后一行 common 改为 combined
[root@aming-01 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/111.com"
ServerName 111.com
ServerAlias www.exampl.com 2111.com.cn
#<Directory /data/wwwroot/111.com>

AllowOverride AuthConfig

    #    AuthName "111.com user auth"
    #    AuthType Basic
    #    AuthUserFile /data/.htpasswd
    #    Require valid-user
    #</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>

[root@aming-01 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@aming-01 ~]# /usr/local/apache2.4/bin/apachectl graceful

[root@aming-01 ~]# tail -5 /usr/local/apache2.4/logs/111.com-access_log 
192.168.106.160 - - [23/Mar/2018:05:21:42 +0800] "GET http://111.com/dfafdjsaf HTTP/1.1" 404 207
192.168.106.1 - - [23/Mar/2018:05:42:37 +0800] "GET / HTTP/1.1" 200 8 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36"
192.168.106.1 - - [23/Mar/2018:05:42:38 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "http://111.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36"
192.168.106.160 - - [23/Mar/2018:05:43:35 +0800] "GET http://111.com/ HTTP/1.1" 200 8 "-" "curl/7.29.0"
192.168.106.160 - - [23/Mar/2018:05:43:42 +0800] "HEAD http://111.com/ HTTP/1.1" 200 - "-" "curl/7.29.0"

11.22 访问日志不记录静态文件 (减少没必须要的日志占用存储空间)

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

网站大多数元素为静态文件,如图片、css、js等,这些元素可以不用记录

把虚拟主机配置文件改成如下:
<VirtualHost *:80>
    DocumentRoot "/data/wwwroot/111.com"
    ServerName 111.com
    ServerAlias www.exampl.com 2111.com.cn
    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>

重新加载配置文件 -t , graceful

mkdir /data/wwwroot/www.123.com/images   //创建目录,并在这目录下上传一个图片

curl -x127.0.0.1:80 -I 111.com/images/123.jpg

tail /usr/local/apache2.4/logs/111.com-access_log

操作过程

添加 SetEnvIf 几行
[root@aming-01 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/111.com"
ServerName 111.com
ServerAlias www.exampl.com 2111.com.cn
#<Directory /data/wwwroot/111.com>

AllowOverride AuthConfig

#    AuthName "111.com user auth"
#    AuthType Basic
#    AuthUserFile /data/.htpasswd
#    Require valid-user
#</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>

[root@aming-01 ~]# cp baidu.png /data/wwwroot/111.com/images/

[root@aming-01 ~]# curl -x127.0.0.1:80 111.com
111.test[root@aming-01 ~]# curl -x127.0.0.1:80 111.com
111.test[root@aming-01 ~]# curl -x127.0.0.1:80 111.com
111.test[root@aming-01 ~]# curl -x127.0.0.1:80 111.com
111.test[root@aming-01 ~]# curl -x127.0.0.1:80 111.com
111.test
[root@aming-01 ~]# curl -x127.0.0.1:80 111.com/images/baidu.png
[root@aming-01 ~]# curl -x127.0.0.1:80 111.com/images/baidu.png
[root@aming-01 ~]# curl -x127.0.0.1:80 111.com/images/baidu.png

[root@aming-01 ~]# tail -5 /usr/local/apache2.4/logs/111.com-access_log 
127.0.0.1 - - [23/Mar/2018:06:26:25 +0800] "GET HTTP://111.com/ HTTP/1.1" 200 8 "-" "curl/7.29.0"
127.0.0.1 - - [23/Mar/2018:06:26:26 +0800] "GET HTTP://111.com/ HTTP/1.1" 200 8 "-" "curl/7.29.0"
127.0.0.1 - - [23/Mar/2018:06:26:32 +0800] "GET HTTP://111.com/ HTTP/1.1" 200 8 "-" "curl/7.29.0"
127.0.0.1 - - [23/Mar/2018:06:26:33 +0800] "GET HTTP://111.com/ HTTP/1.1" 200 8 "-" "curl/7.29.0"
127.0.0.1 - - [23/Mar/2018:06:26:33 +0800] "GET HTTP://111.com/ HTTP/1.1" 200 8 "-" "curl/7.29.0"

11.23 访问日志切割

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

CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/111.com-access_%Y%m%d.log 86400" combined env=!img

重新加载 -t , graceful

操作过程

修改 CustomLog
[root@aming-01 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/111.com"
ServerName 111.com
ServerAlias www.exampl.com 2111.com.cn
#<Directory /data/wwwroot/111.com>

AllowOverride AuthConfig

#   AuthName "111.com user auth"
#   AuthType Basic
#   AuthUserFile /data/.htpasswd
#   Require valid-user
#</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 "|/usr/local/apache2.4/bin/rotatelogs -l logs/111.com-access_%Y%m%d.log 86400" combined env=!img

</VirtualHost>

[root@aming-01 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@aming-01 ~]# /usr/local/apache2.4/bin/apachectl graceful

[root@aming-01 ~]# curl -x127.0.0.1:80 111.com/123.png
[root@aming-01 ~]# curl -x127.0.0.1:80 111.com/123.php
[root@aming-01 ~]# ls /usr/local/apache2.4/logs/
111.com-access_20180323.log

11.24 静态元素过期时间

浏览器访问网站的图片时会把静态的文件缓存在本地电脑里,这样下次再访问时就不用去远程下载了

增加配置,ExpiresActive on 打开功能开关

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

<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 hours"
    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>

需要打开 expires_module 模块
vim /usr/local/apache2.4/conf/httpd.conf
curl 测试,看cache-control:max-age

操作过程

添加 IfModule mod_expires.c 段
[root@aming-01 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/data/wwwroot/111.com"
ServerName 111.com
ServerAlias www.exampl.com 111.com.cn
<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 hours"
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-errorlog"
CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/111.com-access
%Y%m%d.log 86400" combined env=!img
</VirtualHost>

去掉模块前面的 # 号
[root@aming-01 ~]# vim /usr/local/apache2.4/conf/httpd.conf
LoadModule expires_module modules/mod_expires.so

[root@aming-01 ~]# /usr/local/apache2.4/bin/apachectl -t
Syntax OK
[root@aming-01 ~]# /usr/local/apache2.4/bin/apachectl graceful

[root@aming-01 ~]# curl -x127.0.0.1:80 111.com/images/baidu.png -I
HTTP/1.1 200 OK
Date: Fri, 23 Mar 2018 14:38:08 GMT
Server: Apache/2.4.29 (Unix) PHP/5.6.34
Last-Modified: Fri, 23 Mar 2018 06:14:50 GMT
ETag: "1ec5-5680e577a0a80"
Accept-Ranges: bytes
Content-Length: 7877
Cache-Control: max-age=86400
Expires: Sat, 24 Mar 2018 14:38:08 GMT
Content-Type: image/png