skip to Main Content

I have deployed a sample grpc service on my ubuntu server with .net core 3.1. I am able to connect using a plain HTTP URL but when trying to access it via reverse proxy I am getting a Bad grpc response error

my ngnix setting is like

server {
    listen 80;
    server_name abc.def.net;
         location / {
            proxy_pass      http://10.10.10.10:8086/;
            proxy_next_upstream error http_502;
            proxy_redirect     off;
            server_tokens off;
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
            client_max_body_size       25m;
            client_body_buffer_size    256k;

            proxy_connect_timeout     180;
            proxy_send_timeout        180;
            proxy_read_timeout        180;
            proxy_buffer_size          8k;
            proxy_buffers              8 32k;
            proxy_busy_buffers_size    64k;
            proxy_temp_file_write_size 64k;
            proxy_buffering on;

        access_log  /var/log/nginx/abc.def.net_access_log ;
        error_log  /var/log/nginx/abc.def.net_error_log notice;
        }
}

My code for accessing the grpc service is like

var serverAddress = "https://abc.def.net/";

// var serverAddress = "http://10.10.10.10:8086/";
//AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);


var channel = GrpcChannel.ForAddress(serverAddress);
var client = new CreditRatingCheck.CreditRatingCheckClient(channel);
var creditRequest = new CreditRequest { CustomerId = "id0201", Credit = 7000 };
var reply = client.CheckCreditRequest(creditRequest);

Console.WriteLine($"Credit for customer {creditRequest.CustomerId} {(reply.IsAccepted ? "approved" : "rejected")}!");
Console.WriteLine("Press any key to exit...");
Console.ReadKey();

2

Answers


  1. Chosen as BEST ANSWER

    Problem was in nginx file. we have to use grpc_pass instead of proxy_pass


  2. Only adding a config example how we used it (per the request of one of the above commenter)

    In this,

    • We are using variables just so nginx doesn’t fail if the container is offline
    • We detect if its a grpc content type and redirect it to the correct internal service
    • We are using a subpath here, so we had to re-write the URL inside so dotnet would handle the request properly (it doesnt map right without that rewrite under a sub path)
    • Replace [serviceName] with the sub path if needed, or remove if not used.
      location /[serviceName]/ {
        set $internal_service http://[containerName]:6009;
        set $internal_grpc_service grpcs://[containerName]:5009;
        if ($content_type = 'application/grpc' ){
            rewrite ^/[serviceName]/(.*) /$1 break;
            grpc_pass $internal_grpc_service;
        }
        proxy_set_header X-Forwarded-Prefix /[serviceName];
        proxy_pass $internal_service;
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search