1.访问测试
2.解决方案
2.1.编辑elasticsearch-head目录下的Gruntfile.js文件
- 在96行之间添加 hostname: '0.0.0.0',
connect: {
server: {
options: {
hostname: '0.0.0.0',
port: 9100,
base: '.',
keepalive: true
}
}
}
2.2.编辑 /etc/elasticsearch/elasticsearch.yml文件
- 在文章末尾添加三行内容
http.cors.enabled: true
http.cors.allow-origin: "*"
http.cors.allow-methods: "GET"
2.3.重启服务再次浏览器测试访问
3.知识点总结
3.1.CORS是什么?
- wiki上的解释是 Cross-origin resource sharing (CORS) is a mechanism that allows restricted resources ,即跨域访问。 这个字段默认为false,在Elasticsearch安装集群之外的一台机上用Sense、Head等监控插件访问Elasticsearch是不允许的。这个字段最早可以追溯到1.4.x版本,而非5.x特有。
3.2.http.cors.x字段还有哪些用途和用法
1.http.cors.enabled
是否支持跨域,默认为false
2.http.cors.allow-origin
当设置允许跨域,默认为*,表示支持所有域名,如果我们只是允许某些网站能访问,那么可以使用正则表达式。
比如只允许本地地址。 /https?://localhost(:[0-9]+)?/
3.http.cors.max-age
浏览器发送一个“预检”OPTIONS请求,以确定CORS设置。最大年龄定义多久的结果应该缓存。默认为1728000(20天)
4.http.cors.allow-methods
允许跨域的请求方式,默认OPTIONS,HEAD,GET,POST,PUT,DELETE
5.http.cors.allow-headers
跨域允许设置的头信息,默认为X-Requested-With,Content-Type,Content-Length
6.http.cors.allow-credentials
是否返回设置的跨域Access-Control-Allow-Credentials头,如果设置为true,那么会返回给客户端。