package ProxyFun; use Mojo::Base 'Mojolicious'; sub startup { my $app = shift; $app->plugin('Mojolicious::Plugin::ReverseProxy',{ # mandatory destination_url => 'http://www.oetiker.ch', # optional routes => $app->routes, # default mount_point => '/', # default req_processor => sub { my ($c,$req) = @_; # do something to the request object prior # to passing it on to the destination_url # maybe fix the Origin or Referer headers for (qw(Origin Referer)){ my $value = $req->headers->header($_) or next; if ( $value =~ s{http://www.oetiker.ch}{http://localhost:3000} ){ $req->headers->header($_,$value); } } }, res_processor => sub { my ($c,$res) = @_; # do something to the response object prior # to passing it on to the client # maybe fixing the location header # or absolute URLs in the body if (my $location = $res->headers->location){ if ( $location =~ s{http://www.oetiker.ch}{http://localhost:3000} ){ $res->headers->location($location); } } if ($res->headers->content_type =~ m{text/html} and my $body = $res->body){ if ( $body =~ s{http://www.oetiker.ch}{http://localhost:3000}g){ $res->body($body); $res->headers->content_length(length($body)); } } }, } }
The Mojolicious::Plugin::ReverseProxy lets your register a proxy route. The module is rather mindless in the sense that it does not try to help you with fixing headers or content to actually work with the proxy, apart from the Host header.
Host
What makes this Plugin really useful, is that you can supply a req_processor and a res_processor callback which will act on the request prior to passing it on to the destination and on the response prior to returning it to the client respectively.
req_processor
res_processor
The plugin takes the following options:
Where should the proxy connect to
destination_url => 'http://www.oetiker.ch'
the routes object to use for adding the proxy route
under which path should the proxy appear.
Can be pointed to an anonymous subroutine which is called prior to handing control over to the user agent.
If you render the page in the req_processor callback, the page will be returned immediately without calling the destination_url
req_processor callback
destination_url
Can be pointed to an anonymous subroutine which is called prior to rendering the response.
Tobias Oetiker, <tobi@oetiker.ch>
Copyright OETIKER+PARTNER AG 2014
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.8 or, at your option, any later version of Perl 5 you may have available.
1 POD Error
The following errors were encountered while parsing the POD:
You forgot a '=back' before '=head1'
To install Mojolicious::Plugin::ReverseProxy, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious::Plugin::ReverseProxy
CPAN shell
perl -MCPAN -e shell install Mojolicious::Plugin::ReverseProxy
For more information on module installation, please visit the detailed CPAN module installation guide.