我有两个小的 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 }
数据去哪儿了?
最佳答案
如果其他人遇到此问题:在 systemd [Service]
部分中,您需要添加以下行:
NonBlocking=True
尽管这只对于旧版本来说是必要的; node.js 已修复以在 0.12 系列中的某个位置设置非阻塞标志本身。
关于node.js - 使用 systemd 激活套接字时没有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22498753/