node http proxy port number to accept SSL traffic - node.js

Following example is taken from the github page of node-http-proxy
HTTPS -> HTTP
//
// Create the HTTPS proxy server in front of a HTTP server
//
httpProxy.createServer({
target: {
host: 'localhost',
port: 9009
},
ssl: {
key: fs.readFileSync('valid-ssl-key.pem', 'utf8'),
cert: fs.readFileSync('valid-ssl-cert.pem', 'utf8')
}
}).listen(8009);
Question: Why is httpProxy not listening to port 443 for secure SSL traffic?

SSL has default port which is 443 but like with regular HTTP protocol which also has default 80 port it could be bind to a custom port and accessed by specifying a port in the url(https://localhost:8009). Based on this answer.

Related

How do you write an SSL redirect in NodeJS running on AWS EC2 without using port 80 (http) or port 43 (https)?

I have two node servers on a single host. One HTTP server with the responsibility of redirecting to HTTPS, and one HTTPS server responsible for serving my application:
const express = require('express');
const https = require('https');
const http = require('http')
const fs = require('fs');
const app = express();
const httpsOptions = {
key: fs.readFileSync('./local-ssl/key.pem'),
cert: fs.readFileSync('./local-ssl/cert.pem'),
passphrase: '*****'
}
//other stuff
http.createServer(function (req, res) {
res.writeHead(301, { "Location": "https://" + req.headers['host'] + req.url });
res.end();
}).listen(80);
https.createServer(httpsOptions, app).listen(443)
This works great locally.
The issue is, when I deploy this code and run these servers on AWS EC2 you cannot start servers on ports 80 and 443. I am trying to figure out how I can get around this issue. If I run them on different ports, the servers will not respond, and worse, redirect incorrectly.
Example:
If I serve HTTP on 8081 and HTTPS on 8443, when a redirect occurs, the code redirects to
https://my-fun-url.com:8081
which of course does not work because I am not responding to HTTPS on port 8081.
Now, I've explored the option of port forwarding, but how would this work? If I forward ports 80 and 443 to internal ports (let's say) 3000 and 4000 the same redirection problem will occur.
I have scoured the internet for so long and to me this is a simple requirement for any web-app. I would very much appreciate some detailed guidance on my strategy.
If you want to keep ports 8081 and 8443, then you simply replace 8081 with 8443 in the host header:
httpsHost = req.headers.host.replace('8081', '8443');
res.writeHead(301, {
"Location": "https://" + httpsHost + req.url
});
Now, I've explored the option of port forwarding, but how would this work? If I forward ports 80 and 443 to internal ports (let's say) 3000 and 4000 the same redirection problem will occur.
Not exactly. When someone navigates to http://my-fun-url.com (80) the request is forwarded to 3000. Your http server will respond with a redirect to https://my-fun-url.com (443) which will be forwarded to 4000, and the https server will take it from there.
The difference between the two methods is that with ports 80 and 443 being the default, they are implied and therefore can be left out from the host part of the URL. Which makes the redirect easier as there's no port in the host to replace in the first place, just the protocol part (HTTP/HTTPS).

Why do I have to type the ":80" in https://localhost:80 for my website to load?

I recently secured my website on node.js to use https instead of just plain http. However, once I did this, I realized that I had to type out the :80 suffix if I wanted to get to my website to load. Why is this? Doesn't chrome default to port 80 and shouldn't https://localhost suffice?
const port = 80;
https.createServer({
key: fs.readFileSync('./private/ssl/server.key'),
cert: fs.readFileSync('./private/ssl/server.cert')
}, app)
.listen(port, function () {
console.log('Server running on port: ' + port);
});
app.get('/', (req, res) => {
res.sendFile('index.html', { root: path.join(__dirname, './') });
});
app.use(express.static('./public'));```
The default port for HTTPS is 443, not 80.
Be aware that HTTPS uses port 443 by default, and that's probably the source of your confusion.
If you specify both https and :80 in your browser's address bar, you are making an HTTPS request to port 80, which is unusual.
What kind of reply you will be getting depends on your server's configuration.

BrowserSync: How can I serve both HTTP and HTTPS?

Can BrowserSync serve both HTTP and HTTPS on different ports?
const bs = require('browser-sync').create();
bs.init({
server: "./app",
port: 8080
https: false,
....
});
According to documentation there is an option for switching protocol to HTTPS. I would like to have both of them: an HTTP on 8080 port and an HTTPS on 8443 port.

NodeJS Express SSL not working with Bitnami EC2 Instance

I am running my Bitnami Amazon EC2 VPC App via NodeJS using Express (Apache is disabled) and am having trouble getting SSL to work. I have implemented SSL using this implementation: https://www.npmjs.org/package/ssl-root-cas. I purchased the SSL Certificate from Symantec. The app runs HTTP/S servers on ports 8080/8443 and I port forward each to 80/443. The app is run via nohup. I have enabled inbound/outbound https traffic in my security group on EC2.
Here is the sample implementation:
require("ssl-root-cas/latest")
.inject()
.addFile(__dirname + "/ssl/IntermediateCA.cer")
.addFile(__dirname + "/ssl/crossRootCA.cer");
var server = {
http: {
port: process.env.PORT || 8080
},
https: {
port: 8443,
options: {
key: fs.readFileSync("./ssl/server.key"),
cert: fs.readFileSync("./ssl/ssl_certificate.cer")
}
}
};
https.createServer(server.https.options, app).listen(server.https.port);
http.createServer(app).listen(server.http.port);
For whatever reason, SSL is still not working. Suggestions??

How to forward request to Node.js from nginx with tcp_proxy_module?

Now, I had patched nginx with the nginx_tcp_proxy_module, and it is running OK on port 8080.
How do I connect the clients to port 80 of nignx, not port 8080 of Node.js,
so that having the nginx forward the request to Node.js?
Just change 8080 to 80. But TCP and HTTP on the same port is not possible.
Aleternative solution:
Use HAProxy on port 80
Set up nginx to listen on port 81
Run your node.js app on port 8080
Configure HAProxy to
forward Host: your.nodejs.socketio.com to 127.0.0.1:8080
forward everything else to 127.0.0.1:81
If you go down this route you will probably want to preserve client IPs:
Configure HAproxy
Use RealIP module in nginx
Use X-Forwarded-For in socket.io
socketio.handshakeData = function(data) {
var d = socketio.Manager.prototype.handshakeData(data);
d.ip = data.request.headers['x-forwarded-for'] || data.request.connection.remoteAddress;
return d;
};

Resources