网关Nginx做了如下配置,发现代码中拿不到真实IP
proxy_pass http://yth-app;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
拿到的IP如下:
{
"referer": "http://test.10.1.1.122.nip.io:31729/public/workplace",
"authorization": "bearer ",
"x-forwarded-host": "test.10.1.1.122.nip.io:31729",
"tenantid": "1",
"Content-Length": "0",
"X-Real-IP": "10.233.126.40",
"x-request-id": "4919240d-38e2-460b-aba9-610fddea491e",
"accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
"X-Forwarded-Proto": "http",
"Connection": "close",
"Host": "yth-app",
"x-forwarded-for": "127.0.0.1",
"accept": "*/*",
"x-original-uri": "/sys/navigation/userNav",
"x-envoy-expected-rq-timeout-ms": "15000",
"x-scheme": "http",
"accept-encoding": "gzip, deflate",
"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36"
}
发现 “X-Real-IP”: “10.233.126.40” 和 “x-forwarded-for”: “127.0.0.1” 都没拿到真是IP,而 10.233.126.40 对应的是 kubesphere router 的IP地址,如下: