【问题标题】:No data from socket activation with systemd没有来自 systemd 套接字激活的数据
【发布时间】:2014-04-25 06:40:48
【问题描述】:

我有两个小的 systemd 套接字和服务文件:

套接字:

[Socket]
ListenStream=80
# also tried with Accept=no

[Install]
WantedBy=sockets.target

服务:

[Unit]
Description=Test Webserver

[Service]
EnvironmentFile=/path/to/node-script.env
ExecStart=/path/to/node /path/to/server.js
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=node-script
Type=simple
User=test-user
Group=test-user

[Install]
WantedBy=sockets.target

环境:

PORT=80

我有一个很小的 ​​Node.js 脚本:

var http = require('http');
var server = http.createServer();
server.on('connection', function(conn) {
  // socket connection
  console.log('new connection', conn.address());
  conn.on('data', function(data) {
    console.log('more data', data.length);
  });
}).on('request', function(req, res) {
  // a request!
  console.log('new request', req.headers);
}).listen({fd: 3}, function() {
  console.log('listening on provided descriptor', server.address());
});

如果我启动它:

$ systemctl start node-script.socket
$ curl localhost

Curl 就坐在那里。如果我尝试将 netcat 数据直接插入套接字,也是一样。检查日志,我注意到服务器建立了连接,但从未收到任何数据或请求。

listening on provided descriptor { address: '::', family: 'IPv6', port: 80 }
new connection { address: '::1', family: 'IPv6', port: 80 }

数据去哪儿了?

【问题讨论】:

  • 如果您从命令行启动应用程序,/path/to/node /path/to/server.js 是否按预期工作?
  • 是的,当然。从那以后,我一直在解决我的问题,但我不记得是如何解决的。

标签: node.js systemd


【解决方案1】:

如果其他人遇到此问题:在 systemd [Service] 部分,您需要添加此行:

NonBlocking=True

虽然这只对旧版本是必需的; node.js 被修复为在 0.12 系列的某个地方设置非阻塞标志本身。

【讨论】:

    猜你喜欢
    • 2021-09-19
    • 2021-09-05
    • 1970-01-01
    • 2014-09-07
    • 1970-01-01
    • 1970-01-01
    • 2020-10-02
    • 2017-05-12
    • 1970-01-01
    相关资源
    最近更新 更多