4

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.


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.

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.

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>

and run

$ sudo apachectl -t
$ sudo apachectl -k graceful

but the error persists. Is there another setting I'm overlooking?


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?

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');
      }
    }
  );

but I'm getting the following error(s):

"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

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 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!

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]

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.

3
  • The Javascript client library is known to have a bug in connection pooling. Your issue looks a lot like this one.
    – Val
    Commented Oct 10, 2015 at 10:56
  • Saw that one, but the circumstances seem different. There, the pooling dies after working for a while. In my case there's never any connection. The initial attempt to establish a connection fails right away with a "CONNECTION REFUSED" error at some low level. The behavior seems the same as when elasticsearch isn't runnng.
    – Scott
    Commented Oct 10, 2015 at 15:06
  • Things I've considered: Some AJAX cross domain type issue (although it's all on local host: apache, elasticsearch, browser and all); some missing component or configuration on elasticsearch to enable or allow the connection (though I can't find any indication what that might be), an API or version mismatch (I'm using ES 1.7 and the 1.7 elasticsearch.js API and have tried the latest jQuery on the 1.x and 2.x lines), browser issues (same behavior in Chrome and Firefox) and a host of other "well, couldn't hurt to try that" possibilities, none of which were the key.
    – Scott
    Commented Oct 10, 2015 at 15:14

3 Answers 3

22

Well, that was a tough one.

Here's what fixed it:

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>

Per https://jsbroadcast.com/elastic-search-cors/, in elasticsearch-1.7.0/config/elasticsearch.yml add:

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"

I can now run the client.ping call without any error.

6
  • 2
    The apache configuration change is not necessary (or useful) here. Only the change to elasticsearch.yml is needed. Commented May 3, 2016 at 21:46
  • Russell, I experimented extensively with and without the changes in different combinations, and couldn't get it to work without making both changes. (If only one change had worked I would have hit it with far fewer hours of trying.) Why would the web server, which is mediating the connection, not need to allow CORS?
    – Scott
    Commented May 4, 2016 at 15:57
  • My apologies, Scott. I misunderstood your setup. I'm not sure what role I thought Apache was playing here. Commented May 7, 2016 at 4:20
  • No worries. All part of the exchange. :)
    – Scott
    Commented May 7, 2016 at 4:52
  • Thank you sir. Adding the http.cors section to elasticsearch.yml saved my day.
    – richie
    Commented Aug 18, 2016 at 7:10
4

Just adding this to elasticsearch.yml should work.

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"

You don't need to add anything to Apache.

7
  • See my response to Russell above. This was most decidely not sufficient.
    – Scott
    Commented May 12, 2016 at 21:59
  • @scott Yeah, I saw your response but you are saying you need to modify two files, one in Apache and one in ES, which is not true. Thank you for the down vote by the way.
    – Penman
    Commented May 13, 2016 at 9:59
  • Again, here is what was true: Only modifying one of the files did not solve the problem, and modifying two did. I'll believe my own lying eyes, thanks.
    – Scott
    Commented May 14, 2016 at 17:58
  • @scott Well, maybe it worked for you but for me it was just one file that solved the problem. To be honest, I wanted to post this as a comment but because of my reputation, I couldn't do that.
    – Penman
    Commented May 14, 2016 at 18:38
  • Nothing in my response should be taken to imply that there aren't scenarios where only one file needs to be modified. There might be something I could have configured differently elsewhere that would have made that possible in my case, but I don't know what that might be. The down vote was for posting the same answer I had already explained was known to be insufficient. If you have suggestions for identifying what else might be a factor, that would be helpful. Following up with a comment that your solution must work for me when what you mean is that it worked for you is not.
    – Scott
    Commented May 15, 2016 at 18:59
2

Put in your elasticsearch.yml configuration file, located in elasticsearch-xxx\config\elasticsearch.yml, the line below

http.cors.enabled: true

and Enable cors in your internet navigator. Restart you elasticsearch server and try again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.