x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR') if x_forwarded_for: ip = x_forwarded_for.split(',')[0] else: ip = request.META.get('REMOTE_ADDR')
如果用户是正常情况下,request.META['REMOTE_ADDR']可以获得用户的IP地址;
但有些网站服务器会使用ngix等代理http,或者是该网站做了负载均衡,使用request.META['REMOTE_ADDR']抓取到的是127.0.0.1,
这时使用request.META['HTTP_X_FORWARDED_FOR']才获得是用户的真实IP。
若没有做代理,request.META['HTTP_X_FORWARDED_FOR']值为None
转载自:https://blog.csdn.net/chenana/article/details/118493323
标签:HTTP,FORWARDED,ip,request,Django,META,forwarded,客户端 From: https://www.cnblogs.com/shaoyishi/p/16617180.html