繁体   English   中英

将HTTP重定向到https(本地主机除外)

[英]redirect http to https except in localhost

我已将我的node.js应用程序移至https。 我通过nginx反向代理设置https。 但是,我有一个问题,当我输入example.com时,它不会重定向到https://example.com 所以我尝试在我的app.js中编写这段代码

app.get('*', function(req, res, next) {
  if (req.headers.host.indexOf('localhost') > -1 || req.secure) {
    return next();
  }else{
    res.redirect('https://'+req.hostname+req.url);
  }
});

重新部署时不起作用,有任何线索吗?

如果您使用app express模块​​,并且想要获得'/'响应,则请检查以下术语以进行简单案例检查:

app.get('/', function(req, res, next) {
  if (req.headers.host.indexOf('localhost') > -1 || req.secure) {
    return next();
  }else{
    res.redirect('https://google.com');
  }
});

检查它的代码。 它完全运行,然后,如果您想监听安全服务器,则使用exress以安全方式使用createServer

var secureServer = require('https').createServer(httpsOptions, app);

httpsOptions数据是您的证书文件和密钥文件

暂无
暂无

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

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