PM2 and Nginx: 502 Bad Gateway - node.js

I've been trying to deploy my Node project on a brand new DO droplet, but i'm having some problems with PM2.
My steps are a follows:
Node came installed on the Droplet image (Ubuntu, Node v4.4.4)
Installed PM2 globally
Setup Nginx to reverse proxy 127.0.0.1:3000
Cloned my project and did npm install
All i get is Nginx complaining about a 502 Bad Gateway.
If i look at the Nginx error.log i get this:
connect() failed (111: Connection refused) while connecting to
upstream, client: client.ip, server: my.server, request:
"GET / HTTP/1.1", upstream: "http://127.0.0.1:3000/", host:
"my.server"
PM2 doesn't have much to say about anything. Nothing in pm2 logs and status is online.
I tried skipping PM2 and just doing npm start which worked perfectly. I also tried setting up a dummy hello world application instead, and using that with PM2 - it also worked.
So this is currently where i'm at:
My project + PM2: doesn't work.
My project without PM2: works.
Hello World app + PM2: works.
I'm not really sure where to go from here.. I could just skip PM2 and use node, but i do want the features of PM2.
Any ideas?

I just had to start PM2 with bin/www instead of app.js. Express generator and everything...

Nginx has a directive called proxy_read_timeout which defaults to 60 secs. It determines how long nginx will wait to get the response to a request. In nginx. conf file, setting proxy_read_timeout to 120 secs solved our problem.

In my case I renamed app.js to project.js or something and it works.

This is some issue with the node installation. Delete the node_modules folder, install again using npm and start the project using pm2.
pm2 kill
rm -rf node_modules
npm i
pm2 start bin/www

Related

during Nginx hosting (nodejs) pm2 status ocured error

during hosting when uploading my website by cloning git and added to the nginx but when starting the server by using the command PM2 START SERVER.JS it shows status as error
during hosting when uploading my website by cloning git and added to the nginx but when starting the server by using the command PM2 START SERVER.JS it shows status as error

nginx.conf for NodeJS/React App returning 502 and 405

Trying to setup a staging environment on Amazon LINUX EC2 instance and migrate from Heroku.
My repository has two folders:
Web
API
Our frontend and backend are running on the same port in deployment
In dev, these are run on separate ports and all requests from WEB and proxied to API
(for ex. WEB runs on PORT 3000 and API runs on PORT 3001. Have a proxy set up in the package.json file in WEB/)
Currently the application deployment works like this:
Build Web/ for distribution
Copy build/ to API folder
Deploy to Heroku with web npm start
In prod, we only deploy API folder with the WEB build/
Current nginx.conf looks like this
Commented out all other attempts
Also using PM2 to run the thread like so
$ sudo pm2 bin/www
Current thread running like so:
pm2 log
This is running on PORT 3000 on the EC2 instance
Going to the public IPv4 DNS for instance brings me to the login, which it's getting from the /build folder but none of the login methods (or any API calls) are working.
502 response example
I have tried a lot of different configurations. Set up the proxy_pass to port 3000 since thats where the Node process is running.
The only response codes I get are 405 Not Allowed and 502 Bad Gateway
Please let me know if there is any other information I can provide to find the solution.
It looks like you don't have an upstream block in your configuration. Looks like you're trying to use proxy-pass to send to a named server and port instead of a defined upstream. There's is an example on this page that shows how you define the upstream and then send traffic to it. https://nginx.org/en/docs/http/ngx_http_upstream_module.html
server backend1.example.com weight=5;
server backend2.example.com:8080;
server unix:/tmp/backend3;
server backup1.example.com:8080 backup;
server backup2.example.com:8080 backup;
}
server {
location / {
proxy_pass http://backend;
}
}````
Turns out there was an issue with express-sessions being stored in Postgres.
This led me to retest the connection strings and I found out that I kept receiving the following error:
connect ECONNREFUSED 127.0.0.1:5432
I did have a .env file holding the env variables and they were not being read by pm2.
So I added this line to app.js:
const path = require("path");
require('dotenv').config({ path: path.join(__dirname, '.env') });
then restarted the app with pm2 with the following command:
$ pm2 restart /bin/www --update-env

Deploy node.js in hostinger VPS but not running

I have my node app upload into hostinger and npm start run well.
But I still can't access http://my-ip:3000
node and npm and mysql have been installed
but no luck.
Am I missing some important steps?
this problem could come from your code || network
pm2 logs
see if there are some error logs
check on vps that if your service online
curl http://127.0.0.1:3000
if no resposne, you should fix the service code
check if your service runs on the right port
netstat -an | grep 3000

Error on EB AWS - 502 Bad Gateway nginx/1.18.0

I have a problem regarding my Node app that I am running in AWS. Everything has been working fine until today.
Without any new deployment to the app, I am getting this 502 error when I try to access the URL.
I have checked the nginx logs, and Im getting this:
2020/10/11 01:24:55 [error] 4735#0: *1 connect() failed (111: Connection refused) while connecting to upstream, server: , request: "GET /favicon.ico HTTP/1.1", upstream: "http://127.0.0.1:8080/favicon.ico", host: "mydomain.com", referrer: "mydomain.com"
I have tried to create a new security group with new inbound rules but it didnt work, has there been ane "new" update on EB configuration Im missing?
Thanks in advance.
enter image description hereIf you are using python > 3.6 on eb. Then eb will use gunicorn to run your django app.
So you need to install gunicorn with pip install gunicorn also mention it in requirements.txt with pip freeze requirements.txt and most important thing is You need to create "Procfile" in your project root where manage.py is located.
"Procfile" will have no extension.
"Procfile" content is below:
web: gunicorn --bind :8000 --workers 3 --threads 2 ebdjango.wsgi:application
in place of ebdjango put you project name and deploy with eb deploy.
Thanks!
I got exactly the same problem exactly at the same time than you. My solution was to remove the start of the program from index.js to the app.js. After that everything works fine again. For the some reason aws does not start index.js file anymore.

How to find what is causing a 502 Bad Gateway nginx Ubuntu

I'm trying to deploy my app which using nodejs and angular. Everything looks fine on
sudo pm2 logs.
I have also done
sudo nginx -t
Nginx is ok and successful.
However when I look at the error.logs from nginx directory, I have this.
I then when to this link here Nginx error "1024 worker_connections are not enough"
I changed it to 1500 worker connections and I'm still in the same spot. Is there another way around this? Please help, I have been trying to deploy this website for over a week. Thank you for your time.

Resources