[英]How to get client IP address only in Django under nginx?
Hi i am trying to get the client IP address in django.嗨,我正在尝试在 django 中获取客户端 IP 地址。 My webserver is Nginx.我的网络服务器是 Nginx。 so i am doing this in my nginx conf所以我在我的 nginx conf 中这样做
fastcgi_param REMOTE_ADDR $remote_addr; fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param X_FORWADRD_FOR $proxy_add_x_forwarded_for; 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']).问题是我在负载均衡器下有 4 台服务器,当我使用域调用站点时,我得到了服务器的 ip(client_ip = request.META['REMOTE_ADDR'])。 But when i call the site using the ip address i get the client ip only但是当我使用 ip 地址调用该站点时,我只得到客户端 ip
And when i use this (client_ip = request.META['X_FORWARDED_FOR']) i get the client ip and the ip of the server当我使用这个 (client_ip = request.META['X_FORWARDED_FOR']) 我得到客户端 ip 和服务器的 ip
How do i just get the Client ip alone我如何单独获得客户端 ip
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单独获取客户端 ip
You could reintegrate the SetRemoteAddrFromForwardedFor middleware that was removed in Django 1.1 .您可以重新集成在 Django 1.1 中删除的 SetRemoteAddrFromForwardedFor 中间件。 This will dynamically reset request.META["REMOTE_ADDR"]
to the client IP address.这会将request.META["REMOTE_ADDR"]
动态重置为客户端 IP 地址。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.