Caddy2:如何设置文件扩展名的MIME类型和内容处置?

发布于 2025-01-13 23:17:24 字数 1094 浏览 4 评论 0原文

配置/覆盖某些 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-codetext/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柏拉图鍀咏恒 2025-01-20 23:17:24

更新的响应

感谢@ccpizza

您可以通过对扩展名匹配应用一些正则表达式来做得更好。此外,如果默认值正确,则无需指定 mime 类型。所以像这样的东西也同样有效:

example.com {
    root * "/path/to/files/"

    @inlineFiles {
        path ^.*\.(py|mp4|ps1)$
    }
    header @inlineFiles {
        Content-Disposition inline
    }

    file_server {
        browse
    }
}

旧响应

尝试这样的东西

doma.in {
    root * "/path"

    @py {
        path *.py
    }
    header @py {
        Content-Type text/x-python
        Content-Disposition inline
    }

    file_server {
        browse
    }
}

如果您想添加 mp4 文件,只需添加另一个块,如下所示:

@mp4 {
    path *.mp4
}
header @mp4 {
    Content-Type video/mp4
    Content-Disposition inline
}

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:

example.com {
    root * "/path/to/files/"

    @inlineFiles {
        path ^.*\.(py|mp4|ps1)$
    }
    header @inlineFiles {
        Content-Disposition inline
    }

    file_server {
        browse
    }
}

Old Response

Try something like this

doma.in {
    root * "/path"

    @py {
        path *.py
    }
    header @py {
        Content-Type text/x-python
        Content-Disposition inline
    }

    file_server {
        browse
    }
}

If you would like to add mp4 files for example just add another block like this:

@mp4 {
    path *.mp4
}
header @mp4 {
    Content-Type video/mp4
    Content-Disposition inline
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文