【问题标题】:Bad gRPC response. HTTP status code: 502 on https with ngnix. working fine on local httpBad gRPC response. HTTP status code: 502 on https with ngnix. working fine on local http
【发布时间】:2022-12-27 06:41:02
【问题描述】:

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();

【问题讨论】:

    标签: asp.net-core grpc nginx-reverse-proxy


    【解决方案1】:

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

    【讨论】:

    • Just for reference, could you update your answer to include the config with the grpc_pass - I have the same error myself and using grpc_pass. Nice to have a working example to compare too. Thanks!
    • @TravisWhidden solved the problem with grpc_pass? I have a problem too)
    【解决方案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;
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-19
      • 2013-06-05
      • 2016-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多