Simple development http-proxy for multiple source servers
Question: I developed till now with different webapp-servers (Tornado, Django, ...) and
am encountering the same problem again and again:

I want a simple web proxy (reverse proxy) that allows me, that I can combine
different source entities from other web-servers (that could be static files,
dynamic content from an app server or other content) to one set of served
files. That means, the browser should see them as if they come from one
source.

I know, that I can do that with nginx, but I am searching for an even simpler
tool for development. I want something, that can be started on command line
and does not need to run as root. Changing the configuration (routing of the
requests) should be as simple as possible.

In development, I just want to be able to mashup different sources. For
example: On my production server runs something, that I don't want to copy,
but I want to connect with static files on a different server and also a new
application on my development system.

Speed of the proxy is not the issue, just flexibility and speed of
development!

Preferred would be a Python or other scripting solution. I found also a big
list of Python proxys, but after scanning the list, I found that all are
lacking. Most of them just connect to one destination server and no way to
have multiple servers (the proxy has to decide which to take by analysis of
the local url).

I am just wondering, that nobody else has this need ...


Answer: You do not need to start nginx as root as long as you do not let it serve on
port 80. If you want it to run on port 80 as a normal user, [use
setcap](http://stackoverflow.com/questions/413807/is-there-a-way-for-non-root-
processes-to-bind-to-privileged-ports-1024-on-l). In combination with a script
that converts between an nginx configuration file and a route specification
for your reverse proxy, this should give you the most reliable solution.

If you want something simpler/smaller, it should be pretty straight-forward to
write a script using Python's `BaseHTTPServer` and `urllib`. Here's an example
that only implements `GET`, you'd have to extend it at least to `POST` and add
some exception handling:

    
    
    #!/usr/bin/env python
    # encoding: utf-8
    
    import BaseHTTPServer
    import SocketServer
    import urllib
    import re
    
    FORWARD_LIST = {
        '/google/(.*)': r'http://www.google.com/%s',
        '/so/(.*)': r'http://www.stackoverflow.com/%s',
    }
    
    class HTTPServer(BaseHTTPServer.HTTPServer, SocketServer.ThreadingMixIn):
        pass
    
    class ProxyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
        def do_GET(self):
            for pattern, url in FORWARD_LIST.items():
                match = re.search(pattern, self.path)
                if match:
                    url = url % match.groups()
                    break
            else:
                self.send_error(404)
                return
            dataobj = urllib.urlopen(url)
            data = dataobj.read()
            self.send_response(200)
            self.send_header("Content-Length", len(data))
            for key, value in dataobj.info().items():
                self.send_header(key, value)
            self.end_headers()
            self.wfile.write(data)
    
    HTTPServer(("", 1234), ProxyHandler).serve_forever()
    

