【问题标题】:elasticsearch: Cross-Origin Request Blocked despite configuring http moduleelasticsearch:尽管配置了 http 模块,但跨域请求被阻止
【发布时间】:2016-08-22 18:51:58
【问题描述】:

我使用的是 2.3.2 版的 elasticsearch。在我的 elasticsearch.yml 文件中,我添加了以下几行以允许跨域请求。

http.cors.enabled: true
http.cors.allow-credentials: true
http.cors.allow-methods: OPTIONS,HEAD,GET,POST,PUT,DELETE
http.cors.max-age: 0
http.cors.allow-origin: /http?:\/\/localhost(:[0-9]+)?/
http.cors.allow-headers : X-Requested-With,X-Auth-Token,Content-Type,Content-Length

但是,当我尝试从 Firefox 执行查询时,出现以下错误;

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote 
resource at http://localhost:9200/someIndex/_search?size=10&from=0. 
(Reason: CORS header 'Access-Control-Allow-Origin' missing).

"*" 替换http.cors.allow-origin 参数似乎可行,但文档表明这是一个安全请求。

来自浏览器的请求标头如下;

Accept - application/json, text/plain, */*
Accept-Encoding - gzip, deflate
Accept-Language - en-US,en;q=0.5
Content-Length - 26
Content-Type - application/json;charset=utf-8
DNT - 1
Host - localhost:9200
Origin - null
User-Agent - Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:44.0) Gecko/20100101 Firefox/44.0

有人可以建议我在上面做错了什么吗?

【问题讨论】:

  • 你能试着用双引号写吗?
  • 如果添加双引号,elasticsearch 将无法启动。

标签: elasticsearch cors


【解决方案1】:

您似乎有一个小错字。

应该是

http.cors.allow-origin: /https?:\/\/localhost(:[0-9]+)?/
                             ^
                             |
                          add this

即您在? 之前缺少s,这意味着“http 或https”

【讨论】:

  • 嗨 Val - 实际上我已经尝试过了 - 不幸的是,似乎没有任何改变。
  • 我怀疑查询不是来自本地主机,但可能是 127.0.0.1,这可能吗?发送查询时,您能否检查您在 Firefox 开发人员工具中看到的内容?请求者使用什么主机?
  • 添加了上面的 reqeuste 标头 - 似乎正在使用 localhost。也尝试在 yml 文件中使用 127.0.0.1 ,但也不好。
  • 我看到Origin - null,这就是我认为它不起作用的原因。 Host 是目标主机(即你的 ES)。您可能正在从本地文件系统加载 HTML 页面,对吗?见stackoverflow.com/questions/8456538/…
猜你喜欢
  • 1970-01-01
  • 2020-07-09
  • 2019-10-11
  • 2023-03-03
  • 2018-09-09
  • 2021-06-26
  • 2014-10-13
相关资源
最近更新 更多