【问题标题】:How to avoid CORS origin error with docker and elastic search image如何使用 docker 和弹性搜索图像避免 CORS 来源错误
【发布时间】:2020-06-03 06:10:46
【问题描述】:

我试图通过使用 docker 设置 apache web 服务器来绕过 CORS 源错误客户端,以便使用模块。尽管如此,当我将 elasticsearch 服务添加到我的 docker-compose 并尝试从中获取我的 json 响应时,我仍然拥有它。它说“跨源请求被阻止:同源策略不允许在 http://localhost:9200/ 读取远程资源”

我的码头工人撰写:

version: '2'

services:
  apache:
    image: 'bitnami/apache:latest'
    ports:
      - '80:8080'
      - '443:8443'
    volumes:
      - .:/app
  es01:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.7.0
    container_name: es01
    environment:
      - node.name=es01
      - cluster.name=es-docker-cluster
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - data01:/usr/share/elasticsearch/data
    ports:
      - 9200:9200
    networks:
      - elastic

volumes:
  data01:
    driver: local


networks:
  elastic:
    driver: bridge

我的 index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Home</title>
</head>
<body>

<div class="home">
    <h5>Elastic home</h5>
    <p> Cluster name:</p>
    <p id="cluster_name"></p>
    <p> Cluster version: </p>
    <p id="cluster_version"></p>
</div>

<link rel="stylesheet" href="style.css">
<script type="application/javascript">
    fetch('http://localhost:9200')
        .then(async (response) => {
            let data = await response.json()
            document.getElementById('cluster_name').innerText = data.cluster_name
            document.getElementById('cluster_version').innerText = data.version.number
        })
</script>
</body>
</html>

【问题讨论】:

  • 我想我可以在服务弹性上将 Access-Control-Allow-Origin 标头选项设置为 *,以根据我在互联网上找到的内容允许端口 9200,但我不知道该怎么做.

标签: elasticsearch docker-compose cors


【解决方案1】:

我发现了如何将环境弹性服务更改为

   environment:
     - discovery.type=single-node
     - node.name=es01
     - cluster.name=es-docker-cluster
     - bootstrap.memory_lock=true
     - "ES_JAVA_OPTS=-Xms512m -Xmx512m"
     - http.host=0.0.0.0
     - http.port=9200
     - "http.cors.allow-origin=http://localhost"
     - "http.cors.enabled=true"
     - "http.cors.allow-headers=X-Requested-With,X-Auth-Token,Content-Type,Content-Length,Authorization"
     - "http.cors.allow-credentials=true"

【讨论】:

  • 作为那些运行集群的旁注,这也是这样做的方法。顺便说一句,干得好!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-10-21
  • 1970-01-01
  • 2016-01-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-22
相关资源
最近更新 更多