【问题标题】:Allowing my site to be embedded on a iFrame允许我的网站嵌入 iFrame
【发布时间】:2022-01-21 20:00:35
【问题描述】:

我的 Nginx 服务器将 X-Frame 标头设置为 DENY,这到目前为止很好。但现在我只需要允许将我网站的一个页面嵌入到我的域之外的 iframe 中。

我尝试在为网页提供服务的控制器中使用 php 在应用程序级别解决此问题:

header('X-Frame-Options: ALLOW-FROM 127.0.0.1');

但是当我尝试将页面嵌入外部 iframe 时的响应是:

X-Frame-Options:ALLOW-FROM 127.0.0.1
X-Frame-Options:DENY

那么是连接选项(一组 nginx 配置与一组应用程序代码)吗?那么,如何让我的服务器的单个页面嵌入到外部 iframe 中?

【问题讨论】:

  • 你在 nginx 服务器 add_header 指令中有什么东西可以用于 x-frame-options 吗?如果是这样,请尝试将其注释掉。 PHP 应该替换 x-frame-options,除非您在 header() 的第二个参数中指定 false
  • @HalfCrazed 这将禁用我网站其余部分的 x-frame,我需要一个解决方案来阻止我所有页面中的 iframe,但一个

标签: php nginx


【解决方案1】:

为什么不写一个简单的 if else 语句呢?如果 PHP 检测到某个页面(URL),则允许嵌入到 iFrame 中,否则不允许嵌入到 iFrame 中。

【讨论】:

    【解决方案2】:

    对于 Django,我们是这样解决的

    location /the/page/you/want/to/expose/ {
        add_header Access-Control-Allow-Origin *;
        include        uwsgi_params;
        uwsgi_pass     unix:/run/uwsgi/app/appsco/socket;
    }
    
    location / {
        add_header X-Frame-Options DENY;  #This is your catch all.
        include        uwsgi_params;
        uwsgi_pass     unix:/run/uwsgi/app/appsco/socket;
    }
    

    请记住将 add_header 删除到整个服务器。把它放在你的全部捕获中。对于 Django 用户@frame_deny_exempt 看看http://django-secure.readthedocs.org/en/latest/middleware.html

    【讨论】:

      【解决方案3】:

      我认为您需要删除X-Frame-Options:DENY,明确声明允许似乎默认阻止其他所有内容。我只使用 URL 而不是 IP 地址对此进行了测试,但是来自https://example.com 的 x 框架被阻止,而 test.mysite-b.com 可以嵌入 test.mysite-b.com 和使用相同的虚拟主机config mysite-b.com 也被阻止嵌入 test.mysite-b.com。

      add_header X-Frame-Options "ALLOW-FROM http://test.mysite-b.com";
      

      这允许 test.mysite-b.com 嵌入任何使用此配置的站点,包括它自己。其他网站仍然被拒绝,并且无法嵌入 test.mysite-b.com 或其他 URL。

      作为参考,这是我的整个 SSL 配置:

      ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
      ssl_prefer_server_ciphers on;
      ssl_ciphers EECDH+ECDSA+AESGCM:EECDH+aRSA+AESGCM:EECDH+ECDSA+SHA512:EECDH+ECDSA+SHA384:EECDH+ECDSA+SHA256:ECDH+AESGCM:ECDH+AES256:DH+AESGCM:DH+AES256:RSA+AESGCM:!aNULL:!eNULL:!LOW:!RC4:!3DES:!MD5:!EXP:!PSK:!SRP$
      ssl_ecdh_curve secp384r1;
      ssl_session_cache shared:SSL:10m;
      ssl_session_tickets off;
      ssl_stapling on;
      ssl_stapling_verify on;
      resolver 37.235.1.174 37.235.1.177 valid=300s;
      resolver_timeout 5s;
      add_header Strict-Transport-Security 'max-age=31536000; includeSubDomains; preload' always;
      add_header X-Frame-Options "ALLOW-FROM http://test.mysite-b.com";
      add_header X-Content-Type-Options nosniff;
      ssl_dhparam /etc/ssl/certs/dhparam.pem;
      

      一个潜在的问题我认为您的允许 URL 列表是可访问的,所以如果您希望它们由于某种原因被隐藏......它们不会。

      Load denied by X-Frame-Options: https://test.mysite-b.com/ does not permit framing by https://mysite-b.com/iframe.html.
      

      【讨论】:

        猜你喜欢
        • 2011-08-25
        • 2011-11-17
        • 2017-01-21
        • 1970-01-01
        • 2021-07-08
        • 1970-01-01
        • 1970-01-01
        • 2021-06-07
        • 1970-01-01
        相关资源
        最近更新 更多