Search code examples
tomcatgzipstatic-files

How to configure tomcat to serve precompressed static files?


Under nginx I know a option:

gzip_static on;

which allows to access the precompressed gzip file with the same name, for example, if I have a file /var/www/style.css.gz, when request http://localhost/style.css with header ' Accept-Encoding:gzip the server will return the file on disk style.css.gz as style.css.

How can I do the same under Tomcat?


Solution

  • Tomcat doesn't have this feature.

    You could write a servlet/filter/etc. that can do it if you'd like.

    Starting with Tomcat 8.5, you can set the precompressed flag in web.xml to do this. You have to either modify conf/web.xml to enable this site-wide, or copy the configuration for the DefaultServlet from conf/web.xml into your application's WEB-INF/web.xml.

    You must set an <init-param> with the name precompressed to true, like this:

    <init-param>
      <param-name>precompressed</param-name>
      <param-value>true</param-value>
    </init-param>
    

    There are other options available, so you should read about Tomcat's DefaultServlet for more information.

    In Tomcat 8.0, the option is called gzip and is similar to above. You should read about Tomcat's DefaultServlet in Tomcat 8.0 if you are using it. Please note that Tomcat 8 has reached EOL and should no longer be used.