Fixing Elasticsearch-head CORS errors
When testing something for a client, I recently decided to fire up elasticsearch-head to analyse the elasticsearch cluster I had running locally. Unfortunately, I repeatedly got requests failing with CORS errors.
As I was using docker-compose to setup and run the ES cluster, and I needed to modify the
elasticsearch.yml file in the container, I had to add a few lines to my
docker-compose.yml to link my custom config in the correct place:
services: elasticsearch: container_name: elasticsearch image: elasticsearch:7.17.6 environment: - discovery.type=single-node - xpack.security.enabled=false ports: - "9200:9200" volumes: - ./data:/usr/share/elasticsearch/data:rw - type: bind source: ./es-config/elasticsearch.yml target: /usr/share/elasticsearch/config/elasticsearch.yml read_only: true
Once I had that set, I could add my custom
elasticsearch.yml in the correct place (
cluster.name: "docker-cluster" network.host: 0.0.0.0 http.cors.enabled: true http.cors.allow-origin: "*" http.cors.allow-headers: "Authorization,Content-Type"
…but I Still saw the same errors when trying to access the cluster directly. Luckily, there is a proxy supplied with elasticsearch-head that fixes this. Running
» npm run proxy > firstname.lastname@example.org proxy > node proxy/index.js creating proxy localhost:9200 remote: http://localhost:9200 local: http://localhost:9101
…and connecting to
[http://localhost:9101](http://localhost:9101) fixed it!
When getting the
elasticsearch.yml from the docker container, I ran:
docker exec -ti f2bf446675a2_perc-elasticsearch bash
…to get access to the container, and navigated to