Node.js https server: Cannot listen on port 443 - Why?

This is the first time I am creating an HTTPS server in Node, and the code (see below) works for a random port such as 6643, but it will not work on port 443. I get this error:

[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) 

This is on an Amazon Linux EC2 server. I understand that as soon as I set my DNS domain DNS address to the server IP address, when the user searches for https://mydomain.com , the browser will look for my server IP address on port 443, which is supposedly the standard port for HTTPS traffic.

So, I understand that I need to serve https content through port 443.

What am I doing wrong?


Here is my server code:

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); }; 
+6
source share
1 answer

On Linux (and, I believe, most other Unix-like operating systems), the service must be run as root in order to be able to bind to a port with a number less than 1024.

I just checked it in the Node application I was lying in and I saw exactly the same error: a line for a line that did not indicate the file path when I changed the port from 5000 to 443.

During development, most users will run the dev server at a higher number, such as 8080. During production, you may want to use a proper web server, such as Nginx, to serve static content and a reverse proxy. Node, which makes it less problematic, since Nginx can very well be run as root.

EDITING. Since static content is required for your use case, you can use a web server such as Nginx or Apache to handle static files, and a reverse proxy for another port for your dynamic content. Reverse proxying is pretty simple with Nginx - here's an example configuration file:

 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; } } 

This assumes that your web application must be accessible on port 443 and run on port 8000. If the location matches the / static folder, it is served from / var / www / mysite / static. Otherwise, Nginx passes it to the running application on port 8000, which can be a Node.js application, or with Python, or any other.

It also quite decisively solves your problem, since the application will be available on port 443, without having to bind to that port.

+24
source

Source: https://habr.com/ru/post/977249/


All Articles