简体   繁体   English

Twisted + Django +反向代理

[英]Twisted + Django + Reverse Proxy

I am deploying twisted as a web server for my site. 我正在将twisted部署为网站的Web服务器。 I am looking into possibilities of reverse proxying. 我正在研究反向代理的可能性。

I have the following code right now hooked up to my reactor for django. 我现在将以下代码连接到我的Django的反应堆。 I am using comet, and I realize that I absolutely must use port 80 hence I am looking into possibilities of reverse proxying. 我正在使用彗星,并且我意识到我绝对必须使用端口80,因此我正在研究反向代理的可能性。 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. 我在twisted-src上查看了text_proxy,但没有大量示例。 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. 反向代理很有用,因为您可以让像nginx这样的轻量级服务器以很少的内存开销处理数千个http保持活动连接。 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. 相比之下,反向代理与真实Web服务器(在您的情况下为扭曲的)之间的连接更少且寿命较短,因此您可以处理更高的负载。 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. 您似乎想要使用它来使端口12345上的服务器在端口80上可用。这不是反向代理的用途。 Why not just bind port 80 in the first place? 为什么不首先绑定端口80?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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