如何在 Jetty 中启用 gzip 压缩?
Posted
技术标签:
【中文标题】如何在 Jetty 中启用 gzip 压缩?【英文标题】:How to enable gzip compression in Jetty? 【发布时间】: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 文件中添加/修改什么吗?
【问题讨论】:
【参考方案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>
参考文献
jetty.project/jetty-gzip.xml at master · eclipse/jetty.project jetty.project/webdefault.xml at master· eclipse/jetty.project The document type descriptor for the org.eclipse.jetty.xml.XmlConfiguration class Configuring the Jetty Maven Plugin XmlConfiguration (Jetty :: Project 9.4.3.v20170317 API) Issue #1566 · eclipse/jetty.project: ServletContext is not reset/cleared between stop/start【讨论】:
以上是关于如何在 Jetty 中启用 gzip 压缩?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Angular cli 中启用 gzip 压缩以进行生产构建
Java如何在spring boot中启用gzip压缩并验证其生效