zoukankan      html  css  js  c++  java
  • 设置网站expires和max-age属性

    转: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天。如果你的图片基本不更新可以设置的时间长一些。

  • 相关阅读:
    HDU 1124 Factorial
    hdu 1690 Bus System
    hdu 1113 Word Amalgamation
    POJ 2482 Stars in Your Window
    hdu 1385 ZOJ 1456 Minimum Transport Cost(经典floyd)
    hdu 1907 John
    VMware 虚拟机 安装 UBuntu 9.10 命令模式转换成窗口模试
    #pragma CODE_SEG __NEAR_SEG NON_BANKED详解
    Ubuntu 下Hadoop 伪分布式 hadoop0.20.2.tar.gz 的安装
    文件拷贝代码以及疑问
  • 原文地址:https://www.cnblogs.com/ae6623/p/5319816.html
Copyright © 2011-2022 走看看