views:

1224

answers:

4

Hey guys,

So I got a simple setup with nginx for static media and load balancing and tornado as webserver for django (4 servers running). My problem is remote_addr not getting passed on to django so I'm getting a KeyError:

article.ip = request.META['REMOTE_ADDR']

The remote address is getting sent through as X-Real-IP (HTTP_X_REAL_IP) thanks to the nginx.conf:

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

As HTTP is prepended to the META key I can't just do proxy_set_header remote_addr $remote_addr. What I could do is read the X-Real-IP if no remote addr key is found but I'm curious if there's a smarter solution.

Thanks!

+1  A: 

I have a similar setup. After putting nginx in front of apache, I noticed that the IP in the apache logs was always 127.0.0.1. Installing "libapache2-mod-rpaf" seemed to fix it. I have no idea if your problem is related.

asciitaxi
Ah thanks, but I'm not using apache at all for this project.
Nixarn
oops, of course, sorry about that
asciitaxi
A: 

No, it's not possible to pass on remote_addr. So the only solution that I know of is to use X-Real-IP or X-Forwarded-For and make sure that the backend handles these correctly.

windyjonas
A: 

Nixarn Where can I download that middleware?

Peter
I added the middleware an answer to this question
Nixarn
+2  A: 

Here's how I solved the problem. By using this middleware:

class SetRemoteAddrMiddleware(object):
    def process_request(self, request):
        if not request.META.has_key('REMOTE_ADDR'):
            try:
                request.META['REMOTE_ADDR'] = request.META['HTTP_X_REAL_IP']
            except:
                request.META['REMOTE_ADDR'] = '1.1.1.1' # This will place a valid IP in REMOTE_ADDR but this shouldn't happen

Hope that helps!

Nixarn