简体   繁体   中英

node.js on elastic beanstalk: firefox 502 bad gateway

I'm running node on aws elastic beanstalk and everything was fine until I started getting a "502 Bad Gateway" when running an ajax request in a browser.

Checking the logs I see:

2014/12/09 18:56:48 [error] 25746#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 108.56.255.64, server: , request: "OPTIONS /match/update HTTP/1.1", upstream: "http://xxxx:8081/match/update", host: "xxxx.elasticbeanstalk.com"
2014/12/09 18:56:50 [error] 25746#0: *1 upstream prematurely closed connection while reading response header from upstream, client: 108.56.255.64, server: , request: "POST /match/update HTTP/1.1", upstream: "http://xxxx:8081/match/update", host: "xxxx.elasticbeanstalk.com"

I've implemented CORS in node.js so cross domain requests should be ok:

app.all('/', function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

Any ideas?

I found my problem. I had a document.write line in the jQuery portion of the web page making the ajax call. This didn't cause a problem when running locally, but when running on elastic beanstalk, there were two calls made (one for OPTIONS and the other the POST), the first ajax call OPTIONS runs ok, but the document.write would interrupt the POST, terminating the connection. I spent the entire day on this and my problem was a simple document.write. So make sure your async processes can run to completion when loading your page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM