New

The executive guide to generative AI

Read more

为行为分析设置 CORS

编辑

行为分析将事件直接发送到 Elasticsearch API。这意味着浏览器直接向 Elasticsearch API 发出请求。Elasticsearch 支持 跨域资源共享 (CORS),但此功能默认处于禁用状态。因此,浏览器将阻止这些请求。

有两种方法可以解决此问题

在 Elasticsearch 上启用 CORS

编辑

这是最简单的选项。通过将以下内容添加到您的 elasticsearch.yml 文件中,在 Elasticsearch 上启用 CORS

http.cors.allow-origin: "*" # Only use unrestricted value for local development
# Use a specific origin value in production, like `http.cors.allow-origin: "https://<my-website-domain.example>"`
http.cors.enabled: true
http.cors.allow-credentials: true
http.cors.allow-methods: OPTIONS, POST
http.cors.allow-headers: X-Requested-With, X-Auth-Token, Content-Type, Content-Length, Authorization, Access-Control-Allow-Headers, Accept

在 Elastic Cloud 上,您可以通过编辑您的 Elasticsearch 用户设置来完成此操作。

  1. 从您的部署菜单中,转到 编辑 页面。
  2. Elasticsearch 部分中,选择 管理用户设置和扩展
  3. 使用上述配置更新用户设置。
  4. 选择 保存更改

通过支持 CORS 的服务器代理请求

编辑

如果您无法在 Elasticsearch 上启用 CORS,则可以通过支持 CORS 的服务器代理请求。 这比较复杂,但也是一个可行的选择。

Was this helpful?
Feedback