/技术
分类:技术最近更新:2026-04-15浏览:1868
如果防止自己的网站被其他网站 iframe 引用,除了在页面中直接用 js 加判断,还可以直接通过 nginx 添加配置。
前端在 js 里加判断,缺点很明显,需要每个页面有判断的执行语句(不推荐)
判断当前 window 属性是不是顶部 window 对象
javascriptif(top.location != self.location){ top.location = self.location; // 防止页面被框架包含 }
可以直接通过 nginx 配置(推荐)
不仅仅可以让所有页面全部禁止嵌套,还可以通过域名选择性判断。
bashlocation / { # 只允许同源域名进行 iframe 嵌套 add_header X-Frame-Options SAMEORIGIN; add_header X-Content-Type-Options nosniff; ……………… }
nginx 配置 X-Frame-Options 有四个参数:
1、DENY
表示该页面不允许在 frame 中展示,即便是在相同域名的页面中嵌套也不允许。
nginx 配置示例:add_header X-Frame-Options DENY;
2、SAMEORIGIN
表示该页面可以在相同域名页面的 frame 中展示。
nginx 配置示例:add_header X-Frame-Options SAMEORIGIN;
3、ALLOW-FROM url
表示该页面可以在指定来源的 frame 中展示。
nginx 配置示例:add_header X-Frame-Options 'ALLOW-FROM https://xxx.xxxxxx.com';
引号是必须要写的哦!
4、ALLOWALL
表示该页面允许全部来源域名的 frame 展示。