【问题标题】:unable to configure grafana with graphite无法使用石墨配置 grafana
【发布时间】:2014-04-29 20:13:51
【问题描述】:

我正在使用 Nginx 来提供石墨和 grafana(它们都在同一台服务器上运行 - 而不是我的桌面)。我可以通过 Nginx 访问石墨。但是,grafana 似乎无法连接到石墨(错误:石墨 HTTP 请求错误)。我已经为 grafana 复制了下面的 nginx 配置 - 任何有关解决此问题的想法将不胜感激。 在浏览器中失败的请求 URL 是这样的(如果我直接在浏览器中访问就可以访问):

**http://xxx.xxx.xxx.xxx:8080/render**

Nginx 默认

server { 
        listen 85;  ##listen [::]:85; #ipv6only=on;
        server_name grafana;
        root /home/xxxx/grafana-1.5.3/;
        index index.html index.htm;
        ##logging per server
        access_log /var/log/nginx/grafana/access.log;
        error_log /var/log/nginx/grafana/error.log;

       location / {
       ##  root /home/xxxx/grafana-1.5.3/;
       }
}

石墨的 config.js 网址(在 grafana 中)

graphiteUrl: "http://xxx.xxx.xxx.xxx:8080"

编辑 Graphite 不需要身份验证即可从 grafana 访问。另外,我正在使用 grafana v1.5.3

【问题讨论】:

  • 我也有这个问题。你有没有找到解决这个问题的方法?
  • @jmreicha 偏离了方向 - 将在本周晚些时候通过下面提供的答案进行调查,并在此处发布更新。我认为所有三个答案都为解决这个问题提供了有用的信息。

标签: nginx graphite


【解决方案1】:

尝试通过nginx(同源)访问graphite。只需添加新位置

location /render {
                proxy_pass      http://xxx.xxx.xxx.xxx:8080/render;
}

然后在你的 grafana 配置文件中更改石墨 url

【讨论】:

  • 我在 grafana 的 nginx 默认 conf 文件的服务器部分添加了“/render” - 获取请求仍然失败。
  • 从 app.js(渲染调用)对石墨 URL(ON PORT 8080)的 post 调用失败。
【解决方案2】:

尝试运行带有“disable-web-security”标志的浏览器。

【讨论】:

  • 在当前版本的 phantomjs 中,我不得不使用 --web-security=false 来完成这项工作。
【解决方案3】:

我能够通过将请求更改为 GET 而不是 POST 来解决这个问题。有关详细信息,请参阅此问题。 https://github.com/grafana/grafana/issues/345

我的数据源最终看起来像

datasources: {
  graphite: {
    type: 'graphite',
    url: window.location.protocol+"//"+window.location.hostname+":"+window.location.port+"/_graphite",
    default: true,
    render_method: 'GET'  
  },
},

我还没有弄清楚如何让我的石墨安装接受 POST 请求。即使直接查询,我也可以确定 CORS 不是问题。

【讨论】:

    【解决方案4】:

    我认为您需要在 graphite 的 nginx 配置中启用 CORS。看看:http://enable-cors.org/server_nginx.html。这是我使用此链接所做的配置:

    (在我的例子中,grafana 暴露在端口8100,石墨暴露在端口8090;相应地适应(8100 -> 858090 -> 8080))。

    upstream django {
        # Distribute requests to servers based on client IP. This keeps load
        # balancing fair but consistent per-client. In this instance we're
        # only using one uWGSI worker anyway.
        ip_hash;
        server unix:/tmp/uwsgi.sock;
    }
    
    server {
       listen      yourServerIp:8090;
       server_name yourServerName.com;
       access_log      /var/log/nginx/graphite_access.log;
       error_log       /var/log/nginx/graphite_error.log;
       charset     utf-8;
    
    
       # Django admin media.
       location /media/admin/ {
          alias /usr/lib/python2.7/dist-packages/django/contrib/admin/media/;
       }
    
       # Static media.
       location /content/ {
          alias /opt/graphite/webapp/content/;
       }
    
       # Send all non-media requests to the Django server.
       location / {
    
        # CORS (for grafana)
    
        if ($http_origin ~* "^http://yourServerName.com:8100$") {
         set $cors "true";
        }
    
        if ($request_method = 'OPTIONS') {
         set $cors "${cors}options";  
        }
    
        if ($request_method = 'GET') {
         set $cors "${cors}get";  
        }
    
        if ($request_method = 'POST') {
         set $cors "${cors}post";
        }
    
        if ($cors = "trueoptions") {
         add_header 'Access-Control-Allow-Origin' "$http_origin";
         add_header 'Access-Control-Allow-Credentials' 'true';
         add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
         add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
         add_header 'Access-Control-Max-Age' 1728000;
         add_header 'Content-Type' 'text/plain charset=UTF-8';
         add_header 'Content-Length' 0;
    
         return 204;
        }
    
        if ($cors = "truepost") {
         add_header 'Access-Control-Allow-Origin' "$http_origin";
         add_header 'Access-Control-Allow-Credentials' 'true';
         add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
         add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }
    
        if ($cors = "trueget") {
         add_header 'Access-Control-Allow-Origin' "$http_origin";
         add_header 'Access-Control-Allow-Credentials' 'true';
         add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
         add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        }
    
         uwsgi_pass  django;
         include     uwsgi_params;
       }
    }
    

    请注意,对你来说有趣的部分是 # CORS 下面的内容,django 的东西可能对你没用。

    为确保这是一个 CORS 问题,您需要检查浏览器发送的 HTTP 标头;如果有 Origin 标头,则意味着您必须使用 CORS。

    【讨论】:

    • 感谢分享 - 我试过了。我想我可能需要检查一下这个盒子上的石墨是如何配置的。 xxx.xxx.xxx.xxx:8080/render 仍然失败。很可能,我需要检查一下我是如何错误配置石墨的。
    • 您能否让我们快速了解一下 http 请求和响应标头的外观?它可能会有所帮助
    • 这是一个 CORS 问题 - 由于跨域问题,对石墨的发布请求被拒绝。
    • 这个问题是 cors 问题(chrome 和 firefox 确认了这一点)
    【解决方案5】:

    如果您无法编辑 NGinx 配置,要尝试的另一件事是使用 CORS 代理。 我使用 NPM 包 corsproxy 来解决石墨 CORS 问题。

    安装 corsproxy 包:

     mkdir graphiteProxy
     cd graphiteProxy
     npm install corsproxy
     echo "copy the version of http_proxy corsproxy depends on into"
     echo "your local node_modules"
     cp -r node_modules\corsproxy\node_modules\http-proxy node_modules\http_proxy
     touch app.js
    

    app.js:

    // point the grafana config.js to your local proxy: http://localhost:8081
    var cors_proxy = require('corsproxy')
    var http_proxy = require('http-proxy')
    cors_proxy.options = {
       target : "http://{{graphiteserver}}:8080"
    }
    http_proxy.createServer(cors_proxy).listen(8081)
    

    运行代理:

    node app.js
    

    【讨论】:

      【解决方案6】:

      这是我用来托管 grafana 和代理石墨和 elasticsearch 的 nginx 配置文件。

       server {
          listen 81 default_server;
          server_name _;
          location / {
            root /src/grafana;
            index index.html;
          }
          location /graphite/ {
              proxy_pass                 http://127.0.0.1:8000/;
              proxy_set_header           X-Real-IP   $remote_addr;
              proxy_set_header           X-Forwarded-For  $proxy_add_x_forwarded_for;
              proxy_set_header           X-Forwarded-Proto  $scheme;
              proxy_set_header           X-Forwarded-Server  $host;
              proxy_set_header           X-Forwarded-Host  $host;
              proxy_set_header           Host  $host;
      
              client_max_body_size       10m;
              client_body_buffer_size    128k;
      
              proxy_connect_timeout      90;
              proxy_send_timeout         90;
              proxy_read_timeout         90;
      
              proxy_buffer_size          4k;
              proxy_buffers              4 32k;
              proxy_busy_buffers_size    64k;
              proxy_temp_file_write_size 64k;
          }
      
          location /elasticsearch/ {
              proxy_pass                 http://127.0.0.1:9200/;
              proxy_set_header           X-Real-IP   $remote_addr;
              proxy_set_header           X-Forwarded-For  $proxy_add_x_forwarded_for;
              proxy_set_header           X-Forwarded-Proto  $scheme;
              proxy_set_header           X-Forwarded-Server  $host;
              proxy_set_header           X-Forwarded-Host  $host;
              proxy_set_header           Host  $host;
      
              client_max_body_size       10m;
              client_body_buffer_size    128k;
      
              proxy_connect_timeout      90;
              proxy_send_timeout         90;
              proxy_read_timeout         90;
      
              proxy_buffer_size          4k;
              proxy_buffers              4 32k;
              proxy_busy_buffers_size    64k;
              proxy_temp_file_write_size 64k;
          }
        }
      

      【讨论】:

      • 当我回到那个安装时我肯定会这样做 - 我之前无法查看安装。非常感谢
      • 根据我的设置,仅使用位置设置下的根和索引选项不起作用。我收到一个 HTTP 请求错误 frmo app.js(在渲染调用中)。 xxx.xxx.xxx.xxx:8080/render(8080 是运行 grafite 的地方)。我是否需要更改 grafana 或石墨中的任何配置才能使其正常工作?
      • 问题(我认为)是我有一个用于访问其上的应用程序(包括石墨和grafana)的服务器的域名。当我直接尝试使用 URL 时,我现有的设置有效(当我有机会时,我将尝试 Colin 提到的 CORS 选项)。再次感谢分享
      • 我试过这个(没有弹性搜索块)并得到“错误无法加载仪表板/default.json。请确保它存在”。我不确定您的 root 指向哪个目录,我想知道这是否是问题所在。
      【解决方案7】:

      我不确定 OP 是否已经解决了他们的问题,但这对我有用:

      我把石墨和格拉法纳放在同一个网站上。 Graphite 存在于 /grafana/ 内的根和 grafana 中

      这解决了跨站点脚本问题,而无需设置 CORS:graphite 和 grafana 在同一个站点上。

      nginx站点配置:

      upstream graphite {
          server unix:///var/uwsgi/graphite-web.sock;
      }
      
      server {
          listen 8080;
          listen [::]:8080 ipv6only=on;
      
          root /usr/share/graphite-web/;
      
          server_name localhost;
      
          location /static {
                  alias /usr/share/graphite-web/static;
          }
      
          location /grafana {
                  alias /usr/share/graphite-web/grafana;
          }
      
      
          location / {
                  uwsgi_pass graphite;
                  include /etc/nginx/uwsgi_params;
          }
      }
      

      要访问 grafana,我因此转到:

      http://192.168.1.1:8080/grafana
      

      (192.168.1.1是我服务器的ip地址)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-26
        • 2014-11-08
        • 1970-01-01
        • 2018-10-04
        相关资源
        最近更新 更多