构建 NestJS 应用程序我想通过 https 路由所有传入流量,而不会给用户带来不便。
目前我知道的有两种方式,都不符合我的要求。
为 http 和 https 设置两个服务器,然后重定向每个路由/api 端点的流量,这确实不是 DRY,也不是最佳实践。 文档重定向
仅创建 https 服务器,用户总是被迫手动输入我不想要的 https 地址。 文档 https
理想情况下,我会假设一个解决方案,其中检查 https 并强制在某人第一次访问服务器时只需键入
example.com
。我认为这最好在我的 NestJS 应用程序的 main.ts
中完成。
对于我的用例,我认为没有理由在节点 http 服务器功能齐全的情况下使用反向代理层来使服务器膨胀。由于问题与 NestJS 有关,这里我使用 Nest 中间件提出简单的本机解决方案。当然,您还必须遵循有关托管两个服务器的 NestJS 文档,这又相当简单。
import { HttpStatus, Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response } from "express";
@Injectable()
export class HttpsRedirectMiddleware implements NestMiddleware
{
use(req: Request, res: Response, next: () => void)
{
if (!req.secure)
{
const httpsUrl = `https://${req.hostname}${req.originalUrl}`;
res.redirect(HttpStatus.PERMANENT_REDIRECT, httpsUrl);
}
else
{
next();
}
}
}
我们只是询问请求对象连接是否安全,如果不安全,我们会促使浏览器永久重定向到相同的 URL,但这次前缀为
https://
。
然后,上面的中间件类将在 configure()
的 AppModule
方法中注册所有路由。
configure(consumer: MiddlewareConsumer)
{
consumer.apply(HttpsRedirectMiddleware).forRoutes("*");
}
对于生产版本,您可能会使用 nginx。 Nginx 将监听端口 80 并重定向到 NestJS 端口。此解决方案的优点是可以轻松重定向到 https。在您的配置中,您可以放置类似这样的内容
server {
listen 80;
server_name example1.com example2.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name example1.com example2.com;
...
}
因此每个http请求都会重定向到https。并且您的应用程序不必关心 http 请求,因为每个请求之前都会被重定向。
npm install express-sslify --save
然后,在您的
main.ts
文件中:
import enforce from 'express-sslify';
async bootstrap() {
// create your app
// Must be the FIRST middleware applied
// Must add in the 'trustProtoHeader' option
// (I only add this for production, but you can do whatever suits you)
if (process.env.NODE_ENV === 'production') {
app.use(enforce.HTTPS({ trustProtoHeader: true });
}
// other stuff
}
如果您的 NestJS 应用程序托管在 Heroku 上,则您不能在 HTTP 和 HTTPS 端口上运行 2 个服务器。您也不能简单地重定向到
https
,因为 Heorku 通过 HTTP 流量从其面向外部的负载均衡器内部发送数据,并且应用程序将无限重定向。
请参阅 Heroku 文档 和 express-sslify 文档,尤其是有关 Heroku 的部分。