我一直试图使用elasticsearch.jquery.min.js在本地运行elasticsearch,每次我都会得到一个“没有活连接”的错误。
埃塔:在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.根据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的菜鸟。在elasticsearch端,我需要做些什么来允许客户端从浏览器连接吗?
我正在跟踪这本书的ping尝试:
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为失败的尝试显示了以下网络跟踪。在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上看到了很多关于错误的引用,但是没有一种情况看起来有点相似。这感觉就像我只是碰到了一些错误的配置,但我找不到任何东西来表明这是什么。
发布于 2015-10-10 19:15:42
嗯,那是个艰难的决定。
下面是修复它的方法:
在apache.html中,在/etc/apache2/httpd.conf中,配置访问-控制-允许-起源:
<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调用,而不会出现任何错误。
发布于 2016-05-12 16:57:42
只需将其添加到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"您不需要向Apache添加任何内容。
发布于 2015-11-06 13:48:02
放入您的elasticsearch.yml配置文件,位于elasticsearch.yml\config\弹性搜索. the中,下面一行
http.cors.enabled: true并在您的网络导航器中启用cors。重新启动elasticsearch服务器,然后重试。
https://stackoverflow.com/questions/33051405
复制相似问题