配置静态元素过期时间目录概要
- 浏览器访问网站的图片时会把静态的文件缓存在本地电脑里,这样下次再访问时就不用去远程下载了 增加配置
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"
- 需要expires_module
- curl测试,看cache-control: max-age
配置静态元素过期时间
- 静态元素,就是访问的图片、css、js
- 当用浏览器去访问一个网站的时候,这个网站里所有的静态文件(比如图片的样式、js),浏览器就会默认把静态文件缓存在电脑里,叫做临时的目录或目录
- 缓存的时间是在服务器上定义的,如果不去定义,那么浏览器也不会把这些文件清空,或者浏览器有自己的机制去清空这些文件,或者说电脑软件会定时帮你清理这些缓存的文件
- 为什么电脑会自动加载这些静态文件呢?目的就是第二次,第三次访问的时候,不去服务器去下载这些静态文件了
- 在使用浏览器自带的F12键去进行比对,第一次访问一个图片的时候是200的状态码,第二次访问的时候,就是304,它检测到下载的图片并没有修改过,所以就不会重新到服务器下载一次,这样可以节省带宽,但没有规定缓存什么时候清空,什么时候去服务器上去下载,再次下载,因为只要浏览器检测到图片未做更改,它这个状态码就一直为304,304这个状态码就意味着他不会去服务器下载
定义静态文件失效日期
- expires_module模块,定义失效日期
- 打开虚拟主机配置文件vim /usr/local/apache2.4/conf/extra/httpd-vhosts.conf,并添加配置文件
- 需要添加的配置文件
- 这里有所有的图片定义成一天
- 所有的 css 和 js 都定义成两小时
- 其他的没有任何的缓存
ExpiresActive on //打开该功能的开关 ExpiresByType image/gif "access plus 1 days" //定义Type类型,这里是一天 ExpiresByType image/jpeg "access plus 24 hours" //定义Type类型,这里是24小时 ExpiresByType image/png "access plus 24 hours" //定义Type类型 ExpiresByType text/css "now plus 2 hour" //定义Type类型,两小时 ExpiresByType application/x-javascript "now plus 2 hours" // 定义Type类型 ExpiresByType application/javascript "now plus 2 hours" //定义Type类型 ExpiresByType application/x-shockwave-flash "now plus 2 hours" //定义Type类型 ExpiresDefault "now plus 0 min" //定义Type类型
- 在配置文件中添加
[root@hf-01 ~]# vim /usr/local/apache2.4/conf/extra/httpd-vhosts.confDocumentRoot "/data/wwwroot/111.com" ServerName 111.com DocumentRoot "/data/wwwroot/111.com" ServerName 111.com ServerAlias www.example.com 2111.com.cn # 并保存退出# # AllowOverride AuthConfig # AuthName "111.com user auth" # AuthType Basic # AuthUserFile /data/.htpasswd # require valid-user # #RewriteEngine on RewriteCond %{HTTP_HOST} !^111.com$ RewriteRule ^/(.*)$ http://111.com/$1 [R=301,L] 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" 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/123.com-access_%Y%m%d.log 86400" combined env=!img
- 然后检查是否存在语法错误
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl -tSyntax OK[root@hf-01 ~]#
- 查看expire模块是否打开,会发现expire模块没有打开
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl -M |grep expire[root@hf-01 ~]#
- 编辑主配置文件,打开expire模块
- vim /usr/local/apache2.4/conf/httpd.conf
[root@hf-01 ~]# vim /usr/local/apache2.4/conf/httpd.conf搜索 /expire ,找到该行的配置文件,并去除的注释符 # 号LoadModule expires_module modules/mod_expires.so然后保存退出
- 在重新加载配置文件,并查看expire模块
[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl graceful[root@hf-01 ~]# /usr/local/apache2.4/bin/apachectl -M |grep expire expires_module (shared)[root@hf-01 ~]#
- 按ctrl+F5可以强制把浏览器本地的缓存清空
- 用curl命令去访问图片
[root@hf-01 111.com]# curl -x127.0.0.1:80 111.com/11.png -IHTTP/1.1 200 OKDate: Thu, 21 Dec 2017 22:42:28 GMT //当前时间Server: Apache/2.4.29 (Unix) PHP/5.6.30Last-Modified: Thu, 21 Dec 2017 14:40:05 GMTETag: "20aa-560daacfdbb40"Accept-Ranges: bytesContent-Length: 8362Cache-Control: max-age=86400 //缓存的时间Expires: Fri, 22 Dec 2017 22:42:28 GMT //过期时间Content-Type: image/png[root@hf-01 111.com]#
- 若是将expire模块去除,再去curl命令查看图片,就无法看到Cache-Control参数