【发布时间】:2019-11-13 01:29:17
【问题描述】:
我在 Heroku 上部署了一个 express 服务器:https://server.mydomain.com
还有一个 Next.js React 应用也部署在 Heroku 上:https://app.mydomain.com
它们的 SSL 证书都由 Heroku 自动配置,当我访问 https 域时,它们按预期工作。
我遇到的问题是当我访问http://app.mydomain.com时,它没有重定向到https://app.mydomain.com。
我在网上找到的所有解决方案都指向在服务器上强制使用 SSL:
-
this popular question 表示要检查
x-forwarded-proto值:
/* At the top, with other redirect methods before other routes */
app.get('*',function(req,res,next){
if(req.headers['x-forwarded-proto']!='https')
res.redirect('https://app.mydomain.com'+req.url)
else
next() /* Continue to other routes if we're not redirecting */
})
- 和其他人建议使用express-sslify 或heroku-ssl-redirect 之类的包。
这些解决方案适用于服务器请求,但加载 React 客户端页面不一定会触发 app.get()。显然,React 客户端可以独立于服务器运行。
所以问题是: 有人如何为 Heroku 上的子域 Next.js React 客户端应用强制 https?不使用快速服务器方法?
【问题讨论】:
-
你在为你的测功机付钱吗?
-
我有两个应用程序部署到 Heroku,一个在付费帐户上,一个在非付费帐户上。付费的会自动重新路由到 https,而免费的则不会。
-
是的。两者都是付费的。
-
你的 Procfile 是什么样的?
-
睡在上面,
Obviously, a React client can run independently of a server.伸出来。客户端需要服务器,heroku 很可能通过节点服务器(我认为这是默认的 Web 服务器)为您的静态文件夹提供服务。您需要做的是构建一个 express 应用程序来提供您的静态构建文件,并有一个 Procfile 告诉 heroku 针对该 express 应用程序而不是直接针对您的 react 应用程序运行节点。
标签: reactjs ssl heroku https next.js