get the real Client IP in django using nginx

Nginx is proxying the request to Django(maybe uwsgi) for you, so any request that reaches Django will look from localhost.

Change nginx config:

location / {
    proxy_pass http://frontends;
    proxy_pass_header Server;
    proxy_redirect off;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Scheme $scheme;
    proxy_set_header REMOTE_ADDR $remote_addr;

and in django can get real client ip :

ip = request.META.get('HTTP_X_FORWARDED_FOR') or request.META.get('REMOTE_ADDR')

Leave a Reply

Your email address will not be published.