简体   繁体   English

如何在Jetty中启用gzip压缩?

[英]How to enable gzip compression in Jetty?

I would like to know how to enable gzip compression for static files (js, css, html and images) in broadleaf. 我想知道如何为阔叶中的静态文件(js,css,html和图像)启用gzip压缩。 I have tried the following in web.xml file of project and I've also got the dependency corresponding to the org.eclipse.jetty.servlets.GzipFilter class: 我在项目的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>

The questions I have seeen on Stack Overflow didn't give me the answer, so am I posting a new question. 我在Stack Overflow上看到的问题没有给我答案,所以我要发布一个新问题。 In my project, I have a jetty-web.xml like this: 在我的项目中,我有一个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>

When I try to add the filter mappings in the jetty-web.xml file, I am getting an error that it isn't familiar with the filter tag. 当我尝试在jetty-web.xml文件中添加过滤器映射时,出现一个错误,提示它不熟悉filter标签。

Is there anything that I should add/modify in HTML/JS/CSS files? 我应该在HTML / JS / CSS文件中添加/修改任何内容吗?

Use init-param in webdefault.xml : webdefault.xml使用init-param

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

or add the handler to jetty.xml : 或将处理程序添加到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>

References 参考文献

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM