如何使用docker和elasticsearch镜像避免CORS源错误

eza*_*zer 6 cors elasticsearch docker-compose

我设法通过使用 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
Run Code Online (Sandbox Code Playgroud)

我的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>
Run Code Online (Sandbox Code Playgroud)

eza*_*zer 13

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

   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"

Run Code Online (Sandbox Code Playgroud)