更改Grafana的标题(Access-Control-Allow-Origin) [英] Change Grafana's headers (Access-Control-Allow-Origin)

查看:3394
本文介绍了更改Grafana的标题(Access-Control-Allow-Origin)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在普通的PHP页面中显示Grafana仪表板。我按照网站上的说明用oauth进行身份验证。这是我的代码:

I'm trying to display a Grafana dashboard within a plain PHP page. I followed the website instructions to authentiate with oauth. Here is my code:

<?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的标题不允许这种行为:

The page loads, I get the CSS... but I end up with a 404 error. I discover that Grafana's headers do not allow this kind of actions:

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.

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

I'm quite sure I need to configure these headers:

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

问题是我不知道我知道我会在哪里做。我一直在寻找Grafana(或Graphite,我们用它)的.htaccess文件。我也尝试修改Apache2 conf文件(/ etc / apache2 / apache2 / conf);重新启动后,没有任何变化...

The problem is I don't know where I shall do it. I've been looking for an .htaccess file for Grafana (or Graphite, which we use with it). I've also tried modifying the Apache2 conf file (/etc/apache2/apache2/conf); after restart, nothing changes...

我很困难。任何人都可以帮助我吗?

I'm quite stuck. Can anyone help me?

推荐答案

由于违反同源政策。对于localhost,在不同端口上运行的两个网站被认为是两个不同的域。

It is the browser that is blocking the requests and not Grafana due to the requests violating the Same Origin Policy. For localhost, two websites running on different ports are considered to be two different domains.

您需要将Grafana服务器置于反向代理之后以允许跨源资源共享(CORS)。

You will need to put your Grafana server behind a reverse proxy to allow Cross Origin resource sharing (CORS).

有一个问题通过文档链接来描述。

There is an issue describing this with a link to the docs.

  • Issue: https://github.com/grafana/grafana/issues/5837
  • Docs link: http://docs.grafana.org/v1.9/installation/#graphite-server-config

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

Extract from the docs (link above) describing the Apache config:


对于Apache 2.x:

For 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"

请注意,使用会使您的石墨实例非常开放你b $ b可能想考虑使用 http://my.grafana.com 代替

Note that using "" leaves your graphite instance quite open so you might want to consider using "http://my.grafana.com" in place of ""

如果您的Graphite网站受到基本身份验证的保护,您必须启用HTTP动词选项
。请注意,使用基本auth
时,不能将Access-Control-Allow-Origin设置为通配符,还必须指定
标头Access-Control-Allow-Credentials。对于Apache,这看起来像
,如下所示:

If your Graphite web is proteced by basic authentication, you have to enable the HTTP verb OPTIONS. Take note that when using basic auth Access-Control-Allow-Origin must not be set to a wildcard, also the header Access-Control-Allow-Credentials must be specified. This looks like the following for 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:

And here is an issue that describes the config for creating a reverse proxy for Grafana with Apache:

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

如果这些链接没有帮助,那么关于这一点的问题就很多了Grafana回购。这是搜索cors和apache:

If those links do not help, there are a lot of closed issues about this in the Grafana repo. Here is a search for cors and apache:

https://github.com/grafana/grafana/issues?utf8=%E2%9C%93&q=is% 3Aissue%20is%3Aclosed%20cors%20apache

这篇关于更改Grafana的标题(Access-Control-Allow-Origin)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆