【问题标题】:Nest.js GRPC client gets RST_STREAM with code 2 when connecting to server behind NGINX with SSL enabledNest.js GRPC 客户端在启用 SSL 连接到 NGINX 后面的服务器时使用代码 2 获取 RST_STREAM
【发布时间】:2022-02-20 19:20:40
【问题描述】:

我们有 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'

值得注意的是,这两个微服务不与内部 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
}

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

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

还尝试将grpc.example.com:443 作为 URL 并且发生了同样的情况。

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

【问题讨论】:

    标签: nginx ssl nestjs grpc reverse-proxy


    【解决方案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;
    }
    

    【讨论】:

    • 能否详细说明参数?我应该生成一个 rootCert 吗?难道不应该在不生成任何东西的情况下访问 SSL 站点吗?像 HTTPS 一样,还是 grpcurl 正在做的任何方式?
    猜你喜欢
    • 2020-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-30
    • 2017-08-08
    • 1970-01-01
    • 2017-03-07
    • 2017-09-20
    相关资源
    最近更新 更多