在配置 halo 博客 PluginUmami 插件时,会遇到报错信息,拒绝链接(如图)。

IMG-6D2C041FB335B9BE2A04D20ADDCED82F.webp

查看控制台报错信息如下:

Refused to frame 'https://umami.waistu.com/' because an ancestor violates the following Content Security Policy directive: "frame-ancestors 'self' undefined".
Understand this erro

主要为浏览器的 内容安全策略(Content Security Policy, CSP) 相关,通常出现在尝试将某个网页嵌入到 iframe、frame 或 object 等框架元素中时。


解决思路

查阅插件文档,给出的解决方案加入相关 Nginx 配置:

server{
    listen 443 ssl http2;
    server_name [umami.domain.com];
    #...
    add_header Access-Control-Allow-Origin 'https://halo.domain.com';
    add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
    add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
    if ($request_method = 'OPTIONS') {
        return 204;
    }

    add_header Content-Security-Policy 'frame-ancestors halo.domain.com';
    location / {
    #...
    proxy_hide_header 'Access-Control-Allow-Origin';
    proxy_hide_header 'Content-Security-Policy';
    }

}

根据插件文档,对 Nginx 配置文件进行修改,这里以 1Panel 面板为例,在 Nginx 配置文件添加下方内容,即可解决问题。

    add_header Access-Control-Allow-Origin 'https://blog.waistu.com';
    add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';
    add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
    add_header Content-Security-Policy 'frame-ancestors blog.waistu.com';
    proxy_hide_header 'Access-Control-Allow-Origin';
    proxy_hide_header 'Content-Security-Policy';
    if ($request_method = 'OPTIONS') {
      return 204;
    }

添加位置如图,并修改域名为实际 Halo 博客地址后,保存并重载 Nginx 配置文件即可。

IMG-52DEA25CEF9FAF267BBD4BA48A5EAC74.webp


最终效果

IMG-F512FDCA9C78E2687DDF6FE5DA8FDA2B.webp