iframe嵌套其他网站被拒绝
X-Frame-Options: HTTP 响应头是用来给浏览器 指示允许一个页面 可否在<frame>
, <iframe>
, <embed>
或者 <object>
中展现的标记。站点可以通过确保网站没有被嵌入到别人的站点里面,从而避免 点击劫持 攻击。
X-Frame-Options 有三个属性值:
- deny
表示该页面不允许在frame中展示,即便是在相同域名的页面中嵌套也不允许。 - sameorigin
表示该页面可以在相同域名页面的frame中展示。 - allow-from uri
表示该页面可以在指定来源的frame中展示。
nginx配置
需要添加到 ‘http’, ‘server’ 或者 ‘location’ 的配置项中,个人来讲喜欢配置在‘server’ 中
正常情况下都是使用SAMEORIGIN参数,允许同域嵌套
add_header X-Frame-Options SAMEORIGIN;
允许单个域名iframe嵌套
add_header X-Frame-Options ALLOW-FROM http://whsir.com/;
允许多个域名iframe嵌套,注意这里是用逗号分隔
add_header X-Frame-Options "ALLOW-FROM http://whsir.com/,https://cacti.org.cn/";
nginx示例
server { listen 80; server_name *.xxxxxxx.xyz; location /public/ { autoindex on; alias /usr/local/nginx/html/web/myblog/node/public/; } location /webgate/ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:1314/api/; } location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # add_header X-Frame-Options deny; # 此处设置X-Frame-Options配置 add_header X-Frame-Options "ALLOW-FROM https://xxx2.xxxxxxx.com"; if ($host ~ ^(xxxx)\.xxxxxxx\.xyz$) { proxy_pass http://0.0.0.0:8083; } if ($host ~ ^(xxxx)\.xxxxxxx\.xyz$) { proxy_pass http://0.0.0.0:8081; } if ($host ~ ^(xxxx)\.xxxxxxx\.xyz$) { proxy_pass http://0.0.0.0:8082; } proxy_pass http://127.0.0.1:1314/; } }
声明 本人博客的所有东西,部分源于网络书籍和视频,其他的是个人的理解感悟,经过自己整理撰写成博客。 本人博客均只用于个人学习、复习,不作为商业用途,如有侵权,请联系我修改或删除。 联系邮箱:15121014713@163.com