我们可以在不依赖 docker 的情况下使用 nginx 代理进行 gRPC-web 集成吗?

kar*_*hi 4 grpc-web

需要在没有 docker 的情况下使用 NGINX

我尝试使用具有 docker 依赖关系的 envoy 代理使用 gRPC-web 集成,所以我转移到 NGINX,如何在没有 docker 依赖关系的情况下使用 NGINX?

Vin*_*avi 6

由于没有对grpc-webfrom 的直接支持nginx,我们可以在 nginx 的配置文件中进行以下修改,以同时处理grpc-webgrpc调用。

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

上面的配置基于内容类型机制工作,每当grpc-web调用 nginx 时,内容类型将是application/grpc-web并且此内容类型不被 nginx 处理grpc_pass

因此,只有那些具有内容类型的请求可以application/grpc+(proto|json|customType)使用,grpc_pass而其余的请求可以使用proxy_pass.


小智 1

您可以查看 dockerfile 的作用,并且基本上在 docker 映像之外自己完成:https://github.com/grpc/grpc-web/blob/master/net/grpc/gateway/docker/nginx/Dockerfile

主要的事情基本上是运行make standalone-proxy,并将其运行为./gConnector_static/nginx.sh. 您将需要一个nginx.conf配置文件来指定 Nginx 应在何处接收和转发 gRPC-Web 请求