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?
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
.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With