开发者

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

开发者 https://www.devze.com 2023-03-28 22:43 出处:网络
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

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开发者_如何学JAVA 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号