Content-Encoding实体头用于压缩媒体类型。如果存在,它的值表示哪些编码应用于实体主体。它让客户知道,如何解码以获取Content-Type标题引用的媒体类型。
建议尽可能压缩数据并因此使用此字段,但某些类型的资源(如 jpeg 图像)已被压缩。有时使用额外的压缩不会减少有效载荷的大小,甚至可能使有效载荷更长。
Header typeEntity headerForbidden header nameno
语法
Content-Encoding: gzip
Content-Encoding: compress
Content-Encoding: deflate
Content-Encoding: identity
Content-Encoding: br// Multiple, in the order in which they were appliedContent-Encoding: gzip, identity
Content-Encoding: deflate, gzip
指令
gzip一种使用 Lempel-Ziv 编码( LZ77 )和32位 CRC 的格式。这最初是 UNIX gzip程序的格式。
x-gzip为了兼容性的目的,HTTP / 1.1 标准还建议支持该内容编码的服务器应该将其识别为别名。
compress使用 Lempel-Ziv-Welch( LZW )算法的格式。值名取自实施此算法的 UNIX 压缩程序。
与大多数 UNIX 发行版已经消失的压缩程序一样,目前几乎没有浏览器使用这种内容编码,部分原因是由于专利问题(已在2003年过期)。
deflate使用 deflate压缩算法(在 RFC 1951中定义)使用 zlib 结构(在 RFC 1950中定义)。
identity指示身份功能(即不压缩,也不修改)。除非明确指定,否则此标记始终被视为可接受。
br使用 Brotli 算法的格式。
例子
用 gzip 压缩
在客户端,您可以公布一个将在 HTTP 请求中发送的压缩方案列表。Accept-Encoding header 被用于协商内容编码。
Accept-Encoding: gzip, deflate
服务器响应所使用的方案,由Content-Encoding响应 header 。
Content-Encoding: gzip
请注意,服务器没有义务使用任何压缩方法。压缩高度依赖于服务器设置和使用的服务器模块。
规范
SpecificationTitleRFC 7231, section 3.1.2.2: Content-EncodingHypertext Transfer Protocol (HTTP/1.1): Semantics and Content
RFC 7932: Brotli Compressed Data FormatBrotli Compressed Data Format
浏览器兼容性
FeatureChromeEdgeFirefoxInternet ExplorerOperaSafariBasic Support(Yes)(Yes)(Yes)(Yes)(Yes)(Yes)
br50No44.0No36.0No
FeatureAndroidChrome for AndroidEdge mobileFirefox for AndroidIE mobileOpera AndroidiOS SafariBasic Support(Yes)(Yes)(Yes)(Yes)(Yes)(Yes)(Yes)
br5151No44.0NoNoNo