Django后端当中如何去获取客户端的ip
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