简体   繁体   English

用 pm2 防止“EADDRINUSE”?

[英]Prevent "EADDRINUSE" with pm2?

I am about to switch my node application server from phusion passenger to pm2.我即将将我的节点应用程序服务器从 phusion 乘客切换到 pm2。

Most of the ports of my apps are set to 3001. With passenger that's never been a problem, but with pm2 ports collide (EADDRINUSE).我的应用程序的大多数端口都设置为 3001。乘客从来都不是问题,但是 pm2 端口会发生冲突(EADDRINUSE)。

Do I have to set a different port for every app to prevent port collisions?我是否必须为每个应用程序设置不同的端口以防止端口冲突?

Yes, of course you need to have each application listen on a different and free port.是的,当然您需要让每个应用程序在不同的空闲端口上侦听。 It is your app that listen to port, not PM2.监听端口的是你的应用程序,而不是 PM2。

You can leave the same port in source code, but in this case, start your app like this to change port when starting your app:您可以在源代码中保留相同的端口,但在这种情况下,像这样启动您的应用程序以在启动您的应用程序时更改端口:

// Work for express
PORT=3012 pm2 start -n "My Application" app.js

That's because express add this in your startter script:那是因为 express 在你的启动脚本中添加了这个:

var port  = normalizePort( process.env.PORT || '3509' );

Notice that other package may use another name env var, like NODE_PORT for example.请注意,其他 package 可能使用另一个名称 env var,例如 NODE_PORT。

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

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