微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

nginx 配置 X-Frame-Options

假如在域名b.com下,有一个html页面test.html,访问路径为:http://b.com/test.html;如果要防止别人在iframe下访问该页面,则可以通过Nginx配置实现。

举例如下:

现有页面a.html,http://a.com/a.html,该页面一个iframe,src=http://b.com/test.html

<!DOCTYPE html>
<html lang="en">
<head>
    <Meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <h1 id="parent">引用页面</h1>
    <iframe id="myIframe" src="http://b.com/test.html"></iframe>
</body>
</html>

被引用页面http://b.com/test.html内容为:

<!DOCTYPE html>
<html lang="en">
<head>
    <Meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <h2 id="被引用页面">test</h2>
</body>
</html>

 

配置过程:

1、打开Nginx配置文件,在server、http或location的代码块里加入以下代码

      1)add_header x-frame-options DENY; 

          此时访问http://a.com/a.html,则iframe提示:http://b.com/test.html拒绝了我们的连接请求,控制台报错:

         Refused to display 'http://b.com/test.html' in a frame because it set 'x-frame-options' to 'deny'.

      2)add_header x-frame-options SAMEORIGIN; 

          此时访问http://a.com/a.html,则iframe提示:http://b.com/test.html拒绝了我们的连接请求,控制台报错:

         Refused to display 'http://b.com/test.html' in a frame because it set 'x-frame-options' to 'sameorigin'.

2、重启Nginx

如果设置 add_header x-frame-options ALLWOALL; 或者不配置x-frame-options,则http://b.com/test.html是可以在iframe中被引用的

试图通过指定域名可以访问,即设置:add_header x-frame-options "ALLOW-FROM http://a.com",但报错如下:

Invalid 'x-frame-options' header encountered when loading 'http://b.com/test.html': 'ALLOW-FROM http://a.com' is not a recognized directive. The header will be ignored.

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐