【问题标题】:Getting ERR_INVALID_HTTP_RESPONSE while making grpc-web request to local server在向本地服务器发出 grpc-web 请求时获取 ERR_INVALID_HTTP_RESPONSE
【发布时间】:2020-10-05 13:36:41
【问题描述】:

我正在一个角度应用程序中尝试 grpc-web hello world 程序,通过 nginx 访问本地 python 服务器,我在浏览器中收到 ERR_INVALID_HTTP_RESPONSE 错误。我认为该请求甚至没有命中 nginx 代理。虽然我可以通过 python 客户端直接和通过代理访问服务器。

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{}

任何指针我该如何解决这个问题?谢谢

【问题讨论】:

    标签: nginx grpc grpc-python grpc-web


    【解决方案1】:

    我认为nginx 不支持 grpc-web(在通常情况下,即使可能有一些步骤您可以尝试使其工作,请参阅https://github.com/grpc/grpc-web/issues/381#issuecomment-439783765)。它只支持 grpc,这就是 python 客户端工作的原因。对于 grpc-web 你可以使用envoy,在这里查看配置:https://github.com/grpc/grpc-web/blob/master/net/grpc/gateway/examples/echo/envoy.yaml

    【讨论】:

    • 感谢@dmaixner,我会尝试更新的配置。但是,如果是由于代理,它不应该在 nginx 访问/错误日志中报告吗?我在那里什么也没看到。
    猜你喜欢
    • 2021-10-25
    • 2022-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多