これは基本的にmaericsからの回答への追加ですが、コード例を追加したいので、セキュリティの問題を防ぐ方法を追加情報とともに別の回答を書きます。
通常、Web サーバーは 1024 未満のポートにバインドする必要があるため、root として開始されますが、実行するユーザーを特権のないユーザーに変更します。
を使用して node.js でこれを行うことができますprocess.setuid("username")
。
次の例では、サーバーを起動し、root として起動したときにポート 80 にバインドした後、ユーザー「www-data」に root 権限をドロップします。
function StartServer() {
console.log("Starting server...");
// Initalizations such as reading the config file, etc.
server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
});
try {
server.listen(80, "0.0.0.0", function(){
process.setuid("www-data");
});
}
catch(err) {
console.error("Error: [%s] Call: [%s]", err.message, err.syscall);
process.exit(1);
}
}