简体   繁体   English

将HTTP重定向到https express.js

[英]Redirect http to https express.js

I'm trying to reroute http (80) to https (443) in my express app. 我正在尝试在我的Express应用中将http (80)重新路由到https (443) I'm using some middle ware to do this. 我正在使用一些中间件来执行此操作。 If i go to my https://my-example-domain.com , everything is fine. 如果我转到https://my-example-domain.com ,一切都很好。 But if I go to http://my-example-domain.com it does not redirect and nothing shows up. 但是,如果我转到http://my-example-domain.com它不会重定向,也不会显示任何内容。

I also have setup some iptables on my ubuntu server 我还在ubuntu服务器上设置了一些iptables

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 iptables rule? 所以我的问题是我是否只需要添加另一个iptables规则? Or do I need to configure something in my app? 还是我需要在我的应用程序中配置某些内容?

So based on one answer below, I don't think its a middleware problem, but a port issue. 因此,根据下面的一个答案,我认为这不是中间件问题,而是端口问题。 For example: if i go to http://my-example-domain.com , doesn't work. 例如:如果我转到http://my-example-domain.com ,则不起作用。 But if I add port 8443, http://my-example-domain.com:8443 , it redirects fine. 但是,如果我添加了端口8443, http: //my-example-domain.com:8443,它将重定向正常。

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

You just need to listen to both http and https in your express app. 您只需要在您的Express应用中同时收听http和https。 Then include the middleware to reroute if unsecure. 然后包括中间件以在不安全的情况下重新路由。 Then add an iptable to reroute 443 => 8443. Done. 然后添加一个iptable重新路由443 =>8443。完成。

This should work. 这应该工作。

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

I'm using a similar solution, where I also prepend 'www' because our SSL certificate is not valid without it. 我使用的是类似的解决方案,因为我的SSL证书没有有效,因此我还会在其前面加上“ www”。 Works fine in every browser, but Firefox. 在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);

You can achieve redirection from http to https 您可以实现从http到https的重定向

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

This worked fine for me. 这对我来说很好。 First install express-force-ssl. 首先安装express-force-ssl。 this will force server to use secured connection only 这将强制服务器仅使用安全连接

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM