Tony Joseph
Tony Joseph

Reputation: 1892

Nginx does not forwards remote address to gunicorn

I have the following nginx configuration to forward requests to gunicorn.

location / {
    proxy_pass http://127.0.0.1:8000;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

However, when I access remote address using request.META['REMOTE_ADDR'], it always returns 127.0.0.1. I am using Django 1.9

Upvotes: 0

Views: 771

Answers (1)

miki725
miki725

Reputation: 27861

That is correct and expected behavior. If you would like to access users IP you will need to use:

request.META['HTTP_X_FORWARDED_FOR']

Note that in development (without running nginx), REMOTE_ADDR is still correct.

My recommendation is to add a middleware or a utility method which will do the conditional logic to get the actual users IP depending on your settings.

Upvotes: 1

Related Questions