【问题标题】:How to redirect to HTTPS from Angular+Node app hosted in Heroku from a subdomain?如何从子域中托管在 Heroku 中的 Angular+Node 应用程序重定向到 HTTPS?
【发布时间】:2020-11-06 15:24:13
【问题描述】:

已经为此苦苦挣扎了几天。 我有一个 Angular 9 应用程序、一个子域 (subdomain.side.com) 和一个运行 Nodejs 服务器的 Heroku 应用程序。 Heroku 应用程序具有 SSL 并通过 DNS 正确连接到域面板,而且 heroku 正在运行一个简单的服务器来启动应用程序。

问题

我想将用户重定向到我的子域的 HTTPS,例如,如果用户键入 http://subdomain.side.com,它将被重定向到 HTTPS 站点 (https://subdomain.side.com)。

尝试

  1. 添加了 .htaccess 文件。也将其添加到 prod 文件夹中。
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
  1. 最新的 server.js 文件配置了很多不同的设置:
const express = require('express');
const compression = require('compression');
const path = require('path');
const app = express();
app.use(compression());
app.use(express.static(__dirname + '/dist/APP'));

function ensureSecure(req, res, next) {
  if (req.headers['x-forwarded-proto'] === 'https') {
    return next();
  }
  res.redirect('https://' + req.hostname + req.url);
}

app.all('*', ensureSecure);
app.get('/*', function(req, res) {
  res.sendFile(path.join(__dirname + '/dist/APP/index.html'));
});
app.listen(process.env.PORT || 8080);

【问题讨论】:

    标签: node.js angularjs heroku swagger-node-express


    【解决方案1】:

    不是最漂亮的解决方案,但如果有人遇到同样的问题,这是一种解决方法。 我在根路由中添加了一个保护,检查页面是否不是 HTTPS,然后将其重定向到 HTTPS,不理想但是...

      if (location.protocol === 'http:') {
          window.location.href = location.href.replace('http', 'https');
       }
    

    【讨论】:

      猜你喜欢
      • 2020-03-19
      • 1970-01-01
      • 2014-08-20
      • 1970-01-01
      • 2018-07-22
      • 2021-02-26
      • 2018-08-21
      • 2013-08-27
      • 2018-05-12
      相关资源
      最近更新 更多