这几天一直在为此苦苦挣扎。我有一个 Angular 9 应用程序、一个子域 (subdomain.side.com) 和一个运行 Nodejs 服务器的 Heroku 应用程序。Heroku 应用程序具有 SSL 并通过 DNS 正确连接到域面板,而且 heroku 正在运行一个简单的服务器来启动应用程序。
问题
我想将用户重定向到我的子域的 HTTPS,例如,如果用户键入http://subdomain.side.com,它将被重定向到 HTTPS 站点(https://subdomain.side.com)。
尝试
- 添加了 .htaccess 文件。也将其添加到 prod 文件夹中。
RewriteEngine On RewriteCond %{HTTPS} !=on RewriteCond %{HTTP:X-Forwarded-Proto} !https RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
- 配置了许多不同的设置,最新的 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);