Redirect http to https express.js

I am trying to redirect http (80)in https (443)to my express application. For this, I use medium dishes. If I go to https://my-example-domain.com, everything will be all right. But if I go to http://my-example-domain.com, it will not redirect and nothing will appear.

I also have some iptables installed on my ubuntu server

sudo iptables -A INPUT -i eth0 -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -i eth0 -p tcp --dport 443 -j ACCEPT
sudo iptables -A INPUT -i eth0 -p tcp --dport 8443 -j ACCEPT
sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 8443




function requireHTTPS(req, res, next) {
  if (!req.secure) {
    return res.redirect('https://' + req.headers.host + req.url);
  }
  next();
}

// all environments
app.set('port', process.env.PORT || 8443);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.logger('dev'));
app.use(requireHTTPS);  // redirect to https
app.use(express.json());
app.use(express.urlencoded());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));

app.get('/', function(req, res){
    res.render('index');
})

https.createServer(options, app).listen(8443);

So my question is: do I just need to add another rule iptables? Or do I need to configure something in my application?

, , , , . : http://my-example-domain.com, . 8443, http://my-example-domain.com:8443, .

+4
6
var redirectApp = express () ,
redirectServer = http.createServer(redirectApp);

redirectApp.use(function requireHTTPS(req, res, next) {
  if (!req.secure) {
    return res.redirect('https://' + req.headers.host + req.url);
  }
  next();
})

redirectServer.listen(8080);
+6

http, https -. , . iptable 443 = > 8443. .

+4

.

app.use(function(req,resp,next){
    if (req.headers['x-forwarded-proto'] == 'http') {
        return resp.redirect(301, 'https://' + req.headers.host + '/');
    } else {
        return next();
    }
});
+1

I use a similar solution where I also add "www" because our SSL certificate is not valid without it. Works fine in every browser, but Firefox. Any idea?

http.createServer(function(req, res) {
  res.writeHead(301, {
    Location: "https://www." + req.headers["host"].replace("www.", "") + req.url
  });
  res.end();
}).listen(80);
0
source

You can redirect from http to https

if(req.headers["x-forwarded-proto"] == "http") {
 res.redirect(301, "https://" + req.host+req.url);
                next();
}
0
source

It worked for me. Install express-force-ssl first. this will force the server to use only a secure connection

const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const http = require('http');
const app = express();
var forceSsl = require('express-force-ssl');
var request = require('request');
//For https
const https = require('https');
var fs = require('fs');
var options = {
  key: fs.readFileSync('certificates/private.key'),
  cert: fs.readFileSync('certificates/certificate.crt'),
  ca: fs.readFileSync('certificates/ca_bundle.crt')
};

// API file for interacting with MongoDB
const api = require('./server/routes/api');

// Parsers
app.use(forceSsl);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));


// Angular DIST output folder
app.use(express.static(path.join(__dirname, 'dist')));

// API location
app.use('/api', api);

app.get('*', (req, res) => {
  //alert(req);
  res.sendFile(path.join(__dirname, 'dist/index.html'));
});


http.createServer(app).listen(80)
https.createServer(options, app).listen(443);
0
source

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


All Articles