静态文件服务 #

一、基本配置 #

1.1 静态目录 #

go
package main

import "github.com/gofiber/fiber/v2"

func main() {
    app := fiber.New()
    
    // 静态文件目录
    app.Static("/static", "./public")
    
    // 访问 /static/css/style.css → ./public/css/style.css
    
    app.Listen(":3000")
}

1.2 多个静态目录 #

go
app.Static("/static", "./public")
app.Static("/uploads", "./uploads")
app.Static("/assets", "./assets")

二、配置选项 #

2.1 基本配置 #

go
app.Static("/static", "./public", fiber.Static{
    // 是否浏览目录
    Browse: false,
    
    // 默认索引文件
    Index: "index.html",
    
    // 缓存控制
    MaxAge: 3600,
    
    // 是否压缩
    Compress: true,
})

2.2 完整配置 #

go
app.Static("/static", "./public", fiber.Static{
    // 浏览目录
    Browse: true,
    
    // 索引文件
    Index: "index.html",
    
    // 缓存时间(秒)
    MaxAge: 86400,
    
    // 启用压缩
    Compress: true,
    
    // 缓存字节范围请求
    ByteRange: true,
    
    // 自动刷新
    AutoRefresh: false,
    
    // 下载文件
    Download: false,
})

三、目录浏览 #

3.1 启用目录浏览 #

go
app.Static("/files", "./uploads", fiber.Static{
    Browse: true,
})

3.2 自定义浏览模板 #

go
app.Static("/files", "./uploads", fiber.Static{
    Browse: true,
    BrowseTemplate: `
    <!DOCTYPE html>
    <html>
    <head><title>Files</title></head>
    <body>
        <h1>Directory Listing</h1>
        <ul>
        {{range .}}
            <li><a href="{{.URL}}">{{.Name}}</a></li>
        {{end}}
        </ul>
    </body>
    </html>
    `,
})

四、缓存控制 #

4.1 设置缓存时间 #

go
// 缓存1小时
app.Static("/static", "./public", fiber.Static{
    MaxAge: 3600,
})

// 缓存1天
app.Static("/assets", "./assets", fiber.Static{
    MaxAge: 86400,
})

// 禁用缓存
app.Static("/nocache", "./temp", fiber.Static{
    MaxAge: 0,
})

4.2 自定义Cache-Control #

go
app.Use("/static/*", func(c *fiber.Ctx) error {
    c.Set("Cache-Control", "public, max-age=31536000")
    return c.Next()
})

app.Static("/static", "./public")

五、压缩 #

5.1 启用压缩 #

go
app.Static("/static", "./public", fiber.Static{
    Compress: true,
})

5.2 压缩级别 #

go
app.Use(compress.New(compress.Config{
    Level: compress.LevelBestCompression,
}))

app.Static("/static", "./public")

六、SPA应用 #

6.1 单页应用配置 #

go
func main() {
    app := fiber.New()
    
    // 静态资源
    app.Static("/static", "./dist/static")
    
    // 所有路由返回index.html
    app.Get("*", func(c *fiber.Ctx) error {
        return c.SendFile("./dist/index.html")
    })
    
    app.Listen(":3000")
}

6.2 带API的SPA #

go
func main() {
    app := fiber.New()
    
    // API路由
    api := app.Group("/api")
    api.Get("/users", getUsers)
    api.Post("/users", createUser)
    
    // 静态资源
    app.Static("/static", "./dist/static")
    
    // SPA回退
    app.Get("*", func(c *fiber.Ctx) error {
        return c.SendFile("./dist/index.html")
    })
    
    app.Listen(":3000")
}

七、文件下载 #

7.1 单文件下载 #

go
app.Get("/download/:filename", func(c *fiber.Ctx) error {
    filename := c.Params("filename")
    return c.Download("./files/" + filename)
})

7.2 带自定义文件名 #

go
app.Get("/download/:id", func(c *fiber.Ctx) error {
    id := c.Params("id")
    return c.Download("./files/"+id, "document.pdf")
})

八、安全配置 #

8.1 限制访问 #

go
app.Use("/private/*", func(c *fiber.Ctx) error {
    token := c.Query("token")
    if token != "secret" {
        return c.SendStatus(403)
    }
    return c.Next()
})

app.Static("/private", "./private")

8.2 禁止访问敏感文件 #

go
app.Use("/static/*", func(c *fiber.Ctx) error {
    path := c.Path()
    
    // 禁止访问隐藏文件
    if strings.Contains(path, "/.") {
        return c.SendStatus(403)
    }
    
    // 禁止访问特定扩展名
    ext := filepath.Ext(path)
    forbidden := map[string]bool{
        ".env": true,
        ".git": true,
        ".htaccess": true,
    }
    
    if forbidden[ext] {
        return c.SendStatus(403)
    }
    
    return c.Next()
})

九、总结 #

9.1 静态文件配置 #

配置 说明
Browse 目录浏览
Index 默认索引文件
MaxAge 缓存时间
Compress 启用压缩
ByteRange 字节范围请求

9.2 下一步 #

现在你已经了解了静态文件服务,接下来让我们学习 数据库概述,了解Fiber如何与数据库集成!

最后更新:2026-03-28