【问题标题】:Why isn't my Markdown file being gzipped?为什么我的 Markdown 文件没有被 gzip 压缩?
【发布时间】:2013-03-19 12:05:14
【问题描述】:

我的.htaccess 文件中有这个:

# gzip compression
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript application/json
</ifmodule>

这确保给定的内容类型被压缩。

现场演示:http://ecmazing.com/js/index.js(在浏览器的开发工具中打开“Net”面板,查看响应头)

但我的 Markdown 文件没有被 gzip 压缩。

现场演示: http://ecmazing.com/data.md

如您所见,文件的内容类型是text/plain,在我的.htaccess 文件的内容类型列表中。那么,为什么不应用 GZIP 呢?

【问题讨论】:

  • 并且以.md 结尾的文件与您在AddOutputFilterByType 之后列出的一种 Mime 类型相关联……?你试过AddOutputFilter deflate md吗?
  • @CBroe 服务器响应Content-Type text/plain。这不是说是吗?
  • @CBroe 我已经添加了AddOutputFilter deflate md,它现在可以工作了:)。你能解释一下为什么会这样吗?
  • 我猜您的降价文件没有通过AddType 指令主动分配内容类型 text/plain - 因此服务器可能会将该内容类型作为默认值发送,但 @987654335 @ 不会自动建立那个连接……
  • @CBroe 感谢您的信息:)

标签: apache .htaccess gzip markdown


【解决方案1】:

我猜你的 markdown 文件没有通过AddType 指令主动分配内容类型text/plain——因此服务器可能会将该内容类型作为默认值发送,但AddOutputFilterByType 无法识别这些文件自动成为该内容类型。

http://httpd.apache.org/docs/2.2/en/mod/core.html#addoutputfilterbytype 确认说:

“在某些情况下,使用 AddOutputFilterByType 启用过滤器可能会部分或完全失败。例如,如果无法确定 MIME 类型并回退到 DefaultType 设置,即使 DefaultType 相同,也不会应用任何过滤器。”

所以请改用AddOutputFilter deflate md - 或尝试使用AddType text/plain md.md 与内容类型明确关联。

【讨论】:

  • 是的。我最终做到了AddType 'text/plain; charset=UTF-8' md。我也添加了字符集,因为我的 MD 包含非 ASCII 字符。
最近更新 更多