文档
指南
启用 CORS

添加 CORS 头部

仅当您直接从浏览器调用 Elasticsearch 且未代理 Elasticsearch 时才需要此操作。在 代理 Elasticsearch 文档中了解更多信息。

为了直接从浏览器调用 Elasticsearch/Opensearch,您需要在响应中添加 CORS 头部。

Elasticsearch

Docker

此 docker run 命令将在运行 Elasticsearch 时添加指定 CORS 头部。

docker run --name elasticsearch --net elastic -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e "xpack.security.enabled=false" -e http.cors.enabled=true -e "http.cors.allow-origin='*'" -e http.cors.allow-headers=X-Requested-With,X-Auth-Token,Content-Type,Content-Length,Authorization -e http.cors.allow-credentials=true -e network.publish_host=localhost -e xpack.security.enabled=false docker.elastic.co/elasticsearch/elasticsearch:8.6.2

Elastic Cloud

  1. 导航到部署页面
  2. 转到操作 > 编辑部署
  3. 在 Elasticsearch 实例上,点击“Elasticsearch 用户设置和扩展”
  4. 将以下内容添加到“Elasticsearch 用户设置”字段
http.cors.allow-origin: "*"
http.cors.enabled: true
http.cors.allow-credentials: true
http.cors.allow-methods: OPTIONS, HEAD, GET, POST, PUT, DELETE
http.cors.allow-headers: X-Requested-With, X-Auth-Token, Content-Type, Content-Length, Authorization, Access-Control-Allow-Headers, Accept, x-elastic-client-meta

elasticsearch.yml

为此,您需要将以下内容添加到您的 elasticsearch.yml 文件中

http.cors.allow-origin: "*"
http.cors.enabled: true
http.cors.allow-credentials: true
http.cors.allow-methods: OPTIONS, HEAD, GET, POST, PUT, DELETE
http.cors.allow-headers: X-Requested-With, X-Auth-Token, Content-Type, Content-Length, Authorization, Access-Control-Allow-Headers, Accept, x-elastic-client-meta

Opensearch

Docker

此 docker run 命令将在运行 Opensearch 时添加指定 CORS 头部。

docker run --name opensearch --rm -d -p 9200:9200 -e http.port=9200 -e discovery.type=single-node -e http.max_content_length=10MB -e http.cors.enabled=true -e "http.cors.allow-origin='*'" -e http.cors.allow-headers=X-Requested-With,X-Auth-Token,Content-Type,Content-Length,Authorization -e http.cors.allow-credentials=true opensearchproject/opensearch:1.2.4

Apache 2.0 2024 © Joseph McElroy.
需要帮助?加入 Discord