如何避免重启NodeJS服务器时出现日志错误 [英] How to avoid logging errors from restarting NodeJS server

查看:212
本文介绍了如何避免重启NodeJS服务器时出现日志错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将NodeJS用作侦听8443的AWS虚拟机上的Web服务器,并使其运行于:

I use NodeJS as a web server on an AWS virtual machine listening on 8443 and leave it running with:

kill $(lsof -i :8443 | grep .node.bin | cut -d " " -f 2); cd $HOME/server; NODE_ENV=production npm start  >> stdout.txt 2>> stderr.txt &

我监视错误日志.每次我推送更改并重新启动服务器时,错误日志都会显示几行,例如:

I monitor the error log. Every time I push changes and restart the server, the error log gets several lines, such as:


/opt/bitnami/nodejs/bin/.node.bin[31215]: ../src/node.cc:663:void node::ResetStdio(): Assertion `(0) == (err)' failed.
 1: 0x9ef190 node::Abort() [/opt/bitnami/nodejs/bin/.node.bin]
 2: 0x9ef217  [/opt/bitnami/nodejs/bin/.node.bin]
 3: 0x9bd657 node::ResetStdio() [/opt/bitnami/nodejs/bin/.node.bin]
 4: 0x9bd6c0 node::SignalExit(int) [/opt/bitnami/nodejs/bin/.node.bin]
 5: 0x7f7e4922a390  [/lib/x86_64-linux-gnu/libpthread.so.0]
 6: 0x7f7e48f56ad3 epoll_wait [/lib/x86_64-linux-gnu/libc.so.6]
 7: 0x13200b0  [/opt/bitnami/nodejs/bin/.node.bin]
 8: 0x130e26b uv_run [/opt/bitnami/nodejs/bin/.node.bin]
 9: 0xa31ec3 node::NodeMainInstance::Run() [/opt/bitnami/nodejs/bin/.node.bin]
10: 0x9c1cc8 node::Start(int, char**) [/opt/bitnami/nodejs/bin/.node.bin]
11: 0x7f7e48e6f840 __libc_start_main [/lib/x86_64-linux-gnu/libc.so.6]
12: 0x95c085  [/opt/bitnami/nodejs/bin/.node.bin]
Aborted (core dumped)
npm ERR! code ELIFECYCLE
npm ERR! errno 134
npm ERR! EmotionAthletes@0.0.1 start: `node app.js`
npm ERR! Exit status 134
npm ERR! 
npm ERR! Failed at the EmotionAthletes@0.0.1 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/bitnami/.npm/_logs/2020-10-25T12_30_23_826Z-debug.log
npm[31199]: ../src/node.cc:663:void node::ResetStdio(): Assertion `(0) == (err)' failed.
 1: 0x9ef190 node::Abort() [npm]
 2: 0x9ef217  [npm]
 3: 0x9bd657 node::ResetStdio() [npm]
 4: 0x7f7db55c4008  [/lib/x86_64-linux-gnu/libc.so.6]
 5: 0x7f7db55c4055  [/lib/x86_64-linux-gnu/libc.so.6]
 6: 0x994907  [npm]
 7: 0xbc9a29  [npm]
 8: 0xbcb817 v8::internal::Builtin_HandleApiCall(int, unsigned long*, v8::internal::Isolate*) [npm]
 9: 0x13a72b9  [npm]

如何让它在后台运行并避免记录重启错误?

How can I leave it running in the background and avoid logging restart errors?

推荐答案

我从然后,我可以使用Control-C(^ C)停止服务器而不会出错,或者使用以下命令重新启动它并使它在后台运行:

Then I can stop the server without errors with Control-C (^C) or restart it and leave it running in the background with:

kill -s SIGINT $(lsof -i :8080 | grep .node.bin | cut -d " " -f 2); cd $HOME/server; cp config.prod.js config.js; NODE_ENV=production npm start  >> stdout.md 2>> stderr.md &

在生产中,服务器在8443上侦听SSL,并且我有一台小型服务器,它将8080上的所有内容重定向到8443,因此我需要停止两个服务器,否则由于一个端口正在使用,我无法重新启动:

In production, the server listens on 8443 for SSL and I have a small server that redirects everything on 8080 to 8443, so I need to stop both servers or else I cannot restart because one port is in use:

let server, httpServer;

const https = require('https');
// SSL configuration
const credentials = {key: sslPrivateKey, cert: sslCertificate};
const httpsServer = https.createServer(credentials, app);
  
server = httpsServer.listen(8443);

// Also create a small server to redirect HTTP to HTTPS.

const http = express();

// Set up a route to redirect http to https.
http.get('*', function(req, res) {  
  res.redirect('https://' + req.headers.host + req.url);
});

// Have it listen on 8080.
httpServer = http.listen(8080);

process.on("SIGINT", () => {
  // Close main server and HTTP redirection server.
  server.close();
  httpServer.close();
  mongoose.connection.close();
  console.log("SIGINT received, server stopped");
});

我将端口8080和8443用于安全性,因为1000以下的端口(HTTP为80,HTTPS为443)是保留端口.请参阅侦听大多数端口时的Node.js EACCES错误.因此,我没有赋予节点服务器root特权,而是运行以下shell命令将流量重定向到这些端口上的其他端口:

I use ports 8080 and 8443 for security because ports under 1000 (80 for HTTP and 443 for HTTPS) are reserved ports. See Node.js EACCES error when listening on most ports. So instead of giving the node server root privileges, I run these shell commands to redirect traffic to on those ports to other ports:

sudo iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
sudo iptables -t nat -D PREROUTING -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 8443

这篇关于如何避免重启NodeJS服务器时出现日志错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆