简体   繁体   English

如何仅在 nginx 下的 Django 中获取客户端 IP 地址?

[英]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.

相关问题 是否可以在 mikrotik 热点网络下使用 django 获取准确的客户端本地 IP 地址? - is it possible to get exact client local IP address using django under mikrotik hotspot network? 如何用Django测试客户端指定IP地址? - How to specify an IP address with Django test client? Django支持nginx:获取远程IP地址的安全可靠方法 - Django behind nginx: secure & robust way to get remote IP address 如何使用 Nginx 作为 http 服务器和 gunicorn 在 Django 中获取公共 IP 地址? - How can I get public ip address in Django with Nginx as http server and gunicorn? 如何使用aiohttp获取客户端的IP地址 - How to get the IP address of a client using aiohttp 如何在 django 中获取 IP 地址 - How can I get ip address in django 如何在 Django 通道中获取 websocket 连接的客户端 IP 地址? - How do I get the client IP address of a websocket connection in Django Channels? 我如何让django-debug-toolbar只显示在python托管的我的IP地址上的任何地方? - How do I get django-debug-toolbar to only display on my ip address hosted on python anywhere? Django 如何登录客户端ip地址? - Django How can I log client ip address? 如何在 Django 视图中获取客户的 IP 视图 - How to get Client's IP in Django Views
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM