Twisted to proxy php out to apache?
Hello fellow twisters, David Reid suggested that I should ask the twisted.web list for answers ... so here it is. We have a rather large web app based on xul, xmlrpc (via twisted) and php (via apache) .... and we would like to get it all (in due time) in python. As a first step it would be perfect to let twisted handle php page requests so that we can use nevow for the upcomming modules and convert old modules when we have the time to spare. Some time ago, I tried to get PHP to run as a CGI from twisted ... that was so slow that I got timing problems and a non operatable webapp. BTW ... nothing new on the FCGI for PHP front yet? My question ... is it possible to pass php page requests from twisted to apache so I don't suffer the speed loss but still have my app listening on only one port (for http and xmlrpc) ? So can twisted be a proxy for php or is this not the recommended way to go? I know about the reverseproxy stuff .. but if I understand that correctly, it's about passing requests to twisted from apache or other server. I would like it to be the other way round .. twisted passes requests for PHP to apache, receives the processed content and passes that back to the client. (if that method doesn't have any significant drawbacks) - Remi -
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Remi C Cool wrote:
Hello fellow twisters,
Hello again.
I know about the reverseproxy stuff .. but if I understand that correctly, it's about passing requests to twisted from apache or other server. I would like it to be the other way round .. twisted passes requests for PHP to apache, receives the processed content and passes that back to the client. (if that method doesn't have any significant drawbacks)
class ReverseProxyResource(resource.Resource): """Resource that renders the results gotten from another server Put this resource in the tree to cause everything below it to be relayed to a different server. """ I'm sorry, you don't understand it correctly. mod_proxy which passes requests from apache to another server such as twisted is a reverse proxy, ReverseProxyResource is an implementation of a reverse proxy for twisted so that it can forward requests to another server. It takes a host, a port, and a path and it proxies all requests for that resource to the server specified by the host, port, and path. for example, ReverseProxyResource('google.com', 80, '/') will forward requests to http://google.com:80/. - -David - -- "Usually the protocol is this: I appoint someone for a task, which they are not qualified to do. Then, they have to fight a bear if they don't want to do it." -- Glyph Lefkowitz -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.2 (Darwin) iD8DBQFEdJMGrsrO6aeULcgRAkBzAJ4i0cIMWsxq1THnaYJ6NiRxHGs1XwCeOa61 P/9DktOYxqMHSkEz7/RXMs4= =ICdm -----END PGP SIGNATURE-----
David Reid wrote:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1
Remi C Cool wrote:
Hello fellow twisters,
Hello again.
Thanks again for your time.
I know about the reverseproxy stuff .. but if I understand that correctly, it's about passing requests to twisted from apache or other server. I would like it to be the other way round .. twisted passes requests for PHP to apache, receives the processed content and passes that back to the client. (if that method doesn't have any significant drawbacks)
class ReverseProxyResource(resource.Resource): """Resource that renders the results gotten from another server
Put this resource in the tree to cause everything below it to be relayed to a different server. """
I'm sorry, you don't understand it correctly. mod_proxy which passes requests from apache to another server such as twisted is a reverse proxy, ReverseProxyResource is an implementation of a reverse proxy for twisted so that it can forward requests to another server. It takes a host, a port, and a path and it proxies all requests for that resource to the server specified by the host, port, and path.
for example, ReverseProxyResource('google.com', 80, '/') will forward requests to http://google.com:80/.
Ok, I'm with you with that (I need to read more carefully) ... but what I would like to do, is pass all requests for files ending with .php (and maybe .js and .ccs) to be forwarded to another server. Forwarding everything would make the transition from php to nevow a tad more difficult. Would this 'selective' proxying be possible?
- -David
- -- "Usually the protocol is this: I appoint someone for a task, which they are not qualified to do. Then, they have to fight a bear if they don't want to do it." -- Glyph Lefkowitz -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.2 (Darwin)
iD8DBQFEdJMGrsrO6aeULcgRAkBzAJ4i0cIMWsxq1THnaYJ6NiRxHGs1XwCeOa61 P/9DktOYxqMHSkEz7/RXMs4= =ICdm -----END PGP SIGNATURE-----
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Remi Cool wrote:
Ok, I'm with you with that (I need to read more carefully) ... but what I would like to do, is pass all requests for files ending with .php (and maybe .js and .ccs) to be forwarded to another server. Forwarding everything would make the transition from php to nevow a tad more difficult. Would this 'selective' proxying be possible?
Well unless you've specifically got long standing URLs to worry about you should just take whatever static.File you had those php, js, and css wrapped in before (when you tried CGI) and replace it with a ReverseProxyResource to the appropriate path on the apache server. And then put all the nevow code in a seperate part of the hierarchy for example. root = someBlankResource() root.putChild('nevow', MyCoolNevowApplication()) root.putChild('oldapplication', ReverseProxyResource('localhost', 8080, '/')) Note that MyCoolNevowApplication can still reference javascript and CSS being served through /oldapplication by apache via the proxy. You could even put 'oldapplication' as a child of MyCoolNevowApplication and have that be your root resource. But not knowing specifically the url hierarchy of your currently deployed application I can't give a thorough opinion on how you should lay these things out. If your code isnt' easily segmented in this manner then there may not be an out of the box solution for you. - -David - -- "Usually the protocol is this: I appoint someone for a task, which they are not qualified to do. Then, they have to fight a bear if they don't want to do it." -- Glyph Lefkowitz -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.2.2 (Darwin) iD8DBQFEdLa9rsrO6aeULcgRAvnPAJ42pehXVlG63OG7Y8l1RCyVNNIE/wCeLq6B BtS/gF2Edkb3cSfsWmSyeGg= =uLyl -----END PGP SIGNATURE-----
On Wed, 24 May 2006 12:40:45 -0700, David Reid
Remi Cool wrote:
Ok, I'm with you with that (I need to read more carefully) ... but what I would like to do, is pass all requests for files ending with .php (and maybe .js and .ccs) to be forwarded to another server. Forwarding everything would make the transition from php to nevow a tad more difficult. Would this 'selective' proxying be possible?
Well unless you've specifically got long standing URLs to worry about
David's explanation was correct, but I tend to expect long-standing URLs to continue to work. With that in mind: Twisted's philosophy in this regard is to provide simple interfaces so that you can write your own "glue" resources criteria as you see fit. Since I was bored and stuck in a train station while I was reading this message, here's a quick "left/right" switch for resources. Toooootally untested, not even run. The way you'd use it in your program is similar to dreid's .tac example: SegmentSwitch(lambda segments: segments[-1].split('.')[-1] in ('php', 'js', 'css'), MyCoolNevowApplication(), ReverseProxyResource('localhost', 8080, '/')) And here's the implementation: from zope.interface import implements from twisted.web2 import resource, iweb class SegmentSwitch(resource.Resource): """ I am a resource that switches between two alternative resource hierarchies depending on a callable. """ implements(iweb.IResource) def __init__(self, switch, left, right): """ Create a switching resource. @param switch: A one-argument callable that takes a sequence of segments and returns False to divert to the left resource and True to divert to the right. @param left: An IResource provider to be used when 'switch' returns False. @param right: An IResource provider to be used when 'switch' returns True. """ self.switch = switch self.left = left self.right = right def locateChild(self, request, segments): if self.switch(segments): res = self.right else: res = self.left return res.locateChild(request, segments) def render(self, request): return "OOPS"
glyph@divmod.com wrote:
On Wed, 24 May 2006 12:40:45 -0700, David Reid
wrote: Remi Cool wrote:
Ok, I'm with you with that (I need to read more carefully) ... but what I would like to do, is pass all requests for files ending with .php (and maybe .js and .ccs) to be forwarded to another server. Forwarding everything would make the transition from php to nevow a tad more difficult. Would this 'selective' proxying be possible?
Well unless you've specifically got long standing URLs to worry about
David's explanation was correct, but I tend to expect long-standing URLs to continue to work. With that in mind: Twisted's philosophy in this regard is to provide simple interfaces so that you can write your own "glue" resources criteria as you see fit. Since I was bored and stuck in a train station while I was reading this message, here's a quick "left/right" switch for resources. Toooootally untested, not even run.
Thanks for your (and David's) explenation ... I can work this out to a 'perfect' solution for my project. - Remi -
participants (4)
-
David Reid
-
glyph@divmod.com
-
Remi C Cool
-
Remi Cool