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
- Got to SSH via portal or http://<webapp-name>.scm.azurewebsites.net/webssh/host
- Copy the built-in server.xml file to the home directory.
cp /usr/local/tomcat/conf/server.xml /home/server.xml
- Add RewriteValv to /home/server.xml file
<!-- 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>
- Create rewrite.config in the home directory.
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]
- Create startup.sh in the home directory.
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
- Go to the Configuration in the portal and then add /home/startup.sh to Startup Command.
2. Overwrite the build-in context.xml file with own context.xml file
- Got to SSH via portal or http://<webapp-name>.scm.azurewebsites.net/webssh/host
- Copy built-in context.xml to the home directory.
cp /usr/local/tomcat/conf/context.xml /home/context.xml
- Add RewriteValv to /home/context.xml.
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.<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>
- Create rewrite.config file in the WEB-INF folder of web application.
cat rewrite.config RewriteCond %{HTTP_HOST} ^<webapp-name>.azurewebsites.net [NC] RewriteRule ^(.*)$ http://microsoft.com [L,R=301]
- Create startup.sh in the home directory.
#!/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
- Go to the Configuration in the portal and then add /home/startup.sh to Startup Command.
Reference:
Apache Tomcat 9 (9.0.65) - The rewrite Valve
Customize Tomcat configuration in Linux App Service - Microsoft Tech Community
Updated Aug 22, 2022
Version 3.0Yeongseon
Microsoft
Joined July 07, 2021
Apps on Azure Blog
Follow this blog board to get notified when there's new activity