Home > front end >  Nginx, gunicorn, django not passing real IPV6 address
Nginx, gunicorn, django not passing real IPV6 address

Time:09-27

I am currently trying to set up a simple ip website, miip.co with a api with the purpose of just showing your your ip address when you open the site.

When I test https://ready.chair6.net/?url=https://miip.co it shows yes to all but ipv4 literals with only a warning.

My nginx location looks like so

    location / {
        proxy_pass http://unix:/home/www/miip/app.sock;
        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;
        proxy_read_timeout 1000; # this
    }

my gunicorn command looks like so

command = /home/www/miip/venv/bin/gunicorn --workers 3 --bind unix:/home/www/miip/app.sock app.wsgi:application

And my django ip function looks like so

    def get_ip(request):
        x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
        if x_forwarded_for:
            ip = x_forwarded_for.split(',')[0]
        else:
            ip = request.META.get('REMOTE_ADDR')
        return ip

Yet despite this, it always return a ipv4. My system is also on ipv6. I think it has to do with nginx proxy pass but unsure.

CodePudding user response:

Okay, got it.
In nginx proxy_pass you have to pass the ipv6 and in gunicorn you have to double bind https://djangodeployment.readthedocs.io/en/latest/06-gunicorn.html

in nginx,

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_set_header X-NginX-Proxy false;
        real_ip_header X-Real-IP;
        proxy_pass http://[::1]:8000;
        proxy_redirect off;
    }

and in gunicorn

command = /home/www/miip/venv/bin/gunicorn --workers 3 --bind unix:/home/www/miip/app.sock --bind [::1]:8000 app.wsgi:application
  • Related