Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tomcat ExpiresFilter not working correctly

Tags:

tomcat

tomcat7

I can't get Tomcat to send images with the correct expires definition. The browser keep sending get requests for images already downloaded and Tomcat responds with 304. What I would want is that Tomcat will respond to the initial request with proper expires header and without any Last-modified header so the browser will use local cache until the file expires without going to the server each page load to see if the image had changed.

I have the following definition in my web.xml file:

<filter>
    <filter-name>ExpiresFilter</filter-name>
    <filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class>
    <init-param>
        <param-name>ExpiresByType image</param-name>
        <param-value>access plus 1 weeks</param-value>
    </init-param>
    <init-param>
        <param-name>ExpiresByType text/css</param-name>
        <param-value>modification plus 0 minutes</param-value>
    </init-param>
    <init-param>
        <param-name>ExpiresByType application/javascript</param-name>
        <param-value>modification plus 0 minutes</param-value>
    </init-param>
</filter>

<filter-mapping>
    <filter-name>ExpiresFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping> 

Any idea?

like image 507
Yosef Avatar asked Jul 11 '13 07:07

Yosef


1 Answers

Can you enabled ExpiresFilter logs as described in Container Provided Filters > ExpiresFilter > Troubleshooting :

Add to $CATALINA_BASE/conf/logging.properties the following declaration:

org.apache.catalina.filters.ExpiresFilter.level = FINE

You will see log message such as the following:

An expiration header is added:

Mar 26, 2010 2:09:47 PM org.apache.catalina.filters.ExpiresFilter onBeforeWriteResponseBody
  FINE: Request "/tomcat.gif" with response status "200"
  content-type "image/gif", set expiration date 3/26/10 2:19 PM

No expiration header is added:

Mar 26, 2010 2:10:27 PM org.apache.catalina.filters.ExpiresFilter onBeforeWriteResponseBody
  FINE: Request "/docs/config/manager.html" with response status "200"
  content-type "text/html", no expiration configured
like image 141
Cyrille Le Clerc Avatar answered Oct 19 '22 17:10

Cyrille Le Clerc