nginx - grpc_send_timeout 不起作用,Nginx 意外关闭 GRPC 流

标签 nginx grpc nginx-config grpc-go

各位! 我有一个 TLS NGINX 服务器的配置,它代理流(双向/单向)到我的 golang GRPC 服务器。我在 NGINX conf(服务器上下文)中使用参数:

grpc_read_timeout 7d;
grpc_send_timeout 7d;

但是!我的双向流在 60 秒后关闭(频繁从服务器发送数据,在 60 秒内不从客户端发送任何数据),就好像 grpc_send_timeout 设置为默认值(60 秒)

但是!如果我每 20 秒从客户端发送回声请求,它就可以正常工作!

我不知道为什么 grpc_send_timeout 不起作用!

nginx.conf:

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log debug;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    resolver 127.0.0.1 valid=10s;
    resolver_timeout 10s;

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;
}

conf.d/my.service.conf

server {
    listen 443 ssl http2;
    ssl_certificate     my-cert.crt;
    ssl_certificate_key my-key.key;

    access_log "/var/log/nginx/my.service.access.log" main;
    error_log "/var/log/nginx/my.service.error.log" debug;

    grpc_set_header x-real-ip $remote_addr;
    grpc_set_header x-ray-id $request_id;
    grpc_read_timeout 7d;
    grpc_send_timeout 7d; // why it does not work?????

    location /MyGoPackage.MyService {
        grpc_pass grpc://my.service.host:4321;
    }
}

nginx 日志:

/ # cat /var/log/nginx/my_host_access.log 
59.932 192.168.176.1 - - [06/May/2021:14:57:30 +0000] "POST /MyGoPackege.MyService/MyStreamEndpoint HTTP/2.0" 200 1860 "-" "grpc-go/1.29.1" "-"

客户端日志(带有 GRPC 调试日志)

2021-05-06T17:56:30.609+0300    DEBUG   grpc_mobile_client/main.go:39   open connection {"address": "localhost:443"}
INFO: 2021/05/06 17:56:30 parsed scheme: ""
INFO: 2021/05/06 17:56:30 scheme "" not registered, fallback to default scheme
INFO: 2021/05/06 17:56:30 ccResolverWrapper: sending update to cc: {[{localhost:443  <nil> 0 <nil>}] <nil> <nil>}
INFO: 2021/05/06 17:56:30 ClientConn switching balancer to "pick_first"
INFO: 2021/05/06 17:56:30 Channel switches to new LB policy "pick_first"
INFO: 2021/05/06 17:56:30 Subchannel Connectivity change to CONNECTING
INFO: 2021/05/06 17:56:30 Subchannel picks a new address "localhost:443" to connect
INFO: 2021/05/06 17:56:30 pickfirstBalancer: HandleSubConnStateChange: 0xc0004b2d60, {CONNECTING <nil>}
INFO: 2021/05/06 17:56:30 Channel Connectivity change to CONNECTING
INFO: 2021/05/06 17:56:30 Subchannel Connectivity change to READY
INFO: 2021/05/06 17:56:30 pickfirstBalancer: HandleSubConnStateChange: 0xc0004b2d60, {READY <nil>}
INFO: 2021/05/06 17:56:30 Channel Connectivity change to READY
2021-05-06T17:56:30.628+0300    DEBUG  main.go:54   open stream      {"address": localhost:443"}
2021-05-06T17:56:30.974+0300    INFO   main.go:81   new msg from server     {"msg": "hello world"}

// some logs within a 60s

2021-05-06T17:57:30.567+0300    FATAL  main.go:79   receive new msg from stream     {"error": "rpc error: code = Internal desc = stream terminated by RST_STREAM with error code: PROTOCOL_ERROR"}

服务器日志(连接关闭时只有这个,GRPC 调试日志):

INFO: 2021/05/06 17:57:30 transport: loopyWriter.run returning. connection error: desc = "transport is closing"

最佳答案

client_header_timeout 7d;
client_body_timeout 7d;

将此参数添加到 nginx conf 解决了问题

关于nginx - grpc_send_timeout 不起作用,Nginx 意外关闭 GRPC 流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67430437/

相关文章:

ssl - 您能否终止 SSL 以提供自定义错误页面,但在传递到目标服务器之前重新加密?

Nginx 重定向可以在浏览器上工作,但是当使用curl 时,它只输出 301 moving permanent

http - 合并两个 nginx 服务器

docker - 在Docker中使用Fluentd获取Nginx日志

java - "UNAVAILABLE"从android客户端到python服务器的gRPC失败

ssl - 使用 hyperledger-fabric-client 使用 grpcs 和中间证书调用链代码

go - JSON 对我来说比 Protobuf/gRPC 快得多,Go 作为服务器,PHP 作为客户端

docker - 在子路径上带有nginx的piwik docker镜像

nginx - 在 nginx 中拒绝所有站点的用户代理的最佳位置

phpMyAdmin 只有在/etc/php.ini 不存在时才有效