Grpc-go: gRPC server can't get remote client ip by "X-Real-IP" from header by reverse-proxy nginx?

Created on 24 Oct 2019  Â·  4Comments  Â·  Source: grpc/grpc-go

Please see the FAQ in our main README.md before submitting your issue.

What version of gRPC are you using?

google.golang.org/grpc v1.22.1

What version of Go are you using (go version)?

go version go1.12.5 linux/amd64

What operating system (Linux, Windows, …) and version?

Linux version 4.15.0-66-generic (buildd@lgw01-amd64-044) (gcc version 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)) #75-Ubuntu SMP Tue Oct 1 05:24:09 UTC 2019

What did you do?

nginx and gRPC server in one host whose IP(LAN) is 192.168.90.20
gRPC client in another host whose IP(LAN) is 192.168.90.24

(1). use nginx for gRPC server, config file(path: /etc/nginx/conf.d/gRPC.conf) nginx.conf is default, gRPC.conf content as follow:

upstream backend {
    server 127.0.0.1:9090;
    server 127.0.0.1:9091;
    server 127.0.0.1:9092;
}

server {
    listen  9099  http2;
    access_log    /var/log/nginx/access-grpc.log;
    location / {
        grpc_pass grpc://127.0.0.1:9091;
        #grpc_pass grpc://backend;

    }
}

(2). I will get client real ip in server side by code as follow:

//  peer is from package   "google.golang.org/grpc/peer"
if grpcPeer, ok := peer.FromContext(ctx); ok {
        if tcpAddr, ok := grpcPeer.Addr.(*net.TCPAddr); ok {
            externalIP := tcpAddr.IP
            grpclog.Warningf("get the external-ip=%s", externalIP)
            return externalIP
        }
    }

What did you expect to see?

// log in server side:
... get the external-ip=192.168.90.24

What did you see instead?

// log in nginx:
192.168.90.24 - - [07/Nov/2019:11:31:38 +0800] "POST /pb.Tracker/FindResource HTTP/2.0" 200 36 "-" "grpc-go/1.22.0"

// log in server side:
WARNING: 2019/11/07 11:36:08 get the external-ip=127.0.0.1
Question

Most helpful comment

I solved my problem as follow:

  1. Config "grpc_set_header X-Real-IP $remote_addr;"
    nginx for gPRC
server {
    listen  9099  http2;
    access_log    /var/log/nginx/access-grpc.log;
    location / {
        grpc_pass grpc://127.0.0.1:9091;
        grpc_set_header X-Real-IP $remote_addr;
    }
}
  1. Get metadata from context in gRPC server side
    my code:
md, ok := metadata.FromIncomingContext(ctx)
...
md.Get("x-real-ip")
...

All 4 comments

Could you please describe in more detail what you did, what you expected, and what you found instead.

This issue is labeled as requiring an update from the reporter, and no update has been received after 7 days. If no update is provided in the next 7 days, this issue will be automatically closed.

@easwars

I solved my problem as follow:

  1. Config "grpc_set_header X-Real-IP $remote_addr;"
    nginx for gPRC
server {
    listen  9099  http2;
    access_log    /var/log/nginx/access-grpc.log;
    location / {
        grpc_pass grpc://127.0.0.1:9091;
        grpc_set_header X-Real-IP $remote_addr;
    }
}
  1. Get metadata from context in gRPC server side
    my code:
md, ok := metadata.FromIncomingContext(ctx)
...
md.Get("x-real-ip")
...
Was this page helpful?
0 / 5 - 0 ratings

Related issues

23doors picture 23doors  Â·  3Comments

reterVision picture reterVision  Â·  3Comments

arcana261 picture arcana261  Â·  3Comments

jbrook picture jbrook  Â·  4Comments

HeinOldewageRetro picture HeinOldewageRetro  Â·  4Comments