Resin Documentationapp server |
url rewriting and dispatching
Resin comes with powerful and extensible rewrite capabilities that is as powerful as Apache's mod_rewrite. Resin's dispatching is based on a list of dispatch rules configured in the resin-web.xml or the resin.xml configuration files. Each rule has a regular expression matching request URLs. The first dispatch rule that matches takes control of the request. For example, a <resin:Redirect> sends a HTTP redirect, and a <resin:Dispatch> dispatches the request as normal. Each matching rule can rewrite the URL using a target attribute that
accepts Java regular expressions. The following rule flips the first two
segments around, so <web-app xmlns="http://caucho.com/ns/resin" xmlns:resin="urn:java:com.caucho.resin"> <resin:Redirect regexp="^/([^/]+)/([^/]+)" target="/$2/$1"/> </web-app> Dispatching can be more complicated than just on a regular expression matching of the URL. It may depend on request attributes. In the example below, Resin will send a 403 Forbidden response if the URL path starts with /secret AND the child condition evaluates to true. In this case, the condition <resin:IfSecure> succeeds if the request is not an SSL request. <web-app xmlns="http://caucho.com/ns/resin" xmlns:resin="urn:java:com.caucho.resin"> <resin:Forbidden regexp="^/secret"> <resin:IfSecure value="false"/> </resin:Forbidden> </web-app> Basic ConditionsBasic conditions check the request and return true if the condition matches. Conditions can check on authentication (<resin:IfUserInRole>), the remote IP (<resin:IfNetwork>), check for SSL (<resin:IfSecure>), and check for activation time (<resin:IfCron>) or if a file exists (<resin:IfFileExists>). The rewrite conditions can also be used as security conditions, e.g. for <resin:Allow> or <resin:Deny>. Combining ConditionsAn action tag can have zero condition tags or at most one. If you want combine multiple conditions, then you'll need to use composite conditions like <resin:And>, <resin:Or>, <resin:Not>, <resin:NotAnd>, and <resin:NotOr>. <web-app xmlns="http://caucho.com/ns/resin" xmlns:resin="urn:java:com.caucho.resin"> <resin:Forbidden regexp="^/secret"> <resin:And> <resin:IfLocalPort value="80"/> <resin:Not> <resin:IfUserInRole role="admin"/> </resin:Not> </resin:And> </resin:Forbidden> </web-app> The rewrite capability can also add standard predefined filters to modify the output, e.g. setting a response header. Filters can use conditions as restrictions, just like the dispatch rules. <web-app xmlns="http://caucho.com/ns/resin" xmlns:resin="urn:java:com.caucho.resin"> <resin:SetHeader regexp="^/secret" name="Foo" value="bar"/> </web-app> Servlet FiltersStandard servlet filters can also be invoked as an action to the dispatch target. Your filter is created using Java Injection syntax and will be applied if the dispatch rule matches. <web-app xmlns="http://caucho.com/ns/resin" xmlns:resin="urn:java:com.caucho.resin"> <resin:Dispatch regexp="^/test"> <mypkg:MyFilter xmlns:my="urn:java:com.foo.mypkg"> <mypkg:my-param>my-value</mypkg:my-param> </mypkg:MyFilter> </resin:Dispatch> </web-app>
Logging for the name <logger name="com.caucho.server.rewrite" level="finest"/> [1998/05/08 02:51:31.000] forward ^/foo: '/baz/test.jsp' no match [1998/05/08 02:51:31.000] forward ^/bar: '/baz/test.jsp' no match [1998/05/08 02:51:31.000] forward ^/baz: '/baz/test.jsp' --> '/hogwarts/test.jsp' Redirect HTTP requests to HTTPS requestsThe desired behaviour is to redirect plain connections to SSL connections. http://anything.com/anything.html redirect => https://anything.com/anything.html <resin xmlns="http://caucho.com/ns/resin" xmlns:resin="urn:java:com.caucho.resin"> <cluster ...> <host ...> ... <resin:Redirect regexp="^" target="https://${host.name}"> <resin:IfSecure value="false"/> </resin:Redirect> ... </host> </resin> Make an alias for a web-app
The desired behaviour is to make it so that a web-app will match more than
one url pattern. For example, a web-app is deployed in
http://hostname/classroom/physics forward => http://hostname/physics http://hostname/classroom/physics/anything forward => http://hostname/physics/anything
The rewrite-dispatch tag is used at the <resin xmlns="http://caucho.com/ns/resin" xmlns:resin="urn:java:com.caucho.resin"> <cluster id=""> <host id=""> <resin:Forward regexp="^/classroom/physics" target="/physics"/> Forward based on host nameThe desired behaviour is to forward requests internally based on the host name. http://gryffindor.hogwarts.com/anything.html forward => /gryffindor/* http://slytherin.hogwarts.com/anything.html forward => /slytherin/anything.html http://hogwarts.com/anything.html forward => /anything.html
The rewrite-dispatch tag is used at the <resin xmlns="http://caucho.com/ns/resin" xmlns:resin="urn:java:com.caucho.resin"> <cluster> ... <resin:Forward regexp="http://gryffindor\.[^/]+" target="/gryffindor/"/> <resin:Forward regexp="http://slytherin\.[^/]+" target="/slytherin/"/> ... </cluster> </resin> Proxy requests to PHP/Ruby using FastCGI<web-app xmlns="http://caucho.com/ns/resin" xmlns:resin="urn:java:com.caucho.resin"> <resin:FastCgiProxy regexp="\.php$"> <address>127.0.0.1:12345</address> </resin:FastCgiProxy> <resin:FastCgiProxy regexp="\.rb$"> <address>127.0.0.1:67890</address> </resin:FastCgiProxy> </web-app>
|