Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid Host Header when using elasticsearch client

When using the elasticsearch client (from the elasticsearch npm version 15.4.1), the AWS elasticsearch service complains about an Invalid Host Header. This happens for every request even though they work.

I double-checked the configuration for initializing the elasticsearch client and the parameter "host" is correctly formed.

let test = require('elasticsearch').Client({
    host: 'search-xxx.us-west-1.es.amazonaws.com',
    connectionClass: require('http-aws-es')
});

I expected to get a clean ElasticsearchRequest without a corresponding InvalidHostHeaderRequests (I can see these logs on the Cluster health dashboard of the Amazon Elasticsearch Service).

like image 890
TantrixRobotBoy Avatar asked Oct 21 '25 12:10

TantrixRobotBoy


1 Answers

Found the problem. When using elasticsearch library to connect to an AWS ES cluster, the previous syntax can lead to problems, so the best way to initialize the client is specifying the entire 'host' object as follows:

host: {
    protocol: 'https',
    host: 'search-xxx.us-west-1.es.amazonaws.com',
    port: '443',
    path: '/'

The problem here is that probably AWS ES Cluster expects the host field inside the host object and this leads to the "Invalid Host Header" issue. Hope this will help the community to write better code. Refer to https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/16.x/host-reference.html for reference.

like image 154
TantrixRobotBoy Avatar answered Oct 24 '25 01:10

TantrixRobotBoy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!