更改 Grafana 的标头(访问控制允许原点)



我正在尝试在平原PHP页面中显示一个Grafana仪表板。我遵循网站的说明与Oauth进行认证。这是我的代码:

<?php 
$ch = curl_init();
$authorization = "Authorization: Bearer <myToken>";
curl_setopt_array(
    $ch, array( 
    CURLOPT_URL => 'url-to-my-dashboard',
    CURLOPT_HTTPHEADER => array('Content-Type: application/json' , $authorization),
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPAUTH => "HTTP/1.1"
));
$output = curl_exec($ch);
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <?php echo $output; ?>
    </body>
</html>

页面加载,我得到了CSS ...但是我最终遇到了404错误。我发现Grafana的标题不允许这种动作:

Access to Font at 'http://xxxxx' from origin 'http://localhost'
has been blocked by CORS policy:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost' is therefore not allowed access.

我很确定我需要配置这些标头:

Header set Access-Control-Allow-Origin "xxx"
Header set Access-Control-Allow-Methods "GET, OPTIONS"
Header set Access-Control-Allow-Headers "origin, authorization, accept"

问题是我不知道我会在哪里做。我一直在寻找一个.htaccess的Grafana(或Graphite,我们与之一起使用)。我还尝试修改Apache2 Conf File(/etc/apache2/apache2/conf);重新启动后,什么都没有改变...

我很困惑。谁能帮我?

是由于违反了相同的原始策略的请求,浏览器阻止了请求而不是grafana。对于本地主机,在不同端口上运行的两个网站被认为是两个不同的域。

您需要将Grafana服务器放在反向代理后面以允许交叉原点资源共享(CORS)。

有一个问题描述了这一点,并具有指向文档的链接。

  • 问题:https://github.com/grafana/grafana/issues/5837
  • 文档链接:http://docs.grafana.org/v1.9/installation/#graphite-server-config

从文档(上面的链接)提取描述Apache配置:

apache 2.x:

Header set Access-Control-Allow-Origin "*"
Header set Access-Control-Allow-Methods "GET, OPTIONS"
Header set Access-Control-Allow-Headers "origin, authorization, accept"

请注意,使用" "使您的石墨实例非常开放,所以您 可能要考虑使用" http://my.grafana.com"代替" "

如果您的石墨网络是通过基本身份验证构成的,则必须 启用HTTP动词选项。请注意,使用基本验证 访问控制 - 允许原素不得设置为通配符,也必须 必须指定标头访问控制。这外观 喜欢Apache的以下内容:

Header set Access-Control-Allow-Origin    "http://mygrafana.com:5656"
Header set Access-Control-Allow-Methods   "GET, OPTIONS"
Header set Access-Control-Allow-Headers   "origin, authorization, accept"
Header set Access-Control-Allow-Credentials true
<Location />
    AuthName "graphs restricted"
    AuthType Basic
    AuthUserFile /etc/apache2/htpasswd
    <LimitExcept OPTIONS>
      require valid-user
    </LimitExcept>
</Location>

这是一个问题,描述了用Apache为Grafana创建反向代理的配置:

https://github.com/grafana/grafana/issues/4136

如果这些链接无济于事,则在Grafana Repo中有很多封闭的问题。这是对CORS和APACHE的搜索:

https://github.com/grafana/grafana/issues?utf8=✓&amp;q=ias:issue is IS:closed Cors Cors Apache

最新更新