Unable to configure nginx as local proxy for https + grpc-web

198 views Asked by At

I'm trying to connect grpc web client (browser) to nodejs @grpc/grpc-js server. I configured local certificates, serving web app from https and from alias host (https://sferadel.local) My nginx config (I'm running project via Nix devshell)

start-nginx.exec = let
  nginxConf = pkgs.writeText "nginx.conf" ''
    user root root;
    pid ${DEVENV_STATE}/nginx/nginx.pid;
    error_log ${DEVENV_STATE}/nginx/error.log debug;
    daemon off;
    events {
    }
    http {
      access_log ${DEVENV_STATE}/nginx/access.log;
      client_body_temp_path ${DEVENV_STATE}/nginx/;
      proxy_temp_path ${DEVENV_STATE}/nginx/;
      fastcgi_temp_path ${DEVENV_STATE}/nginx/;
      scgi_temp_path ${DEVENV_STATE}/nginx/;
      uwsgi_temp_path ${DEVENV_STATE}/nginx/;
      server {
        server_name sferadel.local;
        listen 3001;
        listen 443 ssl;
        #listen 443 quic reuseport;
        http2 on;
        ssl_certificate ${DEVENV_STATE}/mkcert/sferadel.local+1.pem;
        ssl_certificate_key ${DEVENV_STATE}/mkcert/sferadel.local+1-key.pem;

        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
        add_header 'Access-Control-Allow-Headers' 'keep-alive,user-agent,cache-control,content-type,content-transfer-encoding,custom-header-1,x-accept-content-transfer-encoding,x-accept-response-streaming,x-user-agent,x-grpc-web,grpc-timeout';
        if ($request_method != 'POST') {
          return 404;
        }
        location / {
          grpc_socket_keepalive on;
          grpc_intercept_errors on;
          grpc_set_header X-Real-IP $remote_addr;
          grpc_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          grpc_pass grpc://localhost:3002;
        }
      }
    }
  '';
in "sudo ${pkgs.nginxQuic}/bin/nginx -c ${nginxConf}";

My grpc server:

const server = new grpc.Server({
   // "grpc.enable_http_proxy": 1,
   // "grpc.ssl_target_name_override": "sferadel.local",
   // "grpc-node.tls_enable_trace": 1,
})
server.addService(...adaptService(EchoService, new Echo()))
server.bindAsync(
   "0.0.0.0:3002",
   grpc.ServerCredentials.createSsl(
      fs.readFileSync("./.devenv/state/mkcert/rootCA.pem"),
      [
         {
            cert_chain: fs.readFileSync("./.devenv/state/mkcert/sferadel.local+1.pem"),
            private_key: fs.readFileSync("./.devenv/state/mkcert/sferadel.local+1-key.pem")
         }
      ],
      true
   ),
   (err, port) => {
      console.log(`Server bound on port: ${port}`)
      server.start()
   }
)

Client code:

const transport = new GrpcWebFetchTransport({ baseUrl: "https://sferadel.local:3001", format: "binary" })
new EchoServiceClient(transport).echo({ message: "Hello echo!" })

Nginx error I get:

2023/10/24 19:28:29 [info] 966188#966188: *7 client sent invalid method while reading client request line, client: 127.0.0.1, server: sferadel.local, request: "

What am I doing wrong? enter image description here

0

There are 0 answers