search.png
关于我
menu.png
go http.FileServer无法识别 favicon.ico

在docker 容器里运行go程序,里边使用到了http.FileServer作为静态文件的代理,访问其它的静态文件都正常,唯独favicon.ico报错。

runtime/debug.Stack(0x974fa5, 0x8, 0xc000058730)
        /home/ubuntu/soft/go/src/runtime/debug/stack.go:24 +0x9f
main.exceptionHandler.func1.1(0xa61ef8, 0xc00037a620, 0xa5c480, 0xc00000ede0)
        /home/ubuntu/images/momotodo/momotodo/src/momotodo.go:729 +0x18b
panic(0x908ba0, 0xe7efc0)
        /home/ubuntu/soft/go/src/runtime/panic.go:965 +0x1b9
main.(*StaticFile).Seek(0xc000049ea0, 0x0, 0x0, 0x976c35, 0xc, 0x200)
        <autogenerated>:1 +0x32
net/http.serveContent(0xa61ef8, 0xc00037a620, 0xc00035b700, 0x97eb45, 0xb, 0x0, 0x0, 0x0, 0xc0000a55f8, 0x7fc1ec63af58, ...)
        /home/ubuntu/soft/go/src/net/http/fs.go:241 +0x1412
net/http.serveFile(0xa61ef8, 0xc00037a620, 0xc00035b700, 0xa5ba20, 0xc00000ed98, 0xc000374d40, 0xc, 0xc000368c01)
        /home/ubuntu/soft/go/src/net/http/fs.go:664 +0x61f
net/http.(*fileHandler).ServeHTTP(0xc0000ec8b0, 0xa61ef8, 0xc00037a620, 0xc00035b700)
        /home/ubuntu/soft/go/src/net/http/fs.go:848 +0x9c
net/http.StripPrefix.func1(0xa61ef8, 0xc00037a620, 0xc00035b600)
        /home/ubuntu/soft/go/src/net/http/server.go:2092 +0x234
        ...

但是在本地却无法复现。遂打断点,断到该文件的请求,看看是怎么处理的,于是乎看到了这样一段代码:

// The built-in table is small but on unix it is augmented by the local
// system's mime.types file(s) if available under one or more of these
// names:
//
//   /etc/mime.types
//   /etc/apache2/mime.types
//   /etc/apache/mime.types
//
// On Windows, MIME types are extracted from the registry.
//
// Text types have the charset parameter set to "utf-8" by default.
func TypeByExtension(ext string) string {
    once.Do(initMime)

    // Case-sensitive lookup.
    if v, ok := mimeTypes.Load(ext); ok {
        return v.(string)
    }

好家伙,是从系统读取的这些数据。我进容器一看,果然没有/etc/mime.types。

那解决的方法就很简单了,在docker运行的地方把这个文件挂载上。

-v /etc/mime.types:/etc/mime.types

再次查询,果然出来了。

版权声明

知识共享许可协议 本文章由作者“衡于墨”创作,转载请注明出处,未经允许禁止用于商业用途

本作品采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可。
发布时间:2021年08月03日 22:24:43

评论区#

还没有评论哦,期待您的评论!

关闭特效