来自不同域的Nginx后面的Grafana在面板上返回“不允许起源”

nkhmeac6  于 2023-01-08  发布在  Nginx
关注(0)|答案(1)|浏览(711)

Grafana(版本9)在域名A上运行时没有代理。我想从另一个域名B添加一个Nginx代理。
根据这个post,Grafana不支持多个域,需要智能代理。
基于on official documentation的第一次发布和this second post,Nginx配置应如下所示

# this is required to proxy Grafana Live WebSocket connections.
map $http_upgrade $connection_upgrade {
  default upgrade;
  '' close;
}

upstream grafana {
  server domainA;
}

server {
  listen 8080;
  server_name domainB;

  location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host domainA;
    proxy_pass https://grafana-prj-sso-monitoring.apps.okd.svc.elca.ch;
  }

  # Proxy Grafana Live WebSocket connections.
  location /api/live/ {
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_set_header Host domainA;
    proxy_pass https://domainA;
  }
}

我可以通过代理到达Grafana,但面板返回“不允许原点”。我试图在两个位置上添加标准CORS标头,但没有成功

add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST';
add_header 'Access-Control-Allow-Headers' 'Authorization, Content-Type';
add_header 'Access-Control-Allow-Credentials' 'true';
mw3dktmi

mw3dktmi1#

最后,这适用于单个位置

location / {
  proxy_set_header Host domainA;
  proxy_set_header Origin https://domainA;
  proxy_pass https://domainA;
}

诀窍是同时更改主机和源标头。

相关问题