Sco*_*ott 5 apache ajax jquery localhost access-control
我一直在尝试使用elasticsearch.jquery.min.js ping本地运行的elasticsearch,每次都会出现"无生命连接"错误.
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.
Run Code Online (Sandbox Code Playgroud)
根据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>
Run Code Online (Sandbox Code Playgroud)
并运行
$ sudo apachectl -t
$ sudo apachectl -k graceful
Run Code Online (Sandbox Code Playgroud)
但错误仍然存在.我还有另一种设置吗?
我是elasticsearch.js的菜鸟.我是否需要在弹性搜索方面做任何事情以允许来自浏览器的客户端连接,或者什么?
我正在读这本书尝试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');
}
}
);
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
"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
Run Code Online (Sandbox Code Playgroud)
我可以在命令行上使用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
Run Code Online (Sandbox Code Playgroud)
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!
Run Code Online (Sandbox Code Playgroud)
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]
Run Code Online (Sandbox Code Playgroud)
我真的不知道从哪里开始.我在googlz上看到了很多关于错误的引用,但没有一种情况看起来很相似.感觉就像我只是遇到了一些错误的配置,但我找不到任何可以表明它是什么的东西.
Sco*_*ott 22
嗯,这是一个艰难的.
这是修复它的原因:
根据http://enable-cors.org/server_apache.html,在/etc/apache2/httpd.conf,配置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>
Run Code Online (Sandbox Code Playgroud)
根据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"
Run Code Online (Sandbox Code Playgroud)
我现在可以毫无错误地运行client.ping呼叫.
| 归档时间: |
|
| 查看次数: |
8840 次 |
| 最近记录: |