2

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)
    }
  • Not really sure what you mean by "grpc perceives"; it looks like you have shown us your client code but details of the proxy are probably more relevant (as that will be what is directing your request to the wrong backend service). – Brits Jun 28 '22 at 23:45
  • I added the grpc reverse proxy code block. I mean, when I give a domain name to grpc, it perceives that request as a grpc-web request and sends a request accordingly. – Mansur Berbero Jun 29 '22 at 07:42
  • 1
    Sorry I'm still struggling to understand the issue; what led you to conclude that the request is being "perceived" as grpc-web? If it's the fact that you are seeing an http request then that probably because gRPC runs over HTTP2 (and the [format of the request path](https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md) is `"/" Service-Name "/" {method name}`). – Brits Jun 29 '22 at 10:20
  • I got it now. Sorry, I always think of GRPC as a different protocol. Thanks for your help – Mansur Berbero Jun 29 '22 at 10:40

1 Answers1

1

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).

Brits
  • 14,829
  • 2
  • 18
  • 31
  • Understood. But now I am getting an error. When I searched for this error, I couldn't find anything. Error : `rpc error: code = Unknown desc = malformed header: missing HTTP content-type` ` request: &{POST /Hello.GreetService/Greet HTTP/2.0 2 0 map[Accept-Encoding:[gzip] Content-Length:[108] Content-Type:[application/grpc] Te:[trailers] User-Agent:[grpc-go/1.47.0] 0xc000658990 108 [] false map[] map[] map[] 10.0.0.2:9890 /Hello.GreetService/Greet 0xc0000db4a0 0xc000656300} ` – Mansur Berbero Jun 30 '22 at 09:21
  • There is not really enough info in your question to answer this one. As it's really a different question I'd suggest you create a new post; ideally with a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) because it's difficult to work out the cause without the full context (and please don't post long errors etc in the comments; edit the question). – Brits Jun 30 '22 at 19:28