Get Client IP from AWS and Google Cloud Load Balancer in Nginx

Easy way to retrieve client IP from AWS ELB, Google Cloud Load Balancer and Cloudflare in Nginx web server.

Setting up a cloud load balancer is fun, but it introduces a set of challenges. One of the essential one is getting load balancer IP instead of real client IP.

Recently, I implemented Google Cloud load balancer for Geekflare services and when I looked into my Nginx access log files, it was showing internal load balancer IP.

34.8.44.48 2.854 HIT [09/Dec/2025:11:48:42 +0000] geekflare.com "GET /software/close-crm-review/ HTTP/1.1" 200 28594 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36"

This can happen to other cloud load balancers as well.

Are you in the same situation as I was?

Don’t worry, you can do the following small config to get the client IP in your Nginx.

Client IP from AWS ELB or Google Cloud LB

  • Login to your Nginx web server.
  • Go to the path where it’s installed (default location /etc/nginx)
  • Take a backup of nginx.conf file
  • Add the following in HTTP block
real_ip_header X-Forwarded-For;
set_real_ip_from 0.0.0.0/0;
  • Restart the Nginx, and you should see the visitor’s IP in your access.log file.

Nginx restart command

service nginx restart

If you are behind Cloudflare, then you will see their IP instead of the client’s IP, so you got to do the below as well.

Client IP from Cloudflare

If you are using Cloudflare CDN and getting Cloudflare IP instead of client IP, you can add the following configuration to fix the issue.

As a best practice, take a backup of nginx.conf file and add the following at the end in server block.

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/13;
set_real_ip_from 104.24.0.0/14;
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 2400:cb00::/32;
set_real_ip_from 2606:4700::/32;
set_real_ip_from 2803:f800::/32;
set_real_ip_from 2405:b500::/32;
set_real_ip_from 2405:8100::/32;
set_real_ip_from 2c0f:f248::/32;
set_real_ip_from 2a06:98c0::/29;
real_ip_header CF-Connecting-IP;

After restarting Nginx, you should be able to see the real IP.

Keep this official list of Cloudflare IPs handy to update the above list when they release a new IP range.

Thanks to Our Partners