【发布时间】:2016-01-08 04:16:13
【问题描述】:
我一直在尝试使用 elasticsearch.jquery.min.js 对本地运行的 elasticsearch 进行 ping 操作,但每次都会收到“无活动连接”错误。
ETA:在 Chrome 中,我看到看起来很低级别的“连接被拒绝”。我正在 MacOS X 上开发,我的浏览器通过http://localhost/~myuserid/SiteName/ 指向页面。当我访问 localhost:9200 时,这显然属于跨域 CORS 要求。
我在 Chrome 控制台中看到以下错误:
XMLHttpRequest cannot load http://localhost:9200/?hello=elasticsearch!.
No 'Access-Control-Allow-Origin' header is present on the requested resource.
Origin 'http://localhost' is therefore not allowed access.
根据http://enable-cors.org/server_apache.html,我已将以下内容添加到 /etc/apache2/httpd.conf:
<Directory />
Header set Access-Control-Allow-Origin "localhost:9200"
AllowOverride none
Require all denied
</Directory>
然后运行
$ sudo apachectl -t
$ sudo apachectl -k graceful
但错误仍然存在。我忽略了另一个设置吗?
我是 elasticsearch.js 的菜鸟。在弹性搜索方面我需要做些什么来允许来自浏览器的客户端连接,还是什么?
我在 ping 尝试中关注 the book:
var client = new $.es.Client({
hosts: 'localhost:9200'
});
client.ping(
{
requestTimeout: Infinity,
// undocumented params are appended to the query string
hello: "elasticsearch!"
},
function (error) {
if (error) {
console.error('elasticsearch cluster is down!');
console.error(error);
} else {
console.log('All is well');
}
}
);
但我收到以下错误:
"WARNING: 2015-10-10T07:00:16Z" elasticsearch.jquery.min.js:14:10575
Unable to revive connection: http://localhost:9200/
"WARNING: 2015-10-10T07:00:16Z" elasticsearch.jquery.min.js:14:10575
No living connections
我可以在命令行上使用 curl 进行连接,拉取和插入数据等:
$ curl "localhost:9200/_cat/indices?v"
health status index pri rep docs.count docs.deleted store.size pri.store.size
green open fuddle 1 0 3 0 12.9kb 12.9kb
green open faddle 1 0 0 0 144b 144b
ETA 附加诊断。 Google Chrome 显示以下失败尝试的网络跟踪。在 HTTP 层,响应看起来正在发生。
General
Remote Address:[::1]:9200
Request URL:http://localhost:9200/?hello=elasticsearch!
Request Method:HEAD
Status Code:200 OK
Response Headers
Content-Length:0
Content-Type:text/plain; charset=UTF-8
Request Headers
Accept:text/plain, */*; q=0.01
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:0
Host:localhost:9200
Origin:http://localhost
Referer:http://localhost/~browsc3/Opticon/
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36
Query String Parameters
view URL encoded
hello:elasticsearch!
wget中同样的请求:
wget http://localhost:9200/?hello=elasticsearch!
--2015-10-10 09:47:13-- http://localhost:9200/?hello=elasticsearch!
Resolving localhost... ::1, 127.0.0.1
Connecting to localhost|::1|:9200... connected.
HTTP request sent, awaiting response... 200 OK
Length: 342 [application/json]
Saving to: 'index.html?hello=elasticsearch!'
index.html?hello=elastics 100%[=====================================>] 342 --.-KB/s in 0s
2015-10-10 09:47:13 (65.2 MB/s) - 'index.html?hello=elasticsearch!' saved [342/342]
我真的不知道从这里去哪里。我在 teh googlz 上看到很多关于该错误的引用,但这些情况似乎都不太相似。感觉就像我只是遇到了一些错误配置,但我找不到任何可以表明那是什么的东西。
【问题讨论】:
-
已知 Javascript 客户端库在连接池中存在错误。你的问题看起来很多like this one。
-
看到了那个,但情况似乎不同。在那里,池在工作一段时间后死亡。就我而言,从来没有任何联系。建立连接的初始尝试立即失败,并在某个低级别出现“CONNECTION REFUSED”错误。行为似乎与未运行弹性搜索时相同。
-
我考虑过的事情:一些 AJAX 跨域类型问题(尽管都在本地主机上:apache、elasticsearch、浏览器等等); elasticsearch 上缺少一些组件或配置以启用或允许连接(尽管我找不到任何指示可能是什么),API 或版本不匹配(我正在使用 ES 1.7 和 1.7 elasticsearch.js API 并尝试过1.x 和 2.x 行上的最新 jQuery)、浏览器问题(Chrome 和 Firefox 中的行为相同)以及许多其他“好吧,尝试一下不会有坏处”的可能性,这些都不是关键。
标签: jquery ajax apache localhost access-control