Elastic Beanstalk nginx proxy throwing 502 Bad Gateway - node.js

I have a simple MEAN application, which I compose using a Mongo image and a Node + Express image. I use Elastic Beanstalk to create a Docker Application. I am able to create an environment successfully. EB dashboard tells me that both the application and the environment were successfully created.
However the URL for my EB instance gives me a 502 Bad Gateway.
I researched this error in relation to Beanstalk and variously tried:
Adding a NodeCommand: "npm start" property to .ebextensions/node-command.config which gave me a Duplicate or Invalid property error.
Mimicking the way the project is started as detailed in this repo: https://github.com/shippableSamples/sample_node_eb_docker/tree/tutorial
I am following this tutorial; http://blog.shippable.com/how-to-deploy-to-elastic-beanstalk-part-2, which uses the aforementioned project as an example.
Basically, I moved the app.listen code under bin/www
None of these approaches worked.
My Dockerfile
FROM node:7
RUN mkdir -p /usr/src/rishighan
WORKDIR /usr/src/rishighan
COPY package.json /usr/src/rishighan
COPY bower.json /usr/src/rishighan
#Install dependencies
RUN npm i -g bower && \
npm i && \
bower i --config.interactive=false --allow-root
RUN npm rebuild node-sass
COPY . /usr/src/rishighan
EXPOSE 80
# This starts the app
CMD npm start
docker-compose.yml
# find a way to seed the mongo db
version: '3.2'
services:
mongodb:
build:
context: ./
dockerfile: mongo-seed/Dockerfile
web:
build: .
command: node server.js
volumes:
- .:/usr/src/rishighan
ports:
- "3000:3000"
links:
- mongodb
environment:
PORT: 3000 # this is optional, allows express to use process.env.PORT instead of a raw 3000
Dockerrun.aws.json
{
"AWSEBDockerrunVersion": "1",
"Image": {
"Name": "frishi/rishighanangular_web:nativeEBDeployment",
"Update": "true"
},
"Ports": [
{
"ContainerPort": "80"
}
],
"Volumes": [
]
}
Relevant bits of my package.json
"scripts": {
"dev": "webpack-dev-server --content-base app/ --hot --inline",
"start": "webpack -w --debug --devtool eval --output-pathinfo",
"local-dev": "node server.js",
"test": "testem ci"
}
Relevant bits of my server.js
// Start Server on 3000
let port = process.env.PORT || 3000;
app.listen(port, function () {
console.log("Server listening on port ", port);
});
I also found an answer on SO that suggested overriding the nginx.conf before Beanstalk sets it. I was able to download logs from EB and they are as follows:
nginx error.log
2017/07/24 12:10:47 [error] 3378#0: *8824 connect() failed (111: Connection refused) while connecting to upstream, client: 10.65.133.248, server: , request: "GET / HTTP/1.1", upstream: "http://172.17.0.2:80/", host: "54.243.122.6"
2017/07/24 13:27:16 [error] 3378#0: *9903 connect() failed (111: Connection refused) while connecting to upstream, client: 10.164.151.69, server: , request: "GET /xmlrpc.php HTTP/1.1", upstream: "http://172.17.0.2:80/xmlrpc.php", host: "54.197.241.240"
2017/07/24 13:29:18 [error] 3378#0: *9946 connect() failed (111: Connection refused) while connecting to upstream, client: 10.164.151.69, server: , request: "GET / HTTP/1.1", upstream: "http://172.17.0.2:80/", host: "54.197.241.240"
2017/07/24 14:17:28 [error] 3378#0: *10615 connect() failed (111: Connection refused) while connecting to upstream, client: 10.65.133.248, server: , request: "GET / HTTP/1.1", upstream: "http://172.17.0.2:80/", host: "rishighan-angular-dev.us-east-1.elasticbeanstalk.com"
2017/07/24 14:17:28 [error] 3378#0: *10615 connect() failed (111: Connection refused) while connecting to upstream, client: 10.65.133.248, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://172.17.0.2:80/favicon.ico", host: "rishighan-angular-dev.us-east-1.elasticbeanstalk.com", referrer: "http://rishighan-angular-dev.us-east-1.elasticbeanstalk.com/"
I would appreciate any pointers as to where to start looking for potential causes of the error.

Had a similar issue myself, and the problem was that nginx connects to node.js on port 8081 by default. I can see in your code that you're trying to listen on port 3000 by default.

Related

Nginx Error : (111: Connection refused) while connecting to upstream | EB2 | NodeJs app

I am a newbie and still learning development. I have deployed a nodejs app on EB2 for a project I am making. While most of the time the app works fine, sometimes it will stop working and API calls will stop going through giving me a '504 Gateway Time-out' error. I checked the logs and here is what I found repeatedly :
2022/09/22 16:18:32 [error] 3377#3377: *3027 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.2.149, server: , request: "GET /socket.io/?transport=polling&b64=1&EIO=4 HTTP/1.1", upstream: "http://127.0.0.1:8080/socket.io/?transport=polling&b64=1&EIO=4", host: "www.domain"
2022/09/22 16:18:33 [error] 3377#3377: *3209 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.2.149, server: , request: "GET /socket.io/?transport=polling&b64=1&EIO=4 HTTP/1.1", upstream: "http://127.0.0.1:8080/socket.io/?transport=polling&b64=1&EIO=4", host: "www.domain"
2022/09/22 16:22:40 [error] 3377#3377: *3318 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.2.149, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "172.31.9.9"
What is causing this and how can I resolve this?

Nodejs connect() failed (111: Connection refused) while connecting to upstream, client: localhost, server: , request: "GET / HTTP/1.1", upstream

I deloyed my Nodejs app to beanstalk service, I am able to Signup and Login my app, but after i Login, it just keeps buffering, not pulling any data, i checked my AWS logs and found this error
2021/10/22 23:43:09 [error] 3610#3610: *1 connect() failed (111: Connection refused) while connecting to upstream, client: ip-address, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "ip-address"
i have changed my settings on my security groups in configuration with no luck, would be glad to get some assistance here. Thank you!

connect() failed (111: Connection refused) while connecting to upstream Nodejs Elastick Beanstalk

I have a log nodejs backend code here and it runs on port 8080.
process.env.PORT = 8080 in the environment varisbles.
var port = process.env.PORT;
app.listen(port, ()=> {
console.log("Port is", port);
})
It connects to the databse as I see the output in the log file :
Port is 8080
I get an error in the log file with the following.
/var/log/nginx/error.log
----------------------------------------
connect() failed (111: Connection refused) while connecting to upstream, client: 777.77.7.777, server: , request: "GET / HTTP/1.1", upstream: "http://111.1.1.1:8080/", host: "666.66.66.666"
Does anyone know why it errors?
Should the host: be my elastic beanstalk url by any chance?
If you need any additinal info let me know!

sockjs-node net::ERR_CONNECTION_REFUSED [duplicate]

This question already has an answer here:
GET: ERR_SSL_PROTOCOL_ERROR nginx + vue.js
(1 answer)
Closed 3 years ago.
I created a brand new tiny webapp with vue cli, so without adding anything, apart from what the empty vue-cli scaffolding brings:
(base) marco#pc:~/vueMatters/testproject$ npm run serve
> testproject#0.1.0 serve /home/marco/vueMatters/testproject
> vue-cli-service serve
INFO Starting development server...
98% after emitting CopyPlugin
DONE Compiled successfully in 1409ms 8:14:46 PM
App running at:
- Local: localhost:8080
- Network: 192.168.1.7:8080
Note that the development build is not optimized.
To create a production build, run npm run build.
And got this error message :
GET https://localhost/sockjs-node/info?t=1580228998416 net::ERR_CONNECTION_REFUSED
node --version
v12.10.0
npm -v
6.13.6
webpack-cli#3.3.10
Ubuntu 18.04.03 Server Edition
last lines of /var/log/nginx/error.log :
2020/01/28 18:10:57 [error] 980#980: *34 connect() failed (111:
Connection refused) while connecting to upstream, client:
66.249.79.119, server: ggc.world, request: "GET /robots.txt HTTP/1.1",
upstream: "http://127.0.0.1:8080/robots.txt", host: "ggc.world"
2020/01/28 18:11:37 [error] 980#980: *36 connect() failed (111:
Connection refused) while connecting to upstream, client: 66.249.79.70,
server: ggc.world, request: "GET /robots.txt HTTP/1.1", upstream:
"http://127.0.0.1:8080/robots.txt", host: "www.ggc.world"
How to solve the problem?
You need to specify SSL Certificates in your socket connections.
Here is the reference.
var options = {
key: fs.readFileSync('./file.pem'),
cert: fs.readFileSync('./file.crt')
};
var server = https.createServer(options, app);
var io = require('socket.io')(server);

AWS elastic Beanstalk / nginx : connect() failed (111: Connection refused

I got this message
connect() failed (111: Connection refused
Here is my log:
-------------------------------------
/var/log/nginx/error.log
-------------------------------------
2018/10/21 06:16:33 [error] 4282#0: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.4.119, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8081/", host: "hackingdeal-env.qnyexn72ga.ap-northeast-2.elasticbeanstalk.com"
2018/10/21 06:16:33 [error] 4282#0: *2 connect() failed (111: Connection refused) while connecting to upstream, client: 172.31.4.119, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:8081/favicon.ico", host: "hackingdeal-env.qnyexn72ga.ap-northeast-2.elasticbeanstalk.com", referrer: "http://hackingdeal-env.qnyexn72ga.ap-northeast-2.elasticbeanstalk.com/"
I am using nodejs/express Elastic Beanstalk env.
I have one nginx related file in
.ebextensions/nginx/conf.d/proxy.conf
Upper file contains:
client_max_body_size 50M;
Whenever I try to get my webpage I got 502 bad gateway.
What's wrong with my app?
Just recording my incident here just in case it helps someone or my future self. I had a Django application that had SECURE_SSL_REDIRECT set to True. Since I had no load balancers configured to handle HTTPS traffic I was getting a timeout. Setting it to False fixed the issue. Couple of days wasted on that one.
111 connection refused likely means your app isn't running on the server/port combination. Also check that the security group for your app instance (or load balancer) has an inbound rule set to allow traffic from the nginx instance
I was dealing with this error on my NodeJS application (NEXTJS). Posting this here just in case is useful for someone.
My error was thet the deploy command failed at the build step (next build), which means the Node server never restarted. For that reason nginx could not find the server. You can find this kind of errors in the web.stdout.log
I tested my build command locally, fixed the errors and it worked!

Resources