0

这几天一直在为此苦苦挣扎。我有一个 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);
4

1 回答 1

0

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

  if (location.protocol === 'http:') {
      window.location.href = location.href.replace('http', 'https');
   }
于 2020-07-18T00:23:38.220 回答