简体   繁体   English

使用 PM2 运行应用程序给我一个 502 错误

[英]Running app using PM2 gives me a 502 error

I have a relatively basic Express app that I have sitting behind nginx.我有一个相对基本的 Express 应用程序,我坐在 nginx 后面。

Everything works fine when I run当我运行时一切正常

sudo node server.js

I can access the website without incident.我可以正常访问该网站。

However when I use但是当我使用

sudo pm2 start server.js I get an 'online' status and no errors (helpful), but when attempting to access the website I get a 502 error. sudo pm2 start server.js我得到一个“在线”状态并且没有错误(有帮助),但是在尝试访问网站时我得到一个 502 错误。 The instructions for PM2 don't indicate any additional steps are required. PM2 的说明并未指明需要任何其他步骤。

I've looked for solutions to this problem but they all seem slightly different (and there are a lot of them! I'm beginning to wonder if it works at all) - I've tried a few things, like targeting bin/www instead of server.js but no dice.我一直在寻找这个问题的解决方案,但它们似乎都略有不同(而且有很多!我开始怀疑它是否有效) - 我已经尝试了一些方法,比如定位 bin/www而不是 server.js 但没有骰子。

Do I need to perform additional setup for PM2 compatibility?我是否需要为 PM2 兼容性执行额外的设置? Should I look for alternatives?我应该寻找替代品吗? What could be causing these problems?什么可能导致这些问题?

Here is my current nginx config (improvements welcome), but I will stress that the setup works fine when not using PM2.这是我当前的 nginx 配置(欢迎改进),但我要强调的是,当不使用 PM2 时,设置工作正常。

(Domain replaced with X ) (域替换为X

server {
  listen 80;
  listen 443 ssl;

  server_name www.X.co.uk;

  ssl_certificate /etc/letsencrypt/live/X.co.uk/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/X.co.uk/privkey.pem;

  return 301 https://X.co.uk$request_uri;
}
server {
  listen 80;

  server_name X.co.uk;

  return 301 https://X.co.uk$request_uri;
}
server {
  listen 443 ssl;

  server_name X.co.uk;

  root /var/www/X;

  ssl_certificate /etc/letsencrypt/live/X.co.uk/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/X.co.uk/privkey.pem;

  location / {
    proxy_pass https://localhost:4003;
  }

  include /etc/nginx/h5bp/location/expires.conf;
  include /etc/nginx/h5bp/location/cross-domain-fonts.conf;
  include /etc/nginx/h5bp/location/protect-system-files.conf;
}

Thanks!谢谢!

I had the same problem when installed certbot.安装certbot时我遇到了同样的问题。 But

sudo pm2 delete server
sudo pm2 start server.js

did the trick and everything began to work as expected!成功了,一切都开始按预期工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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