【问题标题】:How to avoid logging errors from restarting NodeJS server如何避免重新启动 NodeJS 服务器导致的日志记录错误
【发布时间】:2021-02-09 00:30:07
【问题描述】:

我在 AWS 虚拟机上使用 NodeJS 作为 Web 服务器,监听 8443 并让它运行:

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

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

/opt/bitnami/nodejs/bin/.node.bin[31215]: ../src/node.cc:663:void node::ResetStdio(): 断言 `(0) == (err)' 失败。 1: 0x9ef190 节点::Abort() [/opt/bitnami/nodejs/bin/.node.bin] 2:0x9ef217 [/opt/bitnami/nodejs/bin/.node.bin] 3: 0x9bd657 节点::ResetStdio() [/opt/bitnami/nodejs/bin/.node.bin] 4: 0x9bd6c0 节点::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 节点::NodeMainInstance::Run() [/opt/bitnami/nodejs/bin/.node.bin] 10: 0x9c1cc8 节点::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] 中止(核心转储) npm 错误!代码生命周期 npm 错误!错误号 134 npm 错误! EmotionAthletes@0.0.1 开始:`node app.js` npm 错误!退出状态 134 npm 错误! npm 错误! EmotionAthletes@0.0.1 启动脚本失败。 npm 错误!这可能不是 npm 的问题。上面可能有额外的日志输出。 npm 错误!可以在以下位置找到此运行的完整日志: npm 错误! /home/bitnami/.npm/_logs/2020-10-25T12_30_23_826Z-debug.log npm[31199]: ../src/node.cc:663:void node::ResetStdio(): 断言 `(0) == (err)' 失败。 1: 0x9ef190 节点::Abort() [npm] 2:0x9ef217 [npm] 3: 0x9bd657 节点::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]

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

【问题讨论】:

    标签: node.js amazon-web-services logging npm


    【解决方案1】:

    我从Difference in NodeJS termination errors得到了答案:

    const express = require('express');
    const http = require('http');
    
    const app = express();
    const httpServer = http.createServer(app);
    let server = httpServer.listen(8080);
    
    process.on("SIGINT", () => {
    
      // Close main server and HTTP redirection server.
      server.close();
      mongoose.connection.close();
      console.log("SIGINT received, server stopped");
    });
    

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

    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,因此我需要停止两台服务器,否则我无法重新启动,因为一个端口正在使用中:

    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 error when listening on most ports。因此,我没有给节点服务器 root 权限,而是运行这些 shell 命令来将这些端口上的流量重定向到其他端口:

    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
    

    【讨论】:

      猜你喜欢
      • 2017-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-03
      • 2023-03-20
      • 1970-01-01
      • 2012-06-27
      相关资源
      最近更新 更多