GRPC反向代理混淆了GRPC和GRPC-Web。

huangapple go评论89阅读模式
英文:

GRPC reverse proxy confusing GRPC and GRPC-Web

问题

我有一个反向代理。在这里,我将反向代理到 api.example.comgrpc.example.com:443。我的 api 域名可以正常工作,但是当我向 grpc.example.com:443 发送请求时,grpc 将其视为 grpc-web,并将请求发送到 grpc.example.com:443/Hello.HelloService/Greeter

   creds := credentials.NewTLS(&tls.Config{
		InsecureSkipVerify: true,
	})

	opts := []grpc.DialOption{
		grpc.WithTransportCredentials(creds),
		grpc.WithBlock(),
	}
	conn, err := grpc.DialContext(context.Background(), 
net.JoinHostPort("grpc.example.com", "443"), opts...)
	if err != nil {
		log.Fatalf("Could not connect to grpc: %v", err)
	}

grpc 反向代理:

grpcHeader := c.Request.Header.Get("Content-Type")
	grpcserver := grpc.Server{}
	if grpcHeader == "application/grpc" || grpcHeader == "application/grpc+json" || grpcHeader == "application/grpc+proto" {
		grpcserver.ServeHTTP(c.Writer, c.Request)
	}
英文:

I have a reverse proxy. Here I reverse proxy to api.example.com and grpc.example.com:443 . My api domain is working, but when I make a request to grpc.example.com:443, grpc perceives it as grpc-web and sends a request as grpc.example.com:443/Hello.HelloService/Greeter.

   creds := credentials.NewTLS(&tls.Config{
		InsecureSkipVerify: true,
	})

	opts := []grpc.DialOption{
		grpc.WithTransportCredentials(creds),
		grpc.WithBlock(),
	}
	conn, err := grpc.DialContext(context.Background(), 
net.JoinHostPort("grpc.example.com", "443"), opts...)
	if err != nil {
		log.Fatalf("Could not connect to grpc: %v", err)
	}

grpc reverse proxy:

grpcHeader := c.Request.Header.Get("Content-Type")
	grpcserver := grpc.Server{}
	if grpcHeader == "application/grpc" || grpcHeader == "application/grpc+json" || grpcHeader == "application/grpc+proto" {
		grpcserver.ServeHTTP(c.Writer, c.Request)
	}

答案1

得分: 1

根据评论,gRPC通常在HTTP/2上运行(但也有其他选项)。

HTTP/2请求路径的格式是"/" Service-Name "/" {method name}。这意味着看到一个请求grpc.example.com:443/Hello.HelloService/Greeter是正常的(并不表示正在使用gRPC-Web)。

英文:

As per the comments gRPC generally runs over HTTP/2 (but there are other options).

The format of the HTTP/2 request path is "/" Service-Name "/" {method name}. This means that seeing a request come in for grpc.example.com:443/Hello.HelloService/Greeter is normal (and not an indication that gRPC-Web is in use).

huangapple
  • 本文由 发表于 2022年6月29日 04:24:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/72792750.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定