【问题标题】:js client could not connect to elasticsearch behind nginxjs客户端无法连接到nginx后面的elasticsearch
【发布时间】:2016-01-25 16:23:19
【问题描述】:

我正在使用 nginx 来保护 elasticsearch。尝试在客户端 js 中访问 elasticsearch 时。它抛出 Unable to revive connection: http://ubuntulocal:80:9200/ 错误。

我的问题是如何使用代理后面的客户端js连接elasticsearch?

节点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;
}
}

【问题讨论】:

    标签: elasticsearch kibana-4 elasticsearch-plugin


    【解决方案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 尝试在失败后重新连接时,它会 ping 您的节点以获取 url,这可能导致您使用代理时结果不一致

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-03
      • 2020-05-26
      • 1970-01-01
      • 1970-01-01
      • 2020-12-27
      • 2013-06-06
      • 2022-10-20
      • 2017-01-02
      相关资源
      最近更新 更多