Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django get IP only returns 127.0.0.1

I have a webserver set up with gunicorn and nginx and django.

I am accessing it remotely, and with this:

def testIP(request):
    ip_address = utils.get_ip(request)

I just keep getting an ip address of 127.0.0.1 Like i said I am accessing it remotely and thus it should not be giving a local address.

I think it might have something to do with gunicorn, but I want to check here first to see if you guys have any insights.

like image 444
DantheMan Avatar asked Dec 22 '10 18:12

DantheMan


1 Answers

How does get_ip() work?

If nginx is a reverse proxy and gunicorn is the app server, it's always getting requests from nginx on the local machine.

The real ip that nginx sends to the app server is in my case HTTP_X_REAL_IP via the nginx conf line proxy_set_header X-Real-IP $remote_addr;

So you might want to set that, and in your django app account for the different header by either using your new IP header or set request.META['REMOTE_ADDR'] = request.META['HTTP_X_REAL_IP']

like image 162
Yuji 'Tomita' Tomita Avatar answered Oct 02 '22 15:10

Yuji 'Tomita' Tomita