Getting ERR_INVALID_HTTP_RESPONSE while making grpc-web request to local server

2.1k views Asked by At

I am trying grpc-web hello world program in an angular application, hitting a local python server via nginx and I am getting ERR_INVALID_HTTP_RESPONSE error in browser. I don't think the request is even hitting the nginx proxy. Though I am able to hit the server via a python client both directly and via proxy.

helloworld.proto

syntax = "proto3";

option go_package = "google.golang.org/grpc/examples/helloworld/helloworld";
option java_multiple_files = true;
option java_package = "io.grpc.examples.helloworld";
option java_outer_classname = "HelloWorldProto";

package helloworld;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
  string name = 1;
}

// The response message containing the greetings
message HelloReply {
  string message = 1;
}

client.ts

  const greeterService = new GreeterClient(
      'http://localhost:8086',
      null,
      null
    );

    const request = new HelloRequest();
    request.setName('Hello World!');

    const call = greeterService.sayHello(
      request,
      { 'custom-header-1': 'value1' },
      (err: grpcWeb.Error, response: HelloReply) => {
        console.log(response.getMessage());
      }
    );
    call.on('status', (status: grpcWeb.Status) => {
      console.log(status);
    });

python server.py

class Greeter(helloworld_pb2_grpc.GreeterServicer):

    def SayHello(self, request, context):
        return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)


def serve():
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
    server.add_insecure_port('[::]:50052')
    server.start()
    server.wait_for_termination()


if __name__ == '__main__':
    logging.basicConfig()
    serve()

nginx.conf

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent"';

     server {
        listen 8086 http2;
 
        access_log logs/access.log main;
 
        location /helloworld.Greeter {
            grpc_pass grpc://localhost:50052;
        }
    }
    
}

events{}

Error faced

Any pointers how can I resolve this? thanks

1

There are 1 answers

1
dmaixner On

I think nginx doesn't support grpc-web (under usual circumstances, even though there might be some steps you can try to make it work, see https://github.com/grpc/grpc-web/issues/381#issuecomment-439783765). It only supports grpc, that's why python client is working. For grpc-web you can use envoy, see configuration here: https://github.com/grpc/grpc-web/blob/master/net/grpc/gateway/examples/echo/envoy.yaml