查询Elasticsearch时如何解决CORS问题?

时间:2019-02-14 19:04:25

标签: apache docker elasticsearch cors

我正在开发一个软件项目,并且正在使用Elasticsearch支持我的搜索功能。我有一台用于测试的服务器,并且正在使用docker compose创建集群。每当我使用我的应用搜索栏(通过与ES在同一服务器上的Apache托管)时,由于CORS的限制,我永远都无法获得响应(我正在使用Chrome扩展程序来解决此问题,但是我不希望用户拥有安装此文件进行搜索)。

我尝试在elasticsearch.yml文件中启用适当的http设置,但是似乎没有做任何事情。我正在使用Elasticsearch javascript模块发出请求。

docker-compose:

version: '3.6'
services:
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.5.0
    container_name: elasticsearch
    environment:
      - node.name=es01
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      nproc: 65535
      memlock:
        soft: -1
        hard: -1
    cap_add:
      - ALL
    privileged: true
    deploy:
      mode: global
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s
    volumes:
      - esdata1:/usr/share/elasticsearch/data
      - ./elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
    ports:
      - 9200:9200
      - 9300:9300
  elasticsearch2:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.5.0
    container_name: elasticsearch2
    environment:
      - node.name=es02
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - "discovery.zen.ping.unicast.hosts=elasticsearch"
    ulimits:
      nproc: 65535
      memlock:
        soft: -1
        hard: -1
    cap_add:
      - ALL
    privileged: true
    deploy:
      mode: global
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s
    volumes:
      - esdata2:/usr/share/elasticsearch/data
      - ./elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
  elasticsearch3:
    image: docker.elastic.co/elasticsearch/elasticsearch:6.5.0
    container_name: elasticsearch3
    environment:
      - node.name=es03
      - cluster.name=docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
      - "discovery.zen.ping.unicast.hosts=elasticsearch"
    ulimits:
      nproc: 65535
      memlock:
        soft: -1
        hard: -1
    cap_add:
      - ALL
    privileged: true
    deploy:
      mode: global
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s
    volumes:
      - esdata3:/usr/share/elasticsearch/data
      - ./elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
  kibana:
    image: docker.elastic.co/kibana/kibana-oss:6.5.0
    container_name: kibana
    environment:
      SERVER_NAME: localhost
      ELASTICSEARCH_URL: http://elasticsearch:9200/
    ports:
      - 5601:5601
    ulimits:
      nproc: 65535
      memlock:
        soft: -1
        hard: -1
    cap_add:
      - ALL
    deploy:
      mode: global
      restart_policy:
        condition: on-failure
        delay: 5s
        max_attempts: 3
        window: 120s
volumes:
  esdata1:
  esdata2:
  esdata3:

elasticsearch.yml:

http.cors.enabled: true
http.cors.allow-origin: "*"
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"

我如何连接到ES(使用javascript):

var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
  host: 'myServersIP:9200'
});

因此,当我访问服务器网页(www.mydomain.com)时,我得到了应用程序,一切看起来都不错。尝试搜索时,出现COR问题,无法返回任何结果。我认为这可能是因为我通过“ myServersIP:9200”而不是某些代理子域连接到客户端,但是我不确定。关于我可能做错了什么/如何解决此问题的任何想法?

1 个答案:

答案 0 :(得分:0)

尝试在C:\ ProgramData \ Elastic \ Elasticsearch \ config(Windows案例)下进行操作

编辑它并(负责任地)添加CORS配置,因为“ *”非常危险

希望这对您有帮助