简体   繁体   中英

How to get client IP address only in Django under nginx?

Hi i am trying to get the client IP address in django. My webserver is Nginx. so i am doing this in my nginx conf

fastcgi_param REMOTE_ADDR $remote_addr;

fastcgi_param X_FORWADRD_FOR $proxy_add_x_forwarded_for;

The problem is i have 4 server under a loadbalancer and when i call a site using a domain i get the ip of the server when using this(client_ip = request.META['REMOTE_ADDR']). But when i call the site using the ip address i get the client ip only

And when i use this (client_ip = request.META['X_FORWARDED_FOR']) i get the client ip and the ip of the server

How do i just get the Client ip alone

For now what i have done is

ipaddress = request.META['X_FORWADRD_FOR']

ipaddress = ipaddress.split(",")

ip_address = ipaddress[0]

to get the client ip alone

You could reintegrate the SetRemoteAddrFromForwardedFor middleware that was removed in Django 1.1 . This will dynamically reset request.META["REMOTE_ADDR"] to the client IP address.

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