Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we use nginx proxy for gRPC-web integration, without docker dependency?

Tags:

grpc-web

Need to use NGINX without docker

I have tried using gRPC-web integration using the envoy proxy that had docker dependencies and so I moved to NGINX, how to use NGINX without docker dependencies?

like image 380
karthi Avatar asked Jan 26 '23 19:01

karthi


2 Answers

Since there is no direct support for grpc-web from nginx, we can make a below hack in the config file of the nginx to work with both grpc-web and grpc calls.

server {
    listen 1449 ssl http2;
    server_name `domain-name`;
    ssl_certificate `pem-file`; # managed by Certbot
    ssl_certificate_key `key-file`; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    location / {

    #
    ## Any request with the content-type application/grpc+(json|proto|customType) will not enter the
    ## if condition block and make a grpc_pass while rest of the requests enters into the if block
    ## and makes a proxy_prass request. Explicitly grpc-web will also enter the if block.
    #

    if ($content_type !~ 'application\/grpc(?!-web)(.*)'){
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,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,content-type,snet-current-block-number,snet-free-call-user-id,snet-payment-channel-signature-bin,snet-payment-type,x-grpc-web';
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain charset=UTF-8';
            add_header 'Content-Length' 0;
        proxy_pass http://reroute_url;
    }
    grpc_pass grpc://reroute_url;
    }
}

The above config works based on the content-type mechanism, whenever the grpc-web makes a call to nginx, the content-type would be application/grpc-web and this content-type is not been handled by nginx with grpc_pass.

Hence, only those requests with content-type application/grpc+(proto|json|customType) works with grpc_pass while rest of the requests will work with proxy_pass.

like image 183
Vinay Wadagavi Avatar answered Jun 08 '23 09:06

Vinay Wadagavi


You can look at what the dockerfile does and basically do it yourself outside of the docker image: https://github.com/grpc/grpc-web/blob/master/net/grpc/gateway/docker/nginx/Dockerfile

The main thing is basically to run make standalone-proxy, and run it as ./gConnector_static/nginx.sh. You will need a nginx.conf config file to specify where Nginx should receive and forward the gRPC-Web requests

like image 32
Stanley Cheung Avatar answered Jun 08 '23 10:06

Stanley Cheung