【问题标题】:How to enable gzip compression in Jetty?如何在 Jetty 中启用 gzip 压缩?
【发布时间】:2015-02-15 05:20:58
【问题描述】:

我想知道如何为 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 时,我收到一个错误,它不熟悉 过滤标签。

我应该在 HTML/JS/CSS 文件中添加/修改什么吗?

【问题讨论】:

    标签: jetty gzip configuration-files servlet-filters


    【解决方案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>
    

    参考文献

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-15
      • 1970-01-01
      • 2013-12-29
      • 1970-01-01
      • 2014-09-26
      • 2018-08-20
      • 2013-01-08
      • 2013-05-05
      相关资源
      最近更新 更多