阅读本文需要了解如何给 Web Station 中的站点添加自定义配置。可以参阅此文群晖WebStation中使用Nginx作为服务器配置自定义规则

创建一个配置文件:

# /etc/nginx/conf.d/static_cache.conf

# 1. 图片、视频、音频等大文件 - 缓存 30 天
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
    expires 30d;
    access_log off;
    # 可选: 防止盗链
    # valid_referers none blocked server_names *.example.com;
    # if ($invalid_referer) { return 403; }
}

# 2. 字体文件 - 缓存 365 天
location ~ .*\.(eot|ttf|otf|woff|woff2|svg)$ {
    expires 365d;
    access_log off;
    # 字体跨域设置 (CORS) - 很多字体加载需要这个
    add_header Access-Control-Allow-Origin *;
}

# 3. JS 和 CSS 文件 - 缓存 7 天 (根据是否带hash指纹调整)
location ~ .*\.(js|css)$ {
    expires 7d;
    access_log off;
}

# 4. 禁止缓存 HTML (确保入口文件永远最新)
location ~ .*\.(html|htm)$ {
    add_header Cache-Control "no-cache, must-revalidate";
    expires 0;
}

在需要启用的站点上加载此配置文件:

# /usr/local/etc/nginx/conf.d/58e187bd-30bd-4e7e-8dca-670f2329ed12/user.conf

if ($host ~ "(^simaek.com$)") {
    rewrite ^(.*) https://www.simaek.com$1 permanent;
}

location / {
    index index.html index.php;

    if (-f $request_filename/index.html) {
        rewrite (.*) $1/index.html break;
    }

    if (-f $request_filename/index.php) {
        rewrite (.*) $1/index.php;
    }

    if (!-f $request_filename) {
        rewrite (.*) /index.php;
    }
}

include conf.d/static_cache.conf;
最后修改:2025 年 12 月 12 日
如果觉得我的文章对你有用,请随意赞赏