如何在Jetty中启用gzip压缩?

3

我想知道如何在Broadleaf中为静态文件(js、css、html和图像)启用gzip压缩。我尝试了在项目的web.xml文件中添加以下内容,并且已经添加了与org.eclipse.jetty.servlets.GzipFilter类相对应的依赖:

<filter>
       <filter-name>GzipFilter</filter-name>
       <filter-class> org.eclipse.jetty.servlets.GzipFilter</filter-class>
       <init-param>
         <param-name>mimeTypes</param-name>
         <param-value>text/html,text/plain,text/xml,application/xhtml+xml,text/css,application/javascript,image/svg+xml</param-value>
       </init-param>
   </filter>

   <filter-mapping>
      <filter-name>GzipFilter</filter-name>
      <url-pattern>/*</url-pattern>
   </filter-mapping>

我在Stack Overflow上看到的问题没有给我答案,所以我要发布一个新问题。在我的项目中,我有一个名为jetty-web.xml的文件,内容如下:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">

<Configure class="org.eclipse.jetty.webapp.WebAppContext">

    <Set name="contextPath">/</Set>

</Configure>

当我尝试在jetty-web.xml文件中添加“filter mappings”时,出现了错误,说它不熟悉“filter”标签。 HTML/JS/CSS文件中是否需要添加或修改任何内容?
1个回答

1
使用webdefault.xml中的init-param:
<init-param>
  <param-name>gzip</param-name>
  <param-value>true</param-value>
</init-param>

或者将处理程序添加到jetty.xml中:
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">

<!-- =============================================================== -->
<!-- Mixin the GZIP Handler                                          -->
<!-- This applies the GZIP Handler to the entire server              -->
<!-- If a GZIP handler is required for an individual context, then   -->
<!-- use a context XML (see test.xml example in distribution)        -->
<!-- =============================================================== -->

<Configure id="Server" class="org.eclipse.jetty.server.Server">
  <Call name="insertHandler">
    <Arg>
      <New id="GzipHandler" class="org.eclipse.jetty.server.handler.gzip.GzipHandler">
        <Set name="minGzipSize"><Property name="jetty.gzip.minGzipSize" deprecated="gzip.minGzipSize" default="2048"/></Set>
        <Set name="checkGzExists"><Property name="jetty.gzip.checkGzExists" deprecated="gzip.checkGzExists" default="false"/></Set>
        <Set name="compressionLevel"><Property name="jetty.gzip.compressionLevel" deprecated="gzip.compressionLevel" default="-1"/></Set>
        <Set name="inflateBufferSize"><Property name="jetty.gzip.inflateBufferSize" default="0"/></Set>
        <Set name="syncFlush"><Property name="jetty.gzip.syncFlush" default="false" /></Set>

        <Set name="excludedAgentPatterns">
          <Array type="String">
            <Item><Property name="jetty.gzip.excludedUserAgent" deprecated="gzip.excludedUserAgent" default=".*MSIE.6\.0.*"/></Item>
          </Array>
        </Set>

        <Set name="includedMethodList"><Property name="jetty.gzip.includedMethodList" default="GET" /></Set>
        <Set name="excludedMethodList"><Property name="jetty.gzip.excludedMethodList" default="" /></Set>

<!--
        <Set name="includedMethods">
          <Array type="String">
            <Item>GET</Item>
          </Array>
        </Set>
        <Set name="includedPaths">
          <Array type="String">
            <Item>/*</Item>
          </Array>
        </Set>
        <Set name="excludedPaths">
          <Array type="String">
            <Item>*.gz</Item>
          </Array>
        </Set>
        <Call name="addIncludedMimeTypes">
          <Arg><Array type="String">
            <Item>some/type</Item>
          </Array></Arg>
        </Call>
        <Call name="addExcludedMimeTypes">
          <Arg><Array type="String">
            <Item>some/type</Item>
          </Array></Arg>
        </Call>
-->

      </New>
    </Arg>
  </Call>
</Configure>

参考文献

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接