我在Heroku上部署了Express服务器:https://server.mydomain.com
同时,我还在Heroku上部署了一个Next.js React应用程序:https://app.mydomain.com
两者的SSL证书都是由Heroku自动配置的,当我访问https域时,它们按预期工作。
我的问题是当我访问http://app.mydomain.com时,它不会重定向到https://app.mydomain.com。
我在网上找到的所有解决方案都指向在服务器上强制使用SSL:
- 这个流行的问题建议检查
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上强制使用https来处理子域名Next.js React客户端应用程序?而不使用express服务器方法?