How to configure URL Rewrite in Tomcat Java App on Azure App Service.
Published Aug 01 2022 08:15 PM 10.3K Views
Microsoft

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

  1. Got to SSH via portal or http://<webapp-name>.scm.azurewebsites.net/webssh/host
     
  2. Copy the built-in server.xml file to the home directory.
    cp /usr/local/tomcat/conf/server.xml /home/server.xml​


  3. 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 &quot;%r&quot; %s %b %D %{x-arr-log-id}i" />                                                         
    	                                                                                                                                         
    	        <Valve className="org.apache.catalina.valves.rewrite.RewriteValve" />                                                            
          </Host>  ​
  4. Create rewrite.config in the home directory.
    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.
  5. 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​
  6. Go to the Configuration in the portal and then add /home/startup.sh to Startup Command.
    Yeongseon_0-1658717685533.png

     

2. Overwrite the build-in context.xml file with own context.xml file

  1. Got to SSH via portal or http://<webapp-name>.scm.azurewebsites.net/webssh/host

  2. Copy built-in context.xml to the home directory.
    cp /usr/local/tomcat/conf/context.xml /home/context.xml​
  3. Add RewriteValv to /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.
  4. 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]​
  5. 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​
  6. Go to the Configuration in the portal and then add /home/startup.sh to Startup Command.
    Yeongseon_1-1658718013636.png

     

Reference:

Apache Tomcat 9 (9.0.65) - The rewrite Valve

Customize Tomcat configuration in Linux App Service - Microsoft Tech Community

 

Co-Authors
Version history
Last update:
‎Aug 22 2022 09:42 AM
Updated by: