简体   繁体   中英

Nothing is rendered when using a Twisted Reverse Proxy behind a static URL path

I would like to run a Twisted server and serve various things at different URL paths. The first thing I want to do is to set up a reverse proxy when someone hits the /app1 path. This is what I have so far, but when visiting 127.0.0.1/app1 nothing is returned, it doesn't break or anything, just get a blank page.

from twisted.internet import reactor
from twisted.web.server import Site
from twisted.web.resource import Resource
from twisted.web import proxy, server    

site1 = proxy.ReverseProxyResource('127.0.0.1', 3003, '')
site2 = proxy.ReverseProxyResource('127.0.0.1', 3004, '')

root = Resource()
root.putChild("app1", site1)
root.putChild("app2", site2)


reactor.listenTCP(8090, Site(root))
reactor.run()

I have successfully been able to get something working that looks like this:

from twisted.internet import reactor
from twisted.web.server import Site
from twisted.web.resource import Resource
from twisted.web import proxy, server 

site = proxy.ReverseProxyResource('127.0.0.1', 3003, '')    

reactor.listenTCP(8090, Site(site))
reactor.run()

But this only works when going to 127.0.0.1:8090

Anyone have any ideas?

The reverse proxy can only serve up what the backend HTTP server gives it.

Since your code looks more or less correct, my guess is that the backend HTTP server isn't serving the response you expect.

You could try using tcpdump or wireshark to look at the response the backend generates - or use some server-specific tool to get more debug information. You could also try with a different backend HTTP server that you know definitely generates a response to verify that the proxy is (or is not) working.

So it appears the solution was rather simple. I simply needed to add a / to the path parameter and everything is being rendered properly:

from twisted.internet import reactor
from twisted.web.server import Site
from twisted.web.resource import Resource
from twisted.web import proxy, server    

site1 = proxy.ReverseProxyResource('127.0.0.1', 3003, '/')
site2 = proxy.ReverseProxyResource('127.0.0.1', 3004, '/')

root = Resource()
root.putChild("app1", site1)
root.putChild("app2", site2)


reactor.listenTCP(8090, Site(root))
reactor.run()

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