Reputation: 11
this is my first real problem with node. I have recently converted my website to https. This is great but my site can only be accessed by "https://www.example.com". Before converting I used port 80 (The default port), and basic express routing. I used to be able to connect to the ip address of my server and typing the domain name "www.example.com". I have the DNS set to redirect to the ip address of the server with ipv4 and ipv6. After switching to https I am no longer able to access the server using the ip address and the port 443 (I host on port 443). I am wanting to know why I can not access my website using the ip address + the port number (123.456.78.90:443) and why I must be so specific when getting at my website using https://www.example.com and not just www.example.com.
express = require('express');
app = express();
var http = require('https')
var fs = require('fs')
var sslPath = '/etc/letsencrypt/live/www.example.site/'
var options = {
key: fs.readFileSync(sslPath + 'privkey.pem'),
cert: fs.readFileSync(sslPath + 'fullchain.pem')
}
app.use(express.static("public"));
app.set("view engine", "ejs");
app.get("/", function(req,res){
console.log("Someone Connected")
res.render("homepage");
});
server = http.createServer(options, app)
io = require('socket.io').listen(server)
server.listen(443)
Upvotes: 0
Views: 324
Reputation: 708036
If your server only accepts https
, then you HAVE to specify https://
in the URL. That's the only way the browser knows that is the protocol you intend to use. When you leave out the https://
from a URL in the url bar of the browser, the browser assumes http
, no matter what port number you specify.
You could make your server respond to http on port 80 and automatically forward to https. Then, you could type www.example.com
in the browser and it would automatically redirect to https://www.example.com
.
Your server will never respond to 123.456.78.90:443
which the browser will turn into http://123.456.78.90:443
because on port 443, you're listening for https
connections, not http
connections (you can't listen for both on the same port).
Using the auto-redirect logic described about, you could type 123.456.78.90
into the browser and it would redirect to https://123.456.78.90
which would work.
You can either implement the auto-redirect yourself (set up a simple http server listening on port 80 and redirect all incoming requests to the same URL, but with https in front of it). Or, many hosting providers offer this feature built into their infrastructure (built into proxies or load balancers).
Upvotes: 3