Saturday, June 29, 2013

How To Use IIS7 as Front End to Java Web Servers Like Tomcat and Jetty

While working on a project, I was faced with a situation where, I needed to use IIS server as the front end for the Jetty web server. 

I had a web application developed using Java running on Jetty.  It was time to host it on a public server, I already had a hosting provider and a hosting plan setup (from one of my previous projects) the only problem was, this was a Windows 2012 server running IIS8 on it.  There was already another .NET website hosted on IIS on this server.  I just wanted to add my web application written in Java to this setup.  Well, I know, not a very ideal situation, but I needed to get this done and get it done fast enough!

I thought for a few moments about what are my options here
  • I could install Apache or some other HTTP server and use it to front my Jetty
  • I could use expose Jetty or some other Servlet Container directly to the world (Note that, this was not a mission critical application with huge load and ultra high availability or anything like that)
  • I could use IIS to front my Jetty server
As I mentioned earlier, I needed to get this done as fast as I could.  So out of all the options that came to my mind, last one sounded like the best or at least the fastest option.

I googled around a bit to find out, what was the least painful way to front Jetty with IIS.  There were numerous options that surfaced, I tried a few options like using the JK Connector with ISAPI filter but the one that I ended up using was - Application Request Routing (ARR).  ARR is a free Web extension for IIS 7+.  

It was pretty straightforward to configure ARR on IIS 7+ to front any Java Based web server (or any server for that matter).  In this post I am going to demonstrate the steps needed to configure ARR on IIS7 so that IIS can be used to front any Java based web server.

How Do They Do It?

The process can be divided into 3 easy steps.
  • Install ARR
  • Configure ARR
  • Test the setup
Step - 1 - Install ARR

ARR is available as a free download from  http://www.iis.net/downloads/microsoft/application-request-routing URL.  Download and install this on the machine that has IIS 7+.

After the installation if you open up the IIS Manager (inetmgr) tool, you should see a new section called Server Farms
Server Farm Section Added to IIS

As the name suggests you can do a lot more (like setting up server farms) with ARR, but for now lets just focus on getting using IIS to front Jetty.

Step - 2 - Configure ARR
  • This is where the meat of the solution lies.  Let's start by creation a Server Farm.  Right click on the Server Farm section and select Create Server Farm...
Create Server Farm Section
  • This should open up a wizard that will ask you some details about the Server Farm.  First step is to give a name to the server farm.  In my setup both the IIS and Jetty were on the same physical machine, I called the server farm localhost.  You can call it Blah or Lady Gaga or Tom Hanks or anything else, just go crazy!
Give a name to the server farm
  • Next step would ask you a little more details about the server where we need to forward the request.  Just enter the IP address of you machine.  Please also make sure that, you change the port number in the configuration to match the port number where Jetty is listening for requests.  The port number settings are hidden under the Advanced Settings... link.  In my case Jetty was listening on port 8085.
Provide IP address with port of server where we need to forward the requests
  • Once you are done changing the port number, click Add on the same dialog.  This should add the server to the table below with status as Online.  Once the server is added, you should be all set to Click Finish on the wizard.
Server added to the farm
  • Another confirmation popup would appear which states that, IIS Manager can create a URL rewrite rule to route all incoming request.., just click No here.  We will add the rule ourselves.
  • After clicking Finish, you should have a view very similar to this one
Server Farm Added
  • On this page click the Routing Rules icon.  This should take you to a page where we can configure the Routing Rules.  On this page make sure that Use URL Rewrite to inspect incoming requests is checked.  If it is unchecked, then check it and click Apply on the right side.
Check Use URL Rewrite to inspect incoming requests
  • Click on the URL Rewrite... link under the Advanced Routing section on the right side.  This would open up a page similar to this one.
URL Rewrite Page
  • Click on Add Rule(s)... link on the right side.  On clicking the link it would ask you about what type of rule template you want to start with, just select the Blank rule under the Inbound rules section and click OK.
New Blank Rule
  • Next comes a longish page where we need to configure our rule.  Do not worry, its pretty straightforward.
New Rule Form
  • Just enter the name of the rule as GitShah (again, go crazy here!). Change the Using drop down value from Regular Expressions to Wildcards.  Now the only thing left is to enter a Pattern.
Rule name with Wildcard selected
  • Click the Test pattern next to the Pattern text box.  You should be presented with a dialog where we can test the URL pattern.  
  • Basically in this dialog we need to inform ARR about which URL should it match and where should it forward the request to.  In my case, I wanted to forward any request coming to http://localhost/gitshah to http://localhost:8085/gitshah (served by Jetty).  Hence, any request that starts with /gitshah I wanted to forward to the Jettey server.  
  • This can be done by entering /gitshah* into the Patter (wildcard) text field.  To test whether our understanding is correct, just enter a few patterns in the Input data to test: text box and click the Test button.
Testing the pattern
  • You will notice that in the table below, it shows two rows {R:0} and {R:1}, they are nothing but parts of the test URL that matched the pattern.  {R:0} contains the entire string and {R:1} holds the value other than the pattern itself.  Click Close when you are satisfied with the pattern you want.  It will ask you whether you want to save the pattern, Click Yes.
  • Next you only need to change just more one thing on this page, change the Action type drop down value from Rewrite to Roue to Server Farm
Action value changed to Route to Server Farm
  • That's about all the configuration we need.  Click Apply on the right side and navigate Back to Rules.  You should see a view similar to this one
New rule created
 Step - 3 - Test the setup

All we now need to do is navigate to the URL http://localhost/gitshah/.  If everything is configured correctly (and luck is on your side!), we should see a page that is served from the Jetty web server.
Page served by IIS in Front of Jetty
Success! 

As we can see its pretty simple with ARR to use IIS as Front End to any Java Web Server.  That said, it might not be the best way of doing it.  Depending on your scenario please choose the optimal option.
Have some Fun!