PM2 and Nginx: 502 Bad Gateway
Asked Answered
A

4

12

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:

  1. Node came installed on the Droplet image (Ubuntu, Node v4.4.4)
  2. Installed PM2 globally
  3. Setup Nginx to reverse proxy 127.0.0.1:3000
  4. 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?

Adair answered 24/5, 2016 at 10:24 Comment(3)
Are you sure your app is actually starting on port 3000?Kampmeier
Well.. it's probably isn't as Nginx is complaining. I just have no idea how i can check. PM2 says that's everything is all good and online and the logs are clean.Adair
My problem actually turned out to so silly actually, I noticed that 502s were given only for POST requests that do modify the database, which was a sqllite file within the app directory which caused PM2 to restart the app before sending the response when that file was written because it was run with --watch flag. on other cases it can be the --kill-timeout that is too short for you response time, which also causes the app restart.Trocar
A
15

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

Adair answered 24/5, 2016 at 12:51 Comment(7)
Have had an incredibly horrific afternoon trying to solve this problem. Would love to know why bin/www worked while app.js did not.Ferrite
Thank you. Worked with my MEAN application.Eadwina
Also ensure NODE_ENV=productionGipps
what is bin/www and where is it located ?Owe
@PeterMoses and anyone else interested: bin/www is relative your application directory. Example: my project is in /var/www/my-project/. From my-project, I ran pm2 start node ./bin/www and my application is now served as expected.Cobbler
work in 2022!!!Asel
Relative paths are always a pain!! Thanks mateSagittal
C
0

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.

Cobden answered 22/7, 2020 at 3:49 Comment(0)
R
0

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

Rainie answered 9/2, 2022 at 20:38 Comment(0)
B
-3

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

Baeda answered 11/7, 2021 at 2:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.