[Twisted-web] reverse proxy with nevow

Mike Mueller pyp at gmx.net
Fri Nov 12 01:51:15 MST 2004

At 09:57 11.11.2004 +0100, you wrote:
>Hi Mike,
>in just a bit I'll have to set up a reverse proxy with url rewriting.  Do 
>you mind to share your setup?  (and can I post it on the devel list of 
>OpenPortalGuard (openportalguard.sf.net))?
>many thanks

Here is my version.
It serves static files from subdirectories in a given directory.
The names of the subdirectories correspond to the domain names.
In the example below, this is just domain1.com but it could be more domains.
The reverse proxy part redirects all requests to domains2.com to port 8080 
on the same machine.
So instead using domain2.com:8080 you can use domain2.com directly (that is 
on port 80).

The source for my server on port 80 looks like this:

from twisted.web import vhost, static, server, proxy
from twisted.application import internet, service
import os

# use this path to put several subdirectories
# the name of each subdirectory is used as a domain name
vhostDir = '..\hosts'

root = vhost.NameVirtualHost()
dir = 'domain1.com'
# making all files in ..\hosts\domain1.com\ available
# as static pages, just put your html files there
root.addHost(dir, static.File(os.path.join(vhostDir, dir)))

# now the reverse proxy part
vhostName = 'domain2.com'
# there is another server listening at 8080
reverseProxy = proxy.ReverseProxyResource('localhost', 8080, '')
# just add it to root and tell that it is a reverseProxy
root.addHost(vhostName, reverseProxy)
application = service.Application('web')
sc = service.IServiceCollection(application)
site = server.Site(root)
i = internet.TCPServer(80, site)

For this to work you have to have a directory ../hosts/domain1.com and of 
course put the lines       domain1.com       domain2.com
in your /etc/hosts.

The source for the reverse proxy looks like this:

import random
from nevow import appserver, vhost
from twisted.application import internet, service
from nevow import rend, tags
from nevow import loaders

vResource = vhost.VHostMonsterResource()

class VHostGreeter(rend.Page):
     docFactory = loaders.stan(
     tags.head[ tags.title[ "Runnig as VHostMonsterResource" ]],
         tags.h1(style="font-size: large")[ "This works." ]

root = VHostGreeter()
#just add child as nevow.vhost.VHostMonsterResource()
root.putChild("vhost", vResource)

application = service.Application('web-proxy')
sc = service.IServiceCollection(application)
site = appserver.NevowSite(root)
i = internet.TCPServer(8080, site)

The server listens on port 8080 and can still be directly accessed via 
But it also works behind the static server. In this case it means the 
bowser just shows domain2.com.

That's what I wanted. You might need to adapt to your needs.



More information about the Twisted-web mailing list