简体   繁体   中英

Twisted + Django + Reverse Proxy

I am deploying twisted as a web server for my site. I am looking into possibilities of reverse proxying.

I have the following code right now hooked up to my reactor for django. I am using comet, and I realize that I absolutely must use port 80 hence I am looking into possibilities of reverse proxying. On this site, I found the following example:

# Django setup
sys.path.append("shoout_web")
os.environ['DJANGO_SETTINGS_MODULE'] = 'shoout_web.settings'

def wrapper_WSGIRootWrapper():
    # Build the wrapper first
    generic = WSGIHandler()
    def HandlerWrapper(environ, start_response):
        environ['engine'] = engine
        return generic(environ, start_response)

    # Thread and Allowing Ctrl-C to get you out cleanly:
    pool = threadpool.ThreadPool()
    pool.start()
    reactor.addSystemEventTrigger('after', 'shutdown', pool.stop)
    return wsgi.WSGIResource(reactor, pool, HandlerWrapper)
WSGIRoot = wrapper_WSGIRootWrapper()

# Reverse Proxy
class Simple(Resource):
    isLeaf = False

    def getChild(self, name, request):
        if name == "orbited":
            print "orbited"
            return proxy.ReverseProxyResource('localhost', 12345, "/"+name)
        else: 
            return WSGIRoot.getChildWithDefault(name, request)

# Attaching proxy + django 
log_dir = './.log'
if not os.path.exists(log_dir):
    os.makedirs(log_dir)
reactor.listenTCP(DJANGO_PORT, server.Site(Simple(), logPath=os.path.join(log_dir, '.django.log')))

My trouble is I don't really know what to fill in in the else part of that second code part. I looked at text_proxy on twisted-src and there weren't substantial examples for this. Any help?

It is not clear to me why you want to use a reverse-proxy. I think you're trying to use the right tool for the wrong reasons.

Reverse proxy is useful because you can have a lightweight server like nginx handle thousands of http keep-alive connections with very little memory overhead. The connections between the reverse proxy and the real web server (twisted in your case) are fewer and short-lived by comparison, thus you can handle higher loads. Note that if you're using long-lived comet connections, there's no benefit here, because you need the connection open in both servers for the duration.

You seem to be wanting to use it to simply make the server on port 12345 available on port 80. This is not what a reverse-proxy is for. Why not just bind port 80 in the first place?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM