支持CORS的Dockerized Elasticsearch

1aaf6o9v  于 5个月前  发布在  ElasticSearch
关注(0)|答案(1)|浏览(57)

我尝试设置一个本地Elasticsearch示例用于开发目的。因此,我从一个基本的docker-compose.yml文件开始:

services:

  elasticsearch:
    image: elasticsearch:8.7.1
    ports:
      - 9200:9200
    environment:
      discovery.type: single-node # Runs as a single-node
      ES_JAVA_OPTS: '-Xms256m -Xmx256m'
      network.bind_host: 0.0.0.0
      xpack.security.enabled: 'false'
    volumes: # Stores elasticsearch data locally on the esdata Docker volume
      - esdata:/usr/share/elasticsearch/data
    networks:
      - internal
      - elastic

# Define the Docker volume named esdata for the Elasticsearch container.
volumes:
  esdata:
# Networks to use
networks:
  elastic:
    external: true
  internal:
    driver: bridge

字符串
这工作正常,为了测试使用Elasticsearch的WordPress插件。然而,现在我需要避免CORS相关的错误,所以我尝试向环境添加参数:

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, Access-Control-Allow-Headers, Accept
  http.cors.allow-credentials: 'true'


这不起作用。Elasticsearch在应用更改后拒绝连接。
所以,我需要的是最低限度的安全性,但同时允许来自任何地方的CORS请求。
您的建议将不胜感激。

taor4pac

taor4pac1#

你会得到CORS错误,因为Elasticpress的Autossuggest功能是一个前端概念,后端URL与前端不匹配。
你真的应该保持你的Elasticsearch集群完全私有,并保持与它的通信仅限于网络请求。
也就是说,由于Autossuggest需要使用面向公众的端点,我们已经通过以下方式使用Nginx扩展端点来解决这个问题:

location /es-search {
    limit_except POST {
        deny all;
    }
    rewrite ^/es-search <desired-es-index-here>/_search break;
    proxy_set_header Host $host;
    # Only enable if you're using Authentication with your ES Cluster
    # proxy_set_header Authorization "Basic <Base64EncodedPasswordHere>";
    proxy_pass https://elasticsearch:9200; # Whatever your internal DNS name or IP address is for your ES Cluster goes here
}

字符串
通过这种方式,我们公开了一个/es-search端点供公众使用,并允许服务器使用Elasticsearch本身进行身份验证。这样,所有请求都变成了内部请求,并且不需要修改CORS设置。
您可以随意将其用作启动板,并在尖括号之间填写您的网络详细信息。

相关问题