当连接到启用了 SSL 的 NGINX 后面的服务器时,Nest.js GRPC 客户端获取 RST_STREAM,代码为 2

S. *_*ini 5 ssl reverse-proxy nginx grpc nestjs

我们有 2 个微服务需要通过 GRPC 进行通信,它们都是用 Nestjs 编写的。这两种服务可以通过 IP 和端口轻松通信。

现在,当 GRPC 服务器位于启用了 SSL 的 Nginx 代理后面时(没有 SSL 的 Nginx 工作正常),就会出现问题。我尝试测试它grpcurl,请求到达 GRPC 服务器,但是当启用 SSL 时,用 Nestjs 编写的 GRPC 客户端无法通信,并出现以下错误:

code: 13,
details: 'Received RST_STREAM with code 2 triggered by internal client error: Protocol error'
Run Code Online (Sandbox Code Playgroud)

值得注意的是,这两个微服务不与内部 SSL 通信,Nginx 向 GRPC 服务器发送未加密的流量。

这是 Nginx 配置:

server {
    server_name grpc.example.com;
    error_log /var/log/nginx/grpc.log info;


    location / {
        grpc_pass grpc://127.0.0.1:5001;
    }

    listen 443 ssl http2;

    ssl_certificate /path/to/cert
    ssl_certificate_key /path/to/cert/key
}
Run Code Online (Sandbox Code Playgroud)

GRPC 客户端微服务使用此文档并获取如下 URL:

imports: [
  ClientsModule.register([
    {
      name: 'HERO_PACKAGE',
      transport: Transport.GRPC,
      options: {
        url: 'grpc.example.com'
        package: 'hero',
        protoPath: join(__dirname, 'hero/hero.proto'),
      },
    },
  ]),
];
Run Code Online (Sandbox Code Playgroud)

也尝试grpc.example.com:443作为 URL,同样的情况发生。

如果有任何线索可以帮助解决问题,我将不胜感激,并且如果需要,我很乐意提供更多详细信息。

小智 1

我的猜测是在 Nest 中注册 ClientsModule 时配置凭据。这是我认为你应该使用的功能。ChannelCredentials.createSsl您可以向它提供 rootCerts。

     * Return a new ChannelCredentials instance with a given set of credentials.
     * The resulting instance can be used to construct a Channel that communicates
     * over TLS.
     * @param rootCerts The root certificate data.
     * @param privateKey The client certificate private key, if available.
     * @param certChain The client certificate key chain, if available.
     */
    static createSsl(rootCerts?: Buffer | null, privateKey?: Buffer | null, certChain?: Buffer | null, verifyOptions?: VerifyOptions): ChannelCredentials;
}
Run Code Online (Sandbox Code Playgroud)