简体   繁体   English

Grafana 没有“Access-Control-Allow-Origin”标头

[英]No 'Access-Control-Allow-Origin' header for Grafana

I'm trying to setup Grafana on top of nginx.我正在尝试在 nginx 之上设置 Grafana。 Here's how my current setup is.这是我目前的设置。 Grafana is supposed to talk to both graphite and elastic search on the same server. Grafana 应该在同一台服务器上与石墨和弹性搜索进行对话。

Here's my nginx configuration file.这是我的 nginx 配置文件。 I'm not sure what's wrong in this configuration:我不确定这个配置有什么问题:

#graphite server block
server {
 listen                8080 ;
 access_log            /var/log/nginx/graphite.access.log;
 error_log            /var/log/nginx/graphite.error.log;

 location / {

 include uwsgi_params;
 uwsgi_pass 127.0.0.1:3031;
 }
}

#grafana server block
server {
 listen                9400;

 access_log            /var/log/nginx/grafana.access.log;
 error_log            /var/log/nginx/grafana.error.log;

 location / {
auth_basic            "Restricted";
auth_basic_user_file  /etc/nginx/.htpasswd;

    add_header  Access-Control-Allow-Origin 'http://54.123.456.789:9400';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE';
    add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type, origin, accept';
    add_header 'Access-Control-Allow-Credentials' 'true';

root /usr/share/grafana;
 }
}

Now, whenever I try to run Grafana, it gives me the following error:现在,每当我尝试运行 Grafana 时,都会出现以下错误:

XMLHttpRequest cannot load http://54.123.456.789:8080/render. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://54.123.456.789:9400' is therefore not allowed access.

Can someone please help me out in this?有人可以帮我解决这个问题吗? Thanks in advance.提前致谢。

Try putting the four lines of Access-Control-Allow-* in the configuration of the graphite server.尝试将Access-Control-Allow-*这四行放在graphite server的配置中。 To my mind, grafana is asking graphite and that's graphite who has to allow Grafana.在我看来,grafana 要求使用石墨,那是石墨谁必须允许 Grafana。

Ok I wasn't specifically setting up Graphana, but I was intending CORS to work with the auth_basic directive from nginx because such directive overrides any headers that you had before whenever authentication is required (When the server returns a 401 basically)好的,我没有专门设置 Graphana,但我打算让 CORS 与来自 nginx 的auth_basic指令一起使用,因为这样的指令会在需要身份验证时覆盖您之前拥有的任何标头(当服务器基本上返回 401 时)

So after a copule hours of research I found this Gist: https://gist.github.com/oroce/8742704 which is specifically targetted to Graphana and possibly gives a complete answer to this question.因此,经过数小时的研究,我发现了这个 Gist: https ://gist.github.com/oroce/8742704,它专门针对 Graphana,可能对这个问题给出了完整的答案。

BUT for my particular purposes, which again were to combine auth_basic with CORS headers via add_header , my take away from that Gist is the following:但是对于我的特定目的,再次是通过add_headerauth_basic与 CORS 标头结合起来,我从该要点中得出的结论如下:

Your server location should follow a structure like the one below:您的服务器位置应遵循如下结构:

location / {
    proxy_pass <PROXY_PASS_VALUE>;
    
    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;

    # Any additional headers and proxy configuration for the upstream...

    # Remove the CORS Origin header if set by the upstream
    proxy_hide_header 'Access-Control-Allow-Origin';

    # Add our own set of CORS headers
    # The origin specifically, when using ith with authentication CANNOT be set to * as per the spec, it must return 1 and only 1 value so to mimic "*"'s behavior we mirror the origin
    add_header Access-Control-Allow-Origin      $http_origin;
    add_header Access-Control-Allow-Methods  
   'GET,POST,PUT,DELETE,OPTIONS';
    add_header Access-Control-Allow-Headers     'Authorization';
    add_header Access-Control-Allow-Credentials 'true';
        
    if ( $request_method = 'OPTIONS' ) {
        # If request method is options we immediately return with 200 OK
        # If we didn't do this then the headers would be overwritten by the auth_basic directive when Browser pre-flight requests are made
        return 200;
    }

    # This should be set AFTER the headers and the OPTIONS methos are taken care of     
    auth_basic            'Restricted';
    auth_basic_user_file  <HTPASSD_FILE_PATH>;
}

Then when using this from a browser environment, you could issue the following:然后在浏览器环境中使用它时,您可以发出以下命令:

fetch( 
    '<URL>',
    {
        method: 'POST',
        body: <YOUR_BODY_OBJECT>,
        // This must be set for BASIC Auth to work with CORS
        credentials: 'include'
    }
)
    .then( response => response.json() )
    .then( data => {
        console.log( data );
    } );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM