If you are using a Tomcat Java App, you can easily set route rules using Tomcat. This article provides how to configure the URL Rewrite functionality step by step in Tomcat Java App on Azure App Service.
What is the URL Rewrite Value?
The URL Rewrite functionality was introduced from Tomcat 8. This feature is similar to mod_rewrite from Apache HTTP Server.
There are 2 ways to use URL rewrite functionality in Tomcat.
1. Overwrite the built-in server.xml file with own server.xml file
2. Overwrite the built-in context.xml file with own context.xml file
1. Overwrite the built-in server.xml file with own server.xml file
cp /usr/local/tomcat/conf/server.xml /home/server.xml
<!-- In AppService below is customized to add : unpackWARs="${site.unpackWARs}" and workDir="${site.tempdir}-->
<Host name="localhost" appBase="${site.root}"
unpackWARs="${site.unpackWARs}" autoDeploy="true" workDir="${site.tempdir}">
<!-- SingleSignOn valve, share authentication between web applications
Documentation at: /docs/config/valve.html -->
<!--
<Valve className="org.apache.catalina.authenticator.SingleSignOn" />
-->
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<!-- In AppService prefix and pattern are customized-->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="${site.logdir}/http/RawLogs"
prefix="site_access_log.${catalina.instance.name}" suffix=".txt"
pattern="%h %l %u %t "%r" %s %b %D %{x-arr-log-id}i" />
<Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />
</Host>
cat rewrite.config
RewriteCond %{HTTP_HOST} ^<webapp-name>.azurewebsites.net [NC]
RewriteRule ^(.*)$ http://microsoft.com [L,R=301]
With this setup a request to the url path {webapp-name>.azurewebsites.net would get route to http://microsoft.com. You can find more information for RewriteRule on Apache Tomcat 9 (9.0.65) - The rewrite Valve.cat startup.sh
#!/bin/bash
#echo "Using customer server.xml to overwrite /usr/local/tomcat/conf/server.xml"
#cp /home/server.xml /usr/local/tomcat/conf/server.xml
#echo "Move rewrite.config file to the Host configuration folder"
#cp /home/rewrite.config /usr/local/tomcat/conf/Catalina/localhost/rewrite.config
2. Overwrite the build-in context.xml file with own context.xml file
cp /usr/local/tomcat/conf/context.xml /home/context.xml
<Context>
<!-- Default set of monitored resources. If one of these changes, the -->
<!-- web application will be reloaded. -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>WEB-INF/tomcat-web.xml</WatchedResource>
<!-- In AppService we customize WatchedResource to /home/site/deployments/active -->
<WatchedResource>/home/site/deployments/active</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!-- In AppService we need to disable session persistence across Tomcat restarts-->
<Manager pathname="" />
<Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />
</Context>
With this setup a request to the url path {webapp-name>.azurewebsites.net would get route to http://microsoft.com. You can find more information for RewriteRule on Apache Tomcat 9 (9.0.65) - The rewrite Valve.cat rewrite.config
RewriteCond %{HTTP_HOST} ^<webapp-name>.azurewebsites.net [NC]
RewriteRule ^(.*)$ http://microsoft.com [L,R=301]
#!/bin/bash
echo "Using customer context.xml to overwrite /usr/local/tomcat/conf/context.xml"
cp /home/context.xml /usr/local/tomcat/conf/context.xml
Reference:
Apache Tomcat 9 (9.0.65) - The rewrite Valve
Customize Tomcat configuration in Linux App Service - Microsoft Tech Community
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.