elasticsearch.js 客户端连接被拒绝:无法识别 Access-Control-Allow-Origin? [英] elasticsearch.js client connection refused: Access-Control-Allow-Origin not recognized?
问题描述
我一直在尝试使用 elasticsearch.jquery.min.js ping 本地运行的 elasticsearch,但每次都出现无活动连接"错误.
I've been trying to ping a locally running elasticsearch using elasticsearch.jquery.min.js and I get a "no living connection" error each time.
预计到达时间:在 Chrome 中,我看到了一个非常低级别的拒绝连接".我在 MacOS X 上开发,我的浏览器通过 http://localhost/~myuserid/SiteName/
指向页面.当我访问 localhost:9200
时,这显然属于跨域 CORS 要求.
ETA: In Chrome I see what looks like a pretty low level "Connection Refused". I'm developing on MacOS X, and my browser points at the page via http://localhost/~myuserid/SiteName/
. As I'm accessing localhost:9200
this clearly falls under cross domain CORS requirements.
我在 Chrome 控制台中看到以下错误:
I see the following error in the Chrome console:
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:
Per http://enable-cors.org/server_apache.html I've added the following to /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
但错误仍然存在.我忽略了另一个设置吗?
but the error persists. Is there another setting I'm overlooking?
我是 elasticsearch.js 的菜鸟.我需要在 elasticsearch 端做些什么来允许来自浏览器的客户端连接或其他什么吗?
I'm a noob to elasticsearch.js. Is there anything I need to do on the elasticsearch side to allow client connections from the browser, or something?
我正在关注这本书 在我的 ping 尝试中:
I'm following the book in my ping attempt:
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 连接就好了,拉和插入数据等等:
I can connect using curl on the command line just fine, pull and insert data, etc.:
$ 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 附加诊断.谷歌浏览器显示失败尝试的以下网络跟踪.在 HTTP 层,响应看起来正在发生.
ETA additional diagnostics. Google Chrome shows the following network traces for the failing attempt. At the HTTP layer the response looks like it's happening.
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 中的相同请求:
The same request in 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 上看到很多对错误的引用,但没有一种情况看起来很相似.感觉就像我只是遇到了一些配置错误,但我找不到任何可以表明那是什么的东西.
I'm really at a loss where to go from here. I see lots of references to the error on teh googlz, but none of the circumstances seem remotely similar. It feels like I'm just hitting some misconfiguration, but I can't find anything that would indicate what that is.
推荐答案
嗯,这是一个艰难的问题.
Well, that was a tough one.
修复方法如下:
根据 http://enable-cors.org/server_apache.html,在 /etc/apache2/httpd.conf
,配置Access-Control-Allow-Origin:
Per http://enable-cors.org/server_apache.html, in /etc/apache2/httpd.conf
, configure Access-Control-Allow-Origin:
<Directory />
# Add the following line to enable CORS to connect to local elasticsearch.
Header set Access-Control-Allow-Origin "localhost:9200"
AllowOverride none
Require all denied
</Directory>
根据 https://jsbroadcast.com/elastic-search-cors/,在elasticsearch-1.7.0/config/elasticsearch.yml
添加:
http.cors.enabled : true //
http.cors.allow-origin: "/.*/"
http.cors.allow-methods : OPTIONS, HEAD, GET, POST, PUT, DELETE
http.cors.allow-headers : "X-Requested-With,X-Auth-Token,Content-Type, Content-Length, Authorization"
我现在可以毫无错误地运行 client.ping
调用.
I can now run the client.ping
call without any error.
这篇关于elasticsearch.js 客户端连接被拒绝:无法识别 Access-Control-Allow-Origin?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!