tags:

views:

192

answers:

2

When getting the IP with request.META['REMOTE_ADDR'] code. This works fine on the local system but when hosted on a web server the ip got is 127.0.0.1 - How can this be resolved?

+7  A: 

Your web server is probably behind a load balancer. You can try using request.META['HTTP_X_FORWARDED_FOR'].

Or better, look at the django book, chapter 15 - What’s Middleware? and Reverse Proxy Support (X-Forwarded-For Middleware) sections.

Ofri Raviv
Note that the X-Forwarded-For middleware has been deprecated/removed from recent Django versions, as it was decided that it might give developers a false sense that the X-Forwarded-For header can actually be relied upon (which it can't). Easy enough to write your own; just be aware it can easily be spoofed.
Carl Meyer
+1  A: 

If you are behind a proxy and running apache as the webserver you could use mod_rpaf. The proxy only needs to send X-Forwarded-For or X-Real-IP headers.

http://stderr.net/apache/rpaf/

bjunix