Home > Mobile >  Nginx ignoring proxy_bind directive for gRPC server
Nginx ignoring proxy_bind directive for gRPC server

Time:05-27

I am trying to use the nginx proxy_bind directive to have upstream traffic to a gRPC server use a specific network interface. For some reason, nginx seems to be completely ignoring the directive and just using the default network interface. I tried using the proxy_bind directive for a different server that doesn't use gRPC (it is http1.1 I believe) and that worked fine, so I am led to believe that nginx is ignoring the proxy_bind directive because of something related to the server being a gRPC server. I have confirmed that it works for the normal server and not the gRPC server by running ss and looking for traffic originating from the ip I am trying to bind. There was traffic, but it was only ever going to the normal server. All traffic to the gRPC server had the default local ip.

This is the server config block for the gRPC server where proxy_bind is not working:

server {
    listen 8980 http2;

    # set client body size to 128M to prevent HTTP 413 errors
    client_max_body_size 128M;
    # set client buffer size to 128M to prevent writing temp files
    client_body_buffer_size 128M;

    # We have plenty of RAM, up the output_buffers
    output_buffers 256 128M;

    # Allow plenty of connections
    http2_max_concurrent_streams 100000;
    keepalive_requests 100000;
    keepalive_timeout 120s;

    # Be forgiving with grpc
    grpc_connect_timeout 240;
    grpc_read_timeout 2048;
    grpc_send_timeout 2048;
    grpc_socket_keepalive on;

    proxy_bind <local ip>;

    location / {
        proxy_set_header Host $host;
        grpc_pass grpc://my8980;
    }
}

and this is the server config block for a normal server where proxy_bind is working:

server {
    listen 4646;

    # set client body size to 16M to prevent HTTP 413 errors
    client_max_body_size 64M;
    # set client buffer size to 32M to prevent writing temp files
    client_body_buffer_size 64M;

    # We have plenty of RAM, up the output_buffers
    output_buffers 64 64M;

    # Fix Access-Control-Allow-Origin header
    proxy_hide_header Access-Control-Allow-Origin;
    add_header Access-Control-Allow-Origin $http_origin;
        
    proxy_bind <local ip>;

    location / {
        proxy_set_header Host $host;
        proxy_pass http://myapp1;
        proxy_buffering off;
    }
}

CodePudding user response:

The grpc_pass directive belongs to ngx_http_grpc_module, while proxy_set_header, proxy_hide_header and proxy_bind directives comes from ngx_http_proxy_module. Those modules are two different things. The ngx_http_grpc_module has its own grpc_set_header, grpc_hide_header and grpc_bind analogs to be used instead.

  • Related