Heroku NodeJS http 到 https ssl 强制重定向
我有一个应用程序在 Heroku 上启动并运行,并在 Node.js 上使用 Express.js 和 https
。如何在 Heroku 上使用 Node.js 识别强制重定向到 https
的协议?
我的应用程序只是一个简单的 http
服务器,它(还)没有意识到 Heroku 正在向它发送 https
-请求:
// Heroku provides the port they want you on in this environment variable (hint: it's not 80)
app.listen(process.env.PORT || 3000);
I have an application up and running on Heroku with Express.js on Node.js with https
. How do I identify the protocol to force a redirect to https
with Node.js on Heroku?
My app is just a simple http
-server, it doesn't (yet) realize Heroku is sending it https
-requests:
// Heroku provides the port they want you on in this environment variable (hint: it's not 80)
app.listen(process.env.PORT || 3000);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
截至今天,2014 年 10 月 10 日,使用 Heroku Cedar stack 和 ExpressJS ~3.4.4,这里是一组工作代码。
这里要记住的主要事情是我们正在部署到 Heroku。在加密流量到达您的节点应用程序之前,SSL 终止发生在负载均衡器处。可以使用 req.headers['x-forwarded-proto'] === 'https' 来测试 https 是否用于发出请求。
我们不需要像在其他环境中托管一样关心应用程序内是否有本地 SSL 证书。但是,如果使用您自己的证书、子域等,您应该首先通过 Heroku 附加组件应用 SSL 附加组件。
然后只需添加以下内容即可从 HTTPS 以外的任何内容重定向到 HTTPS。
这与上面接受的答案非常接近,但是:
代码:
SailsJS (0.10.x) 用户注意事项。您可以简单地在 api/policies 中创建一个策略 (enforceSsl.js):
然后从 config/policies.js 中引用以及任何其他策略,例如:
As of today, 10th October 2014, using Heroku Cedar stack, and ExpressJS ~3.4.4, here is a working set of code.
The main thing to remember here is that we ARE deploying to Heroku. SSL termination happens at the load balancer, before encrypted traffic reaches your node app. It is possible to test whether https was used to make the request with req.headers['x-forwarded-proto'] === 'https'.
We don't need to concern ourselves with having local SSL certificates inside the app etc as you might if hosting in other environments. However, you should get a SSL Add-On applied via Heroku Add-ons first if using your own certificate, sub-domains etc.
Then just add the following to do the redirect from anything other than HTTPS to HTTPS.
This is very close to the accepted answer above, but:
Code:
Note for SailsJS (0.10.x) users. You can simply create a policy (enforceSsl.js) inside api/policies:
Then reference from config/policies.js along with any other policies, e.g:
答案是使用 Heroku 转发的“x-forwarded-proto”标头,因为它是代理 thingamabob。 (旁注:它们还传递了其他几个可能很方便的 x- 变量,检查它们出)。
我的代码:
谢谢布兰登,只是在等待 6 小时的延迟,这让我无法回答自己的问题。
The answer is to use the header of 'x-forwarded-proto' that Heroku passes forward as it does it's proxy thingamabob. (side note: They pass several other x- variables too that may be handy, check them out).
My code:
Thanks Brandon, was just waiting for that 6 hour delay thing that wouldn't let me answer my own question.
接受的答案中有一个硬编码的域,如果您在多个域上有相同的代码(例如:dev-yourapp.com、test-yourapp.com、yourapp.com),则不太好。
请改用:
https://blog.mako.ai/2016/03/30/redirect-http-to-https-on-heroku-and-node-generally/
The accepted answer has a hardcoded domain in it, which isn't too good if you have the same code on several domains (eg: dev-yourapp.com, test-yourapp.com, yourapp.com).
Use this instead:
https://blog.mako.ai/2016/03/30/redirect-http-to-https-on-heroku-and-node-generally/
我编写了一个小型节点模块,可以在 Express 项目上强制执行 SSL。它适用于标准情况和反向代理(Heroku、nodejitsu 等)
https://github.com/florianheinemann/快速sslify
I've written a small node module that enforces SSL on express projects. It works both in standard situations and in case of reverse proxies (Heroku, nodejitsu, etc.)
https://github.com/florianheinemann/express-sslify
如果您想在本地主机上测试
x-forwarded-proto
标头,您可以使用 nginx 设置一个虚拟主机文件,该文件代理对节点应用程序的所有请求。你的 nginx vhost 配置文件可能看起来像这样NginX
这里重要的一点是你将所有请求代理到本地主机端口 3000(这是你的节点应用程序运行的地方)并且你正在设置一堆标头,包括 X- Forwarded-Proto
然后在你的应用程序中像往常一样检测该标头
Express
Koa
Hosts
最后你必须将此行添加到你的
hosts
文件中If you want to test out the
x-forwarded-proto
header on your localhost, you can use nginx to setup a vhost file that proxies all of the requests to your node app. Your nginx vhost config file might look like thisNginX
The important bits here are that you are proxying all requests to localhost port 3000 (this is where your node app is running) and you are setting up a bunch of headers including
X-Forwarded-Proto
Then in your app detect that header as usual
Express
Koa
Hosts
Finally you have to add this line to your
hosts
file您应该查看 heroku-ssl-redirect。它就像一个魅力!
You should take a look at heroku-ssl-redirect. It works like a charm!
如果您将 cloudflare.com 与 heroku 结合使用作为 CDN,您可以在 cloudflare 中轻松启用自动 ssl 重定向,如下所示:
登录并转到您的仪表板
选择页面规则
If you are using cloudflare.com as CDN in combination with heroku, you can enable automatic ssl redirect within cloudflare easily like this:
Login and go to your dashboard
Select Page Rules
Loopback 用户可以使用 arcseldon 答案的稍微修改版本作为中间件:
server/middleware/forcessl.js
server/server.js
Loopback users can use a slightly adapted version of arcseldon answer as middleware:
server/middleware/forcessl.js
server/server.js
这是一种更 Express 特定的方法来执行此操作。
This is a more Express specific way to do this.
我正在使用 Vue、Heroku 并遇到了同样的问题:
我更新了我的 server.js,如下所示,我不再碰它,因为它正在工作:):
I am using Vue, Heroku and had same problem :
I updated my server.js as below, and i am not touching it anymore because it is working :) :
使用 app.use 和动态 url。对我来说既可以在本地工作,也可以在 Heroku 上工作
With app.use and dynamic url. Works both localy and on Heroku for me
正如 Derek 指出的那样,检查 X-Forwarded-Proto 标头中的协议在 Heroku 上运行良好。无论如何,这里是我使用的 Express 中间件及其相应测试的要点。
Checking the protocol in the X-Forwarded-Proto header works fine on Heroku, just like Derek has pointed out. For what it's worth, here is a gist of the Express middleware that I use and its corresponding test.