Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Django, why would REMOTE_ADDR return 127.0.0.1 on a web server?

Tags:

python

django

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?

like image 906
Hulk Avatar asked Jan 27 '10 05:01

Hulk


2 Answers

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.

like image 105
Ofri Raviv Avatar answered Oct 29 '22 15:10

Ofri Raviv


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/

like image 45
bjunix Avatar answered Oct 29 '22 15:10

bjunix