11.22 访问日志不记录静态文件
网站大多元素为静态文件, 如图片, CSS,js 等, 这些元素可以不用记录
如果不想在日志中记录这些文件访问日志
- [root@localhost ~]# vim /etc/httpd24/extra/httpd-vhosts.conf
- <VirtualHost *:80>
- ServerAdmin webmaster@111.com
- DocumentRoot "/home/wwwroot/111.com"
- ServerName 111.com
- ServerAlias www.123.cn
- <IfModule mod_rewrite.c>
- RewriteEngine on
- RewriteCond %{HTTP_HOST} !^111.com$
- RewriteRule ^/(.*)$ http://111.com/$1 [R=301.L]
- </IfModule>
- # <Directory /home/wwwroot/111.com> #目录认证
- # <FilesMatch yhtz.php> #匹配文件认证
- # AllowOverride AuthConfig
- # AuthName "账号密码访问"
- # AuthType Basic
- # AuthUserFile /data/.htpasswd
- # require valid-user
- # </FilesMatch>
- # </Directory>
- # 这些定义为 img
- 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
- ErrorLog "logs/111.com-error_log"
- CustomLog "logs/111.com-access_log" combined env=!img #!img 为记录非 img
- </VirtualHost>
检查, 重载配置:
- # apachectl -t
- # apachectl graceful
- mkdir /data/wwwroot/111.com/img // 创建目录, 并在这目录下上传图片
- [root@localhost ~]# curl -xlocalhost:80 111.com/img/snipaste20171121_225348.png -I
- HTTP/1.1 200 OK
Date: Thu, 08 Mar 2018 17:27:03 GMT
- Server: Apache/2.4.29 (Unix) PHP/7.2.1
- Last-Modified: Tue, 21 Nov 2017 14:53:51 GMT
- ETag: "1f5ef-55e7f5efc3dc0"
- Accept-Ranges: bytes
- Content-Length: 128495
- Content-Type: image/png
- [root@localhost ~]# tail -f /usr/local/apache2.4/logs/111.com-access_log
- ::1 - - [09/Mar/2018:01:03:29 +0800] "GET HTTP://www.123.cn/ HTTP/1.1" 301 223 "-" "curl/7.29.0"
- ::1 - - [09/Mar/2018:01:03:33 +0800] "GET HTTP://111.com/ HTTP/1.1" 200 8 "-" "curl/7.29.0"
- 192.168.0.190 - aiker [09/Mar/2018:01:03:42 +0800] "GET / HTTP/1.1" 200 8 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
- 192.168.0.190 - aiker [09/Mar/2018:01:08:25 +0800] "GET / HTTP/1.1" 200 8 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
- 192.168.0.190 - aiker [09/Mar/2018:01:16:12 +0800] "GET / HTTP/1.1" 200 8 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
- 192.168.0.190 - aiker [09/Mar/2018:01:16:16 +0800] "GET / HTTP/1.1" 200 8 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
- 192.168.0.190 - aiker [09/Mar/2018:01:16:18 +0800] "GET / HTTP/1.1" 200 8 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
- 192.168.0.190 - aiker [09/Mar/2018:01:16:26 +0800] "GET / HTTP/1.1" 200 8 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
- 192.168.0.190 - - [09/Mar/2018:01:17:09 +0800] "GET /near-gradient(45deg, HTTP/1.1" 404 219 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
- 192.168.0.190 - - [09/Mar/2018:01:19:21 +0800] "GET /img/ HTTP/1.1" 403 213 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36"
- ::1 - - [09/Mar/2018:01:32:16 +0800] "HEAD HTTP://111.com/img/ HTTP/1.1" 403 - "-" "curl/7.29.0"
- ^E^C
11.23 访问日志切割
日志一直记录总有一天会把整个磁盘占满, 所以有必要让它自动切割, 并删除老的日志文件 ,apache 自带日志切割工具 rotatelogs, 在 t 对应的 vhos 配置就可以了
把虚拟主机配置文件改成如下:
- [root@localhost ~]# vim /etc/httpd24/extra/httpd-vhosts.conf
- <VirtualHost *:80>
- ServerAdmin webmaster@111.com
- DocumentRoot "/home/wwwroot/111.com"
- ServerName 111.com
- ServerAlias www.123.cn
- <IfModule mod_rewrite.c>
- RewriteEngine on
- RewriteCond %{HTTP_HOST} !^111.com$
- RewriteRule ^/(.*)$ http://111.com/$1 [R=301.L]
- </IfModule>
- # <Directory /home/wwwroot/111.com> #目录认证
- # <FilesMatch yhtz.php> #匹配文件认证
- # AllowOverride AuthConfig
- # AuthName "账号密码访问"
- # AuthType Basic
- # AuthUserFile /data/.htpasswd
- # require valid-user
- # </FilesMatch>
- # </Directory>
- 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
- ErrorLog "logs/111.com-error_log"
- CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/111.com-access_%Y%m%d.log 86400" combined env=!img #配置日志切割
- </VirtualHost>
检查并重载配置
- apachectl -t
- apachectl graceful
- curl -xlocalhost:80 111.com/img/ -I
- ls /usr/local/apache2.4/logs // 查看日志目录
11.24 静态元素过期时间
浏览器访问网站的图片时会把静态的文件缓存在本地电脑里, 这样下次再访问时就不用去远程下载了
需要打开 expires_module, 编辑 httpd.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 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"
- </IfModule>
修改后的配置:
- [root@localhost ~]# vim /etc/httpd24/extra/httpd-vhosts.conf
- <VirtualHost *:80>
- ServerAdmin webmaster@111.com
- DocumentRoot "/home/wwwroot/111.com"
- ServerName 111.com
- ServerAlias www.123.cn
- <IfModule mod_rewrite.c>
- RewriteEngine on
- RewriteCond %{HTTP_HOST} !^111.com$
- RewriteRule ^/(.*)$ http://111.com/$1 [R=301.L]
- </IfModule>
- # <Directory /home/wwwroot/111.com> #目录认证
- # <FilesMatch yhtz.php> #匹配文件认证
- # AllowOverride AuthConfig
- # AuthName "账号密码访问"
- # AuthType Basic
- # AuthUserFile /data/.htpasswd
- # require valid-user
- # </FilesMatch>
- # </Directory>
- <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 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"
- </IfModule>
- 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
- ErrorLog "logs/111.com-error_log"
- CustomLog "|/usr/local/apache2.4/bin/rotatelogs -l logs/111.com-access__%Y%m%d.log 86400" combined env=!img
- </VirtualHost>
curl 测试, 看 cache-control: max-age
也可以在浏览器查看
扩展
来源: http://www.bubuko.com/infodetail-2618291.html