Node.js https服务器:无法侦听端口443 – 为什么?

我在Node中第一次创build了一个HTTPS服务器,代码(见下文)适用于像6643这样的随机端口,但是在端口443上不起作用。 我得到这个错误:

[Debug][Server]: Initialized... [Debug][Control Center]: Application initialized... events.js:72 throw er; // Unhandled 'error' event ^ Error: listen EACCES at errnoException (net.js:904:11) at Server._listen2 (net.js:1023:19) at listen (net.js:1064:10) at Server.listen (net.js:1138:5) at Object.module.exports.router (/home/ec2-user/Officeball/Versions/officeball_v0.0.5/server/custom_modules/server.js:52:5) at Object.<anonymous> (/home/ec2-user/Officeball/Versions/officeball_v0.0.5/server/control_center.js:15:59) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) 

这是在Amazon Linux EC2服务器上。 这是我的理解是,一旦我将我的域的DNS名称logging设置为服务器的IP,当用户searchhttps://mydomain.com时 ,浏览器将在端口443查找我的服务器的IP,这应该是HTTPS的标准端口交通。

所以我的理解是,我需要通过端口443提供https内容。

我究竟做错了什么?


这是我的服务器代码:

control_center.js(init)

 /* Control Center */ //DEFINE GLOBALS preloaded = {}; //GET DIRECT WORKING PATH var dirPath = process.cwd(); //REQUIRE CUSTOM MODULES var debug = new (require(dirPath + "/custom_modules/debug"))("Control Center"); var socket = require(dirPath + "/custom_modules/socket")(4546); // ! this is the relevant line var server = require(dirPath + "/custom_modules/server").router(443); //APP INITIALIZE debug.log("Application initialized..."); 

server.js

 /* Server */ //REQUIRE NPM MODULES var fs = require('fs'), https = require('https'), url = require('url'), path = require('path'); //GET DIRECT WORKING PATH var dirPath = process.cwd(); //REQUIRE CUSTOM MODULES //Snip! var debug = new (require(dirPath + "/custom_modules/debug"))("Server"); //Preload requests var preload = require(dirPath + '/custom_modules/preload').init(); //INIT MODULE debug.log("Initialized..."); //DEFINE MODULE VARIABLES var options = { key: fs.readFileSync('SSL/evisiion_private_key.pem'), cert: fs.readFileSync('SSL/evisiion_ssl_cert.pem') }; //LISTEN FOR PATH REQUESTS //route requests to server module.exports.router = function(port) { https.createServer(options, function(req, res) { //Snip! }).listen(port); }; 

在Linux上(我相信大多数其他类Unix操作系统),服务必须以root身份运行,才能绑定到小于1024的端口。

我刚刚在一个Node应用程序中对它进行了validation,当我将端口从5000更改为443时,我看到完全相同的错误,线条相同,禁止文件path。

在开发过程中,大多数人都会在更高编号的端口(如8080)上运行开发服务器。在生产中,您可能希望使用适当的Web服务器(如Nginx)来为静态内容提供服务,并将所有其他代码反向代理到Node应用程序,这使得Nginx可以非常高兴地以root身份运行,从而减less了问题。

编辑:因为你的用例需要服务一些静态内容,那么你可能想使用一个Web服务器,如Nginx或Apache来处理静态文件,并反向代理到另一个端口为您的dynamic内容。 Nginx的反向代理非常简单 – 下面是一个示例configuration文件:

 server { listen 443; server_name example.com; client_max_body_size 50M; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; location /static { root /var/www/mysite; } location / { proxy_pass http://127.0.0.1:8000; } } 

这假定你的web应用程序可以在端口443上访问,并在端口8000上运行。如果该位置与/ static文件夹相匹配,它将从/ var / www / mysite / static提供。 否则,Nginx将把它交给运行在8000端口的应用程序,这个应用程序可能是一个Node.js应用程序,或者一个Python,或者其他的。

这也相当整齐地解决了你的问题,因为应用程序可以在端口443上访问,而不必实际绑定到该端口。