简体   繁体   中英

Getting real ip from Google Cloud Load Balancer and cloudflare

I want to get my real ip I'm using cloudflare and Google Cloud Network Load Balancer.

I have put this source to nginx.conf

# Last updated Sat Jul 30 11:17:32 UTC 2016
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
set_real_ip_from 103.31.4.0/22;
set_real_ip_from 104.16.0.0/12;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 131.0.72.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 162.158.0.0/15;
set_real_ip_from 172.64.0.0/13;
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
set_real_ip_from 199.27.128.0/21;
set_real_ip_from 2400:cb00::/32;
set_real_ip_from 2405:8100::/32;
set_real_ip_from 2405:b500::/32;
set_real_ip_from 2606:4700::/32;
set_real_ip_from 2803:f800::/32;
set_real_ip_from 130.211.0.0/22; #Google IP
real_ip_header X-Forwarded-For; #CF-Connecting-IP;

To check my ip, I made simple rails source.

return render :json => {:ip => request.ip, :remote_ip => request.remote_ip, :x_forwarded_for => request.env['HTTP_X_FORWARDED_FOR'], :client_ip => request.env['HTTP_CLIENT_IP'], :cf => request.env['CF-Connecting-IP']}

.

{"ip":"130.211.0.145","remote_ip":"130.211.0.145","x_forwarded_for":"(REAL IP), 141.101.85.141, 130.211.16.204, 130.211.0.145","client_ip":null,"cf":null}

But ip was not my ip, it's Google Cloud LoadBalancer's. How do I fix this problem? I tried a lot of time over 6 hours.

Thanks.

The CloudFlare Real IP header is available in CF-Connecting-IP, whilst CloudFlare issues an X-Forwarded-For header, it looks like that header is immediately overwritten. In order to mitigate against this use this header instead.

In order to utilise it, be sure to ensure that your nginx configuration contains:

real_ip_header CF-Connecting-IP;

Then your application can yield the remote address from the REMOTE_ADDR environment variable in your application itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM