【问题标题】:Elasticsearch https cors enabled but still getting No 'Access-Control-Allow-Origin' header is present on the requested resourceElasticsearch https cors 已启用,但仍然获得 No 'Access-Control-Allow-Origin' header is present on the requested resource
【发布时间】:2016-08-12 02:09:02
【问题描述】:

我在名为 myIndex 的索引中启用了 cors 的设置,这里是它的设置。但是当我尝试使用一些设置了 cors 标头的简单 Javascript 从 elasticsearch 中提取数据时,我得到了错误

XMLHttpRequest 无法加载 http://elasticSearchDomain.com:9200/myIndex/_search/。对预检请求的响应未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin 'http://localhost:8000' 因此不允许访问。

我正在运行 Javascript,方法是将其包含在我通过 python 服务器打开 html 页面时运行的脚本中。

{
      "state": "open",
      "settings": {
        "index": {
          "http": {
            "cors": {
              "allow-credentials": "true",
              "enabled": "true",
              "allow-origin": "*"
            }
          },
          "creation_date": "1461087830891",
          "number_of_shards": "5",
          "number_of_replicas": "1",
          "version": {
            "created": "1070499"
          },
          "uuid": "2JeIgB7IRs6_DzEb6PLx-w"
        }
      },
      "mappings": {
        "tx": {
          "properties": {
            "next": {
              "format": "dateOptionalTime",
              "type": "date"
            },
            "eid": {
              "index": "not_analyzed",
              "type": "string"
            },
            "95percent_time": {
              "type": "double"
            },
            "total_count": {
              "type": "long"
            },
            "failure_count": {
              "type": "long"
            },
            "pool_id": {
              "type": "long"
            },
            "pool_name": {
              "index": "not_analyzed",
              "type": "string"
            },
            "failure_rate": {
              "type": "double"
            },
            "report_time": {
              "format": "dateOptionalTime",
              "type": "date"
            },
            "txn_type": {
              "index": "not_analyzed",
              "type": "string"
            },
            "status_default": {
              "type": "long"
            },
            "txn_name": {
              "index": "not_analyzed",
              "type": "string"
            },
            "frequency_type": {
              "index": "not_analyzed",
              "type": "string"
            },
            "status_2": {
              "type": "long"
            },
            "status_1": {
              "type": "long"
            },
            "avg_time": {
              "type": "double"
            },
            "datacenter_id": {
              "type": "long"
            },
            "datacenter_name": {
              "index": "not_analyzed",
              "type": "string"
            },
            "server_type": {
              "type": "string"
            }
          }
        }
      },
      "aliases": [

      ]
    }

这是我试图用来从 elasticsearch 中提取数据的 Javascript

var url = "http://elasticSearchDomain.com:9200/myIndex/_search/";
var method = "POST";
var postData = '{"query": { "filtered": { "query": { "query_string": { "query": "*", "analyze_wildcard": true } }, "filter": { "bool": { "must": [ { "query": { "query_string": { "query": "*", "analyze_wildcard": true } } }, { "range": { "report_time": { "gte": 1458409392443, "lte": 1461001392443 } } } ], "must_not": [] } } } }, "size": 0, "aggs": { "2": { "date_histogram": { "field": "report_time", "interval": "12h", "pre_zone": "-07:00", "pre_zone_adjust_large_interval": true, "min_doc_count": 1, "extended_bounds": { "min": 1458409392443, "max": 1461001392443 } }, "aggs": { "3": { "terms": { "field": "pool_name", "size": 20, "order": { "_count": "desc" } } } } } } }';

// You REALLY want async = true.
// Otherwise, it'll block ALL execution waiting for server response.
var async = true;

var request = new XMLHttpRequest();

// specifies how the HTTP response will be handled. 
request.onload = function () {

   // You can get all kinds of information about the HTTP response.
   var status = request.status; // HTTP response status, e.g., 200 for "200 OK"
   var data = request.responseText; // Returned data, e.g., an HTML document.
}

request.open(method, url, async);
request.setRequestHeader('Access-Control-Allow-Headers', '*');
request.setRequestHeader('Access-Control-Allow-Origin', '*');
request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");

// Actually sends the request to the server.
request.send(postData);

【问题讨论】:

    标签: javascript http elasticsearch cors


    【解决方案1】:

    为了某些特殊性(以及任何对正则表达式有问题的人),我不得不在 elasticsearch.yml 中添加这些配置:

    http.cors.enabled: true
    http.cors.allow-origin: /https?:\/\/(localhost)?(127.0.0.1)?(:[0-9]+)?/
    

    不是完美的正则表达式,但我的搜索应用现在可以工作了。

    【讨论】:

    • 兄弟我只想允许两个链接进入“允许来源”。你能告诉我该怎么做吗?
    【解决方案2】:

    这些不是特定于索引的设置。

    cors 设置应该放在 ES 集群中每个节点的 elasticsearch.yml 配置文件中,然后集群重新启动。请参阅here 了解更多信息。

    【讨论】:

    • 你不懂。存储索引的 ES 节点需要拥有它,而不是索引本身。
    • 虽然我没有使用 elasticsearch.yml 配置文件创建我的索引...我正在做一个简单的 curl -XPUT "elasticSerarchDomain:9200/myIndex" -d' { "settings": { "index" : { "http": { "cors": { .....
    • 我建议您阅读一些有关 Elasticsearch 的文档,因为您缺少对该产品的一些基本了解。
    • 我想你是说 cors 级别的配置是为elasticSearchDomain.com:9200 设置的,而不是我的具体索引。正确的?抱歉,我正在使用某人的 ES 集群,而不是我自己创建的。
    • 是的,完全正确。您需要在其他人的集群中使用它。
    猜你喜欢
    • 2021-04-28
    • 2013-12-11
    • 1970-01-01
    • 2019-01-18
    • 2016-07-10
    • 1970-01-01
    • 2022-08-08
    • 2017-12-01
    • 2016-08-30
    相关资源
    最近更新 更多