简体   繁体   English

如何为Jetty 9启用GZIP

[英]How to enable GZIP for Jetty 9

I'm trying to enabled gzip compression on Jetty 9. I do not want to configure it in my web.xml, so based on the Jetty documentation I've configured a override-web.xml. 我试图在Jetty 9上启用gzip压缩。我不想在我的web.xml中对其进行配置,因此根据Jetty文档,我已经配置了override-web.xml。 I'm not using Jetty in an embedded mode, but as a container. 我不是在嵌入式模式下使用Jetty,而是将其用作容器。

In my {jetty.home}/webapps folder, I've my war file - cc.war. 在我的{jetty.home}/webapps文件夹中,我有战争文件-cc.war。 I also have defined a cc.xml as follows 我还定义了一个cc.xml如下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">

<!-- ==================================================================
Configure and deploy the test web application in $(jetty.home)/webapps/test

Note. If this file did not exist or used a context path other that /test
then the default configuration of jetty.xml would discover the test
webapplication with a WebAppDeployer.  By specifying a context in this
directory, additional configuration may be specified and hot deployments
detected.
===================================================================== -->

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

  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <!-- Required minimal context configuration :                        -->
  <!--  + contextPath                                                  -->
  <!--  + war OR resourceBase                                          -->
  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <Set name="contextPath">/</Set>
  <Set name="war"><Property name="jetty.webapps"/>/cc.war</Set>

  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
  <!-- Optional context configuration                                  -->
  <!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!--   <Set name="extractWAR">true</Set>
  <Set name="copyWebDir">false</Set>
 -->
    <!--<Set name="defaultsDescriptor"><Property name="jetty.home" default="."/>/etc/webdefault.xml</Set>-->
  <Set name="overrideDescriptor"><Property name="jetty.webapps" default="."/>/cc.d/override-web.xml</Set>
</Configure>

In the folder cc.d, I've override-web.xml as follows: 在文件夹cc.d中,我按如下所示重写了web.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app
        xmlns="http://xmlns.jcp.org/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_2_5.xsd"
        version="2.5">

    <filter>
        <filter-name>GzipFilter</filter-name>
        <filter-class>org.eclipse.jetty.servlets.GzipFilter</filter-class>
        <init-param>
            <param-name>methods</param-name>
            <param-value>GET,POST</param-value>
        </init-param>
        <init-param>
            <param-name>mimeTypes</param-name>
            <param-value>text/html,text/xml,text/plain,text/css,text/javascript,text/json,application/x-javascript,application/javascript,application/json,application/xml,application/xml+xhtml,image/svg+xml</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>GzipFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping> 
</web-app>

Jetty seems to load this fine, but gzip compression is not applied on any of the responses. Jetty似乎可以很好地加载,但是gzip压缩未应用于任何响应。 I say Jetty loaded this fine because earlier when I tried to put the override-web.xml in the webapps folder, Jetty was complaining. 我说Jetty加载得很好,因为之前我尝试将override-web.xml放在webapps文件夹中时,Jetty抱怨。

I've gone through the various questions here on SO and but none of them seem to have an answer for this. 我已经在这里讨论了SO的各种问题,但是似乎没有一个答案。 Any help is appreciated. 任何帮助表示赞赏。

I think you should be able to configure common filters in webdefault.xml . 我认为您应该能够在webdefault.xml中配置常见过滤器。 Try registering the Gzip filter there. 尝试在此处注册Gzip过滤器。

One way to keep GZIP configuration out of your application while running in a Jetty container (non-embedded) is to add the filter to your context XML: 在Jetty容器(非嵌入式)中运行时,将GZIP配置排除在应用程序之外的一种方法是将过滤器添加到上下文XML:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <Set name="contextPath"><!-- set context path --></Set>
    <Set name="war"><!-- set war path --></Set>

    <Call name="addFilter">
        <Arg>org.eclipse.jetty.servlets.GzipFilter</Arg>
        <Arg>/*</Arg>
        <Arg>
            <Call name="of" class="java.util.EnumSet">
                <Arg><Get name="REQUEST" class="javax.servlet.DispatcherType" /></Arg>
            </Call>
        </Arg>
        <Call name="setInitParameter">
            <Arg>mimetypes</Arg>
            <Arg>text/html,text/xml,text/plain,text/css,text/javascript,text/json,application/x-javascript,application/javascript,application/json,application/xml,application/xml+xhtml,image/svg+xml</Arg>
        </Call>
        <Call name="setInitParameter">
            <Arg>methods</Arg>
            <Arg>GET,POST</Arg>
        </Call>
    </Call>
</Configure>

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

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