[Twisted-web] mod_rewrite while persisting client ips

Miika Keskinen weeti at usr.fi
Wed Oct 20 01:34:17 MDT 2004

Hi there.

I'm writing to the list in case someone finds this useful, I include short 
ProxyIPResource-'hack' and corresponding rewriterule to set 
request.client.host correctly :)

here it goes:


from twisted.application import service, internet

from nevow import rend, loaders, appserver, vhost, inevow
from nevow import tags as T

Example apache configuration

NameVirtualHost *

<VirtualHost *>
         RewriteEngine        on
         RewriteRule (.*)/ http://localhost:8080/proxyip/%{REMOTE_HOST}/vhost/http/test.com/$1 [P]
         ServerName test.com


class ProxyIPResource:
     __implements__ = inevow.IResource,

     def putChild(self, child, res):
         setattr(self, 'child_%s' % (child,), res)

     def locateChild(self, ctx, segments):
         if len(segments) < 1:
             return rend.NotFound

         elif "child_%s" % (segments[0],) in self.__dict__:
             return self.__dict__['child_%s' % (segments[0],)], segments[1:]

             request = inevow.IRequest(ctx)
             request.client.host = segments[0]

             prefixLen = len('/'+'/'.join(request.prepath)+'/'+'/'.join(segments[:1]))
             request.path = '/'+'/'.join(segments[1:])
             request.uri = request.uri[prefixLen:]
             request.prepath = request.prepath[2:]
             return self, segments[1:]

class Simple(rend.Page):
     addSlash = True
     docFactory = loaders.stan(

application = service.Application("Simple")

res = Simple()

vResource = vhost.VHostMonsterResource()
pResource = ProxyIPResource()

res.putChild('proxyip', pResource)
pResource.putChild('vhost', vResource)



More information about the Twisted-web mailing list