无法用石墨配置grafana

时间:2014-04-29 20:13:51

标签: nginx graphite

我正在使用Nginx来提供石墨和grafana(它们都在同一台服务器上运行 - 而不是我的桌面)。我可以通过Nginx访问石墨。但是,grafana似乎无法连接到石墨(错误:Graphite HTTP请求错误)。我已经复制下面的nginx配置为grafana - 任何有关修复此问题的想法将不胜感激。 在浏览器中失败的请求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石墨网址(格拉法纳)

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

修改 Graphite不需要身份验证来访问grafana。另外,我使用的是grafana v1.5.3

7 个答案:

答案 0 :(得分:4)

尝试通过nginx(相同的原点)访问石墨。只需添加新位置

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

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

答案 1 :(得分:3)

尝试运行浏览器whith" disable-web-security"标志。

答案 2 :(得分: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不是问题。

答案 3 :(得分:2)

我认为您需要在石墨的nginx配置中启用CORS。请查看:http://enable-cors.org/server_nginx.html。这是我使用此链接进行的配置:

(在我的情况下,grafana在端口8100上公开,而石墨在端口8090上公开;相应地进行调整(8100 - > 85,{{1} } - > 8090))。

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; } } 以下的内容,django对你来说可能毫无用处。

要确保它是CORS问题,您需要检查浏览器发送的HTTP标头;如果有# CORS标题,则表示您必须使用CORS。

答案 4 :(得分:1)

如果你不能编辑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

答案 5 :(得分:1)

这是我用来托管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;
    }
  }

答案 6 :(得分:1)

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

我把石墨和grafana放在同一个地方。石墨存在于/ 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地址)