最近有需要用到nginx做grpc代理,所以记录下配置及遇到的问题,先发下配置:
server {
listen 8080 ssl http2;
server_name proxy.com;
resolver 8.8.8.8 ipv6=off;access_log logs/grpc_access_proxy.log;
ssl_certificate /path/xxx.pem;
ssl_certificate_key /path/xxx.key;location / {
grpc_pass grpcs://googleads.googleapis.com:443;
grpc_set_header Content-Type application/grpc;
}
}
nginx编译参数:
./configure --with-http_ssl_module --with-http_v2_module --add-dynamic-module=/opt/modules/ngx_http_proxy_connect_module-master --add-dynamic-module=/opt/modules/ngx_devel_kit-0.3.3 --add-dynamic-module=/opt/modules/set-misc-nginx-module-0.33 --add-dynamic-module=/opt/modules/echo-nginx-module-0.63
遇到的问题:
1. 遇到“connect() to [2404:6800:4003:c05::5f]:443 failed (101: Network is unreachable)”的nginx报错,说明这个ipv6的地址连接出问题了,可以将ipv6的dns解析关掉,在nginx的server块下加入“resolver 8.8.8.8 ipv6=off;”这样的配置。
2. 遇到“upstrean sent too large http2 frame: 1377025 while reading response header from upstream”,这问题我了解到有两种情况:
- 上游返回的http头确实太大了,这个时候可以修改nginx的配置即可(具体可以google);
- 你访问的是基于TLS的grpc服务,这个时候在location中可能就需要加上 grpcs 的转发,具体可以参考(GRPC(12)- 使用Nginx反向代理)这篇文章的介绍;