我认为您需要在 graphite 的 nginx 配置中启用 CORS。看看:http://enable-cors.org/server_nginx.html。这是我使用此链接所做的配置:
(在我的例子中,grafana 暴露在端口8100,石墨暴露在端口8090;相应地适应(8100 -> 85,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;
}
}
请注意,对你来说有趣的部分是 # CORS 下面的内容,django 的东西可能对你没用。
为确保这是一个 CORS 问题,您需要检查浏览器发送的 HTTP 标头;如果有 Origin 标头,则意味着您必须使用 CORS。