函数实现:
if 'HTTP_X_FORWARDED_FOR' in request.META:
client_ip = request.META['HTTP_X_FORWARDED_FOR']
client_ip = client_ip.split(",")[0]
else:
client_ip = request.META['REMOTE_ADDR']
问题:
以上函数会在有的地方不好使,检测发现request.META根本无key值等于HTTP_X_FORWARDED_FOR和REMOTE_ADDR。
服务器采用nginx+fastcgi实现。
解决办法:
在nginx的fastcgi配置中加入如下内容:
fastcgi_param REMOTE_ADDR $remote_addr;