js-клиент не смог подключиться к elasticsearch за nginx

Я использую nginx для защиты elasticsearch. При попытке доступа к elasticsearch в клиенте js. Выдает Невозможно восстановить соединение: http://ubuntulocal:80:9200/ ошибка.

Мой вопрос: как подключить elasticsearch с помощью клиента js за прокси?

JS-код узла

var elasticsearch = require('elasticsearch');
var host = [
    {
    host: 'http://ubunutlocal:80',
    auth: 'root:root'
    }]


 var client = new elasticsearch.Client({
    host: host
 });


client.search({
  index : 'bank'
  // undocumented params are appended to the query string
  //hello: "elasticsearch"
}, function (error , response) {
  if (error) {
    console.error('elasticsearch cluster is down!' , error);
  } else {
    console.log('All is well' , response);
  }
});

Журнал ошибок

Elasticsearch ERROR: 2015-10-26T13:14:06Z
  Error: Request error, retrying -- getaddrinfo ENOTFOUND ubuntulocal:80
      at Log.error (/node_modules/elasticsearch/src/lib/log.js:213:60)
      at checkRespForFailure (/node_modules/elasticsearch/src/lib/transport.js:192:18)
      at HttpConnector.<anonymous> (
      /node_modules/elasticsearch/src/lib/connectors/http.js:153:7)
      at ClientRequest.wrapper (
      node_modules/elasticsearch/node_modules/lodash/index.js:3111:19)
      at ClientRequest.emit (events.js:107:17)
      at Socket.socketErrorListener (_http_client.js:271:9)
      at Socket.emit (events.js:107:17)
      at net.js:950:16
      at process._tickCallback (node.js:355:11)

Elasticsearch WARNING: 2015-10-26T13:14:06Z
  Unable to revive connection: http://ubuntulocal:80:9200/

Elasticsearch WARNING: 2015-10-26T13:14:06Z
  No living connections

elasticsearch cluster is down! { [Error: No Living connections] message: 'No Living connections' }

Конфигурация Nginx

server {
    listen 80;
    server_name guidanzlocal;
    location / {
        rewrite ^/(.*) /$1 break;
        proxy_ignore_client_abort on;
        proxy_pass http://localhost:9200;
        proxy_redirect http://localhost:9200 http://guidanzlocal/;
        proxy_set_header  X-Real-IP  $remote_addr;
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header  Host $http_host;
        auth_basic "Elasticsearch Authentication";
        auth_basic_user_file /etc/elasticsearch/user.pwd;
}
}

person Devaraj C    schedule 26.10.2015    source источник


Ответы (1)


Не могли бы вы попробовать переопределить nodesToHostCallback в своей конфигурации:

var host = [{
  host: 'http://ubunutlocal:9200',
  auth: 'root:root'
}]


var client = new elasticsearch.Client({
  host: host,
  nodesToHostCallback: function ( /*nodes*/ ) {
    return [host];
  }
});

Когда elasticsearch-js пытается повторно подключиться после сбоя, он пингует ваш узел, чтобы получить URL-адрес, что может привести к противоречивому результату при использовании прокси-сервера.

person Julien C.    schedule 26.10.2015