设置网站expires和max-age属性

简介: 转:http://www.zicheng.net/article/982022.htm在使用百度站长工具测试网站优化建议时,在 设置静态内容缓存时间 栏目里,会提示 类似 FAILED - (未设置max-age或expires) - http://www.zicheng.net 的内容,我也是遇到同样的问题,经过多次搜索最终找到解决办法,先分别就在nignx、tomcat以及apache中如何设置max-age或expires参数进行简单的讲解。

转:http://www.zicheng.net/article/982022.htm

在使用百度站长工具测试网站优化建议时,在 设置静态内容缓存时间 栏目里,会提示 类似 FAILED - (未设置max-age或expires) - http://www.zicheng.net 的内容,我也是遇到同样的问题,经过多次搜索最终找到解决办法,先分别就在nignx、tomcat以及apache中如何设置max-age或expires参数进行简单的讲解。

分为三块来讲,针对apache,tomcat,nginx

1. apache设置max-age或expires

这里需要修改.htaccess文件。

<IfModule mod_headers.c>

    <FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)$">

        Header set Cache-Control "max-age=604800, public"

    </FilesMatch>

<FilesMatch "\.(xml|txt)$">

    Header set Cache-Control "max-age=18000, public, must-revalidate"

</FilesMatch>

<FilesMatch "\.(html|htm|php|shtml)$">

    Header set Cache-Control "max-age=3600, must-revalidate"

</FilesMatch>

</IfModule>

2. tomcat中设置max-age或expires

首先pom.xml需要引入catalina包,如果不是使用的maven,请自行搜索下载jar包


<dependency>

   <groupId>org.apache.tomcat</groupId>

   <artifactId>tomcat-catalina</artifactId>

   <version>7.0.61</version>

</dependency>

注意,版本必须是7.0.61以上的,如果你不是maven需要引入jar包及相关的依赖包。

其次,然后找到你j2ee项目中的web.xml文件,在文件中加入如下内容


  <filter>

      <filter-name>ExpiresFilter</filter-name>

      <filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>

      <init-param>

          <param-name>ExpiresByType text/html</param-name>

          <param-value>access plus 1 minutes</param-value>

      </init-param>

      <init-param>

          <param-name>ExpiresByType image</param-name>

          <param-value>access plus 10 years</param-value>

      </init-param>

      <init-param>

          <param-name>ExpiresByType text/css</param-name>

          <param-value>access plus 10 months</param-value>

      </init-param>

      <init-param>

          <param-name>ExpiresByType application/javascript</param-name>

          <param-value>access plus 10 months</param-value>

      </init-param>

      <init-param>

          <param-name>ExpiresByType application/x-unknown-content-type</param-name>

          <param-value>access plus 10 years</param-value>

      </init-param>

  </filter>

  <filter-mapping>

      <filter-name>ExpiresFilter</filter-name>

      <url-pattern>/*</url-pattern>

      <dispatcher>REQUEST</dispatcher>

    </filter-mapping>

以上内容分别对js脚本、css样式、图片以及html页面进行了缓存设置。

其中param-value的值可以设置为比如access plus 1 month 15 days 2 hours

不可以使用以下的任意的类型或类型组合,(这个我没看懂!~)

years
months
weeks
days
hours
minutes
seconds

PS:再次提醒catalina的版本要7.0.61以上的才行,低版本里未实现filters.ExpiresFilter类。

3.nginx设置max-age或expires

在server节点下加入如下代码


  location  ~* \.(gif|jpg|png|bmp)$ {

   expires 10d;

  }

这里是设置图片的过期时间为10天。如果你的图片基本不更新可以设置的时间长一些。

目录
相关文章
|
6月前
|
Python
尝试获取一个非对象的"expire_time"属性
尝试获取一个非对象的"expire_time"属性
53 2
|
6月前
|
Python
如果一个变量没有"expire_time"属性
如果一个变量没有"expire_time"属性
40 3
|
7天前
|
Java
@Max和@Min注解失效
Springboot 2.3及以上版本不再内置hibernate-validator 6.0.13.Final添加上述依赖可解决。
13 3
|
9月前
|
C#
C#属性的get与set
C#属性的get与set
|
9月前
|
存储 关系型数据库 MySQL
字段为什么要设置成 not null?
字段为什么要设置成 not null?
118 0
|
前端开发
设置响应头Access-Control-Max-Age减少前端OPTIONS请求
设置响应头Access-Control-Max-Age减少前端OPTIONS请求
131 0
|
存储 PHP
PHP为什么需要设置session_set_cookie_params()函数来配置session的cookie参数?
PHP为什么需要设置session_set_cookie_params()函数来配置session的cookie参数?
102 0
|
C#
get和set访问器
get和set访问器
210 0
|
存储 缓存 CDN
HTTP 头部字段 Cache Control max-age = 0 和 no-cache 的区别
HTTP 头部字段 Cache Control max-age = 0 和 no-cache 的区别
501 0
HTTP 头部字段 Cache Control max-age = 0 和 no-cache 的区别
修改sequenc number
create or replace procedure CHANGE_SEQ_NUMBER( PI_SEQ_OWNER VARCHAR2 , PI_SEQNAME VARCHAR2 , PI_LAST_NUMBER NUMBER) IS V_LastValue integer; V_incr...
654 0