我试图让网站强制使用HTTPS(从HTTP重定向)。我们已通过AWS Elastic Beanstalk设置HTTPS。问题是,目前,HTTP和HTTPS都可以使用。在Elastic Beanstalk上使用Node/Express重定向到HTTPS
阅读了几个帖子后,包括this one,下面的代码就是我想到的。不幸的是,这是行不通的。
我错过了什么?
import express from 'express';
import { join } from 'path';
const app = express();
const buildPath = join(`${__dirname}/../build`);
const enforceHTTPS = (req, res, next) => {
if (req.headers['x-forwarded-proto'] === 'https') return next();
else return res.redirect(301, join(`https://${req.hostname}${req.url}`));
};
app.use(express.static(buildPath));
app.use(enforceHTTPS);
app.get('*', (req, res) => res.sendFile(`${buildPath}/index.html`));
app.listen(process.env.PORT || 3000,() => console.log('Server running on port 3000!'));
export default app;
我想你可以代理所有HTTPS使用AWS请求HTTP在您的节点结束。保持节点在http上运行。否则,请按照建议与https一起使用https服务器。 – DrEarnest