簡體   English   中英

將HTTP重定向到https express.js

[英]Redirect http to https express.js

我正在嘗試在我的Express應用中將http (80)重新路由到https (443) 我正在使用一些中間件來執行此操作。 如果我轉到https://my-example-domain.com ,一切都很好。 但是,如果我轉到http://my-example-domain.com它不會重定向,也不會顯示任何內容。

我還在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);

所以我的問題是我是否只需要添加另一個iptables規則? 還是我需要在我的應用程序中配置某些內容?

因此,根據下面的一個答案,我認為這不是中間件問題,而是端口問題。 例如:如果我轉到http://my-example-domain.com ,則不起作用。 但是,如果我添加了端口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);

您只需要在您的Express應用中同時收聽http和https。 然后包括中間件以在不安全的情況下重新路由。 然后添加一個iptable重新路由443 =>8443。完成。

這應該工作。

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

我使用的是類似的解決方案,因為我的SSL證書沒有有效,因此我還會在其前面加上“ www”。 在Firefox上的所有瀏覽器中都能正常工作。 任何的想法?

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

您可以實現從http到https的重定向

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

這對我來說很好。 首先安裝express-force-ssl。 這將強制服務器僅使用安全連接

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