Tell me please. How to enable file caching for tomcat 8 ? Faced with the fact that the site is loaded for a long time - squeezed css , js . It became easier, but Google advises to enable browser caching. How to do it on tomcat 8 I do not know.

Thank.

What Google writes:

Use browser cache

If you specify the date or expiration date of static resources in the HTTP headers, the browser will download the resources already received from the local disk and not from the Internet.

And there is a list of files.

Answer:

 Accept-Ranges:bytes Cache-Control:max-age=86400 Content-Encoding:gzip Content-Type:text/css Date:Thu, 20 Apr 2017 09:29:02 GMT ETag:W/"27653-1492672656000" Expires:Fri, 21 Apr 2017 09:29:02 GMT Last-Modified:Thu, 20 Apr 2017 07:17:36 GMT Server:Apache-Coyote/1.1 Transfer-Encoding:chunked Vary:Accept-Encoding 
  • Add to the question the phrase that Google tells you. and add where he tells you that. - Mikhail Vaysman
  • @MikhailVaysman added to the top. - Tsyklop

1 answer 1

This function can be added using filters. You are interested in Expires Filter . This configuration will enable caching for just one month.

 <filter> <filter-name>ExpiresFilter</filter-name> <filter-class>org.apache.catalina.filters.ExpiresFilter</filter-class> <init-param> <param-name>ExpiresDefault</param-name> <param-value>access plus 1 month</param-value> </init-param> </filter> <filter-mapping> <filter-name>ExpiresFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> </filter-mapping> 
  • I added this to the web.xml of the tomcat itself. But how to see the result? - Tsyklop
  • in Chrome Dev Tools or Firebug, see the response headers. there will be Expires and Cache-Control: max-age - Mikhail Vaysman
  • had seen. but for some reason I have Expires cost per day. An example of the answer was thrown into the cap. - Tsyklop
  • did you overload the server? what page did you check? - Mikhail Vaysman
  • Yes, I rebooted. The main thing. - Tsyklop