我正在尝试从 Grafana Cloud 进行 API 查询。
我有这个 JavaScript 文件:
const url = new URL('/<apache proxy>' + '/loki/api/v1/labels', window.location.origin);
const user = '<user>';
const apikey = '<apikey>';
fetch(url.toString(), {
method: 'POST',
headers: {
'Authorization': 'Basic ' + btoa(user + ':' + apikey)
},
})
.then((response) => console.log(response));
(代理是为了解决 CORS 错误。)
它给了我这个错误:
Response {
body: (...)
bodyUsed: false
headers: Headers {}
ok: false
redirected: false
status: 500
statusText: "Internal Server Error"
type: "basic"
url: "http://<server>/<proxy>/loki/api/v1/labels"
[[Prototype]]: Response
}
我该如何解决这个问题?
我最终通过更改 Apache 反向代理文件解决了这个问题:
<VirtualHost *:80>
ProxyPreserveHost on
SSLProxyEngine on
SSLProxyVerify none
SSLProxyCheckPeerCN off
SSLProxyCheckPeerName off
SSLProxyCheckPeerExpire off
<Location /<proxy> >
ProxyPass <cloud url>
ProxyPassReverse <cloud url>
RequestHeader set Authorization "Basic <encoded code>"
# encoded code from: btoa('<user>:<apikey>')
</Location>
</VirtualHost>
Authorization
也可以在JavaScript文件中设置,但在代理中设置它意味着它不可见,因此更安全。