Using Squatting with reverse proxy (Apache)
OK so you’ve just developed your first Squatting (On::Continuity) app and you now want to put it live on your main website/webserver. So how do you do this?
Well a good seamless & scalable choice is using a Reverse Proxy. This allows us to map a URL straight to a running Squatting app without too much fuss (famous last words!)
For this exercise I’ve used Apache (2.2) and with a bit trail & error I got below to work for me.
First off we need to switch on reverse proxy. You will need to following in your httpd.conf file….
ProxyRequests Off <Proxy *> Order deny,allow Allow from all </Proxy>
Now we want to make it point to our Squatting app….
<VirtualHost *:80> DocumentRoot "/var/www/vhosts/www.mysite.com/html" ServerName www.mysite.com ProxyPass /demo/ http://localhost:4234/demo/ ProxyPassReverse /demo/ http://localhost:4234/demo/ </VirtualHost>
Now any requests to http://www.mysite.com/demo/ are “forwarded” on to your Squatting app running on port 4234.
You may notice that I put a trailing /demo/ on the proxy. This is because the Squatting app is launched via this script….
#!/usr/bin/env perl use Demo 'On::Continuity', 'With::AccessTrace'; Demo->init; Demo->relocate( '/demo' ); Demo->continue(port => 4234);
I found this helped the webserver map back to the proxy when requests were made by the browser (eg. http://www.mysite.com/demo/login). I’m sure there must be another (better?) way but this did work best for me despite endless fiddling with ProxyPass* & RewriteRules ;-(
Restart Apache & start above script in your Squatting app directory and you should find it is now accessible at http://www.mysite.com/demo/. That is except for any static content u had ;-(
Apache is better suited for pushing out static content so its best to let it deal with this by putting static files somewhere under its document root. However this did present a “best practise” problem between Continuity serving static files during testing and going live with Apache.
My approach to this was to prefix all static URI with “demohtml” using following code in Demo.pm
package Demo; use strict; use warnings; use base 'Squatting'; BEGIN { our %CONFIG = ( static => '/demohtml', TT => { config => { INCLUDE_PATH => './tt' }, postfix => '.tt', site_tit => 'Demo', } ); } use Demo::Controllers; use Demo::Views; # static file handling sub continue { my $app = shift; $app->next::method( docroot => "./www", staticp => sub { $_[0]->url =~ m/\.(jpg|jpeg|gif|png|css|ico|js|swf)$/ }, @_ ); } 1;
Then I would have the following in my View & template(s)…..
# in Demo::Views (good place is in "_" default template) $v->{ Static } = $Demo::CONFIG{ static }; # and then in TT file(s).... <link rel="stylesheet" href="[% Static %]/demo.css" type="text/css" media="screen" title="Demo" />
Therefore the above now appears in the HTML like so…
<link rel="stylesheet" href="/demohtml/demo.css" type="text/css" media="screen" title="Demo" />
This Demo’s static content lives under ./www/demohtml/ in the Squatting app directory so works fine with Continuity during testing. I then sym-linked ./www/demohtml under the Apache document root so that u can see it @ http://www.mysite.com/demohtml/
So Bob’s your Uncle… u should now find your Squatting app working via reverse proxy as u would expect it to 😉
/I3az/
Further reading… Have a look at App::VW to manage the Squatting apps.
References…
Trackbacks