handy django middleware for shared hosting

Posted by Ian Holsman Wed, 16 Aug 2006 10:49:00 GMT

on some shared hosting environments like webfaction they install a reverse proxy solution where you run a apache2 webserver on a custom port, and the main webserver forwards the request through to you.

This works great, and I’ve been using things like this for years, but the problem you get is that all the requests appear to be from 127.0.0.1 (or the machine which is doing the proxying for you) which isn’t the best if you want to record that stuff.

webfaction fixes this in the log files for you automatically, but that doesn’t help you if you want to do something with the IP# in django.

enter FixIP it takes the X-Forwarded-For header sent by compliant servers which holds the real IP and makes django work properly.

installation is easy.. just add the routine to your middleware settings ala
MIDDLEWARE_CLASSES = (
    "django.middleware.common.CommonMiddleware",
    "zilbo.common.utils.middleware.fixip.FixIP",
   ...

Posted in  | Tags  | 4 comments

Comments

  1. Avatar Kjell Magne Fauske said about 1 hour later:

    Thanks for the tip! I’ve been wondering why all of my recorded IPs were set to 127.0.0.1. Now I know why.

  2. Avatar Jay Graves said about 12 hours later:

    This is great. It wasn’t a problem for me yet, but I know it would be soon.

  3. Avatar SmileyChris said about 16 hours later:

    Cool, Ian! I recokon it looks good enough for it to be added to core middleware (but off by default, like request).

  4. Avatar Ian said 1 day later:

    update: it’s in the core as SetRemoteAddrFromForwardedFor

    cool!

Comments are disabled