我正在使用nginx来保护elasticsearch。尝试访问客户端js中的elasticsearch时。抛出无法恢复连接: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;
}
}
答案 0 :(得分:0)
您可以尝试覆盖配置中的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,这会在你使用代理时导致不一致的结果