---
title: "nginx 接入gRPC"
author: "tcspecial"
date: "2019-02-05T11:14:16Z"
tags: []
categories: ["开源软件"]
slug: "nginx-grpc"
draft: false
lastmod: "2019-02-09T09:52:00Z"
id: 2437372
url: "https://tcspecial.iteye.com/blog/2437372"
---

nginx 1.13.10新增了对gRPC的支持，本文介绍通过nginx接入grpc服务。

 

一. nginx版本

```
$ nginx -V
nginx version: nginx/1.15.6
built by clang 10.0.0 (clang-1000.11.45.5)
built with OpenSSL 1.0.2p  14 Aug 2018
TLS SNI support enabled
--with-http_ssl_module --with-http_v2_module
```

 若源码安装，需指定http2编译选项：--with-http\_v2\_module

 

二. grpc负载均衡配置

```
#grpc服务列表
upstream grpcservs {
    server 127.0.0.1:80;
}

server {
    listen       8080 http2;	#配置http2
    server_name  localhost;

    #location配置
    location /helloworld.Greeter {
        grpc_pass grpc://grpcservs;
    }
}
```

 

三. 测试

设置greeter\_client 连接端口为8080。

$ ./greeter\_client

Greeter received: Hello world

 

nginx访问日志：

```
127.0.0.1 - - [05/Feb/2019:11:01:49 +0800] "POST /helloworld.Greeter/SayHello HTTP/2.0" 200 18 "-" "grpc-c++/1.18.0 grpc-c/7.0.0 (osx; chttp2; goose)" "-"
```

 

可见nginx配置生效，通过http2协议通信。

 

四. 抓包分析

![200 ok](http://dl2.iteye.com/upload/attachment/0131/5100/5f5a39e4-f6bc-3614-bf57-7f84fa7314da.png)