Caddy2:如何设置文件扩展名的MIME类型和内容处置?
配置/覆盖某些 MIME 类型的处理方式的方法是什么?
例如,使用默认配置时,Python 文件 (.py
) 会使用 Content-disposition":attachment
提供服务,该文件会打开文件保存对话框,而不是在中显示内联纯文本 虽然这有效,但
我已经确定了一种可能的方法:
@py {
path *.py
}
header @py Content-Type text/plain
它看起来更像是一种黑客行为,而不是添加/修改 mime 类型,因为它只是根据子字符串匹配设置标头,这是唯一可能的方法吗?
什么定义了应提供特定 MIME 类型内联或有 "Content-disposition":attachment
吗? caddy 是否公开了一种简单的方法来配置 MIME 类型的处理方式?
.py
文件的正确 mime 类型是 >application/x-python-code
或 text/x-python
我如何配置 caddy 将这些类型视为内联纯文本,而不添加 Content-disposition “:附件
标题?强制 text/plain
可以作为一种解决方法,但我假设有一种更简洁的方法可以做到这一点。
更新:进一步细读 caddy 文档 看来 caddy 依赖于 go 的 有限的 MIME 集types,然后使用特定于操作系统的全局 MIME 类型进一步扩展,例如 /etc/mime.types
、/etc/apache2/mime.type
、等,或者如果在 Windows 上则从注册表中。
What is the way to configure/override how certain MIME types are handled?
For example, with the default configuration a python file (.py
) is served with Content-disposition": attachment
which opens a file save dialog rather than displaying inline plain text in the browser.
I've identified one possible approach:
@py {
path *.py
}
header @py Content-Type text/plain
While this works, it looks more of a hack rather than adding/modifying a mime type, given that it's simply setting a header based on a substring match. Is this the only possible approach?
What defines that a specific MIME type should be served inline or have "Content-disposition": attachment
? Does caddy expose a straightforward way to configure how MIME types are handled?
The correct mime type for .py
files is application/x-python-code
or text/x-python
. How can I configure caddy to treat these types as inline plain text, and not add the Content-disposition": attachment
header? Forcing text/plain
works as a workaround, but I am assuming there is a cleaner way to do it.
Update: on further perusal of caddy docs it appears that caddy relies on go's rather limited set of MIME types, which are then further extended with OS-specific global MIME types from e.g. /etc/mime.types
, /etc/apache2/mime.type
, etc, or from the registry if on windows.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新的响应
感谢@ccpizza
您可以通过对扩展名匹配应用一些正则表达式来做得更好。此外,如果默认值正确,则无需指定 mime 类型。所以像这样的东西也同样有效:
旧响应
尝试这样的东西
如果您想添加 mp4 文件,只需添加另一个块,如下所示:
Updated Response
Thanks @ccpizza
You can do this a lot nicer by applying some regex to the extension matching. Also, specifying the mime type is not necessary if the default is correct. So something like this works just as well:
Old Response
Try something like this
If you would like to add mp4 files for example just add another block like this: