My Adventures in Coding

February 6, 2015

IIS – PowerShell script to add IIS URL Rewrite Rule

Filed under: IIS — Brian @ 11:50 am
Tags: , , , , , , , ,

We have a Java REST API that runs on the same server as a .NET application. We only allow port 80 to be open on our web server. For this reason we needed to create a URL Rewrite Rule in IIS to redirect external traffic to the REST API on port 80 to port 81. It is fairly easy to do this setup in IIS, however we always want to have all of our server setup scripted. So here is a simple PowerShell script to create a URL Rewrite rule.

Install Application Request Routing (ARR)

Powershell Script to add Rule

In this example the application is called “fddapi” and the root of the application path is “/fddapi” which is the pattern we are looking for in the URL Rewrite rule.

$site = "iis:\sites\Default Web Site"
$filterRoot = "system.webServer/rewrite/rules/rule[@name='fddapi$_']"
Clear-WebConfiguration -pspath $site -filter $filterRoot
Add-WebConfigurationProperty -pspath $site -filter "system.webServer/rewrite/rules" -name "." -value @{name='fddapi' + $_ ;patternSyntax='Regular Expressions';stopProcessing='False'}
Set-WebConfigurationProperty -pspath $site -filter "$filterRoot/match" -name "url" -value "(fddapi)/.*"
Set-WebConfigurationProperty -pspath $site -filter "$filterRoot/conditions" -name "logicalGrouping" -value "MatchAny"
Set-WebConfigurationProperty -pspath $site -filter "$filterRoot/action" -name "type" -value "Rewrite"
Set-WebConfigurationProperty -pspath $site -filter "$filterRoot/action" -name "url" -value "http://localhost:8081/{R:0}"

After running the script your new URL Rewrite rule should now be created with the following settings:EditInBoundRule

 

So now any request to http://localhost/fddapi/someroute will be automatically redirected by IIS to our Java REST API (running on Jetty) to http://localhost:8081/fddapi/someroute and the caller will never know the difference!

January 11, 2015

IIS – Setup IIS as a proxy to Jetty

Filed under: .NET,IIS — Brian @ 12:35 am
Tags: , ,

On my current project we are working on two applications for a customer, one is in .NET and the other is in Java. The client only wants to have port 80 and 443 open on the server, however, we will need to have two web servers running, IIS for the .NET application and Jetty for the Java application. Our solution was to run the .NET application on IIS on port 80 and the Java application on Jetty on port 81, then have IIS route traffic for the Java application coming in on port 80 to port 81 using IIS as a proxy to Jetty. The following is a simple tutorial on how to setup IIS as a proxy to Jetty.

Install Application Request Routing (ARR)

Setup IIS to Jetty Redirect

For this example the route for the Jetty application will be “fddapi”.

Open IIS and on the Server select “Application Request Routing” icon.

1-Open_AAR_from_Default_Website

Select “Server Proxy Settings”.

2-Server_Proxy_Settings

Set “Enable Proxy” setting and “Apply” the change.

3-Enable_Proxy_Setting

Go to “Default Web Site”, select “URL Rewrite”.

4-Select Url ReWrite

Under the Actions menu select “Add Rules”.

5-Add_Rule

Under “Inbound Rules” select “Blank Rule” and click “Ok”.

6-Blank_Rule

Edit the Inbound Rule with a pattern to match on and how to rewrite the url.

7-Edit_Inbound_Rule

Test the Setup

Now open a browser and go to the url: http://localhost/fddapi/someroute which should work now without needing to specify port 8081.

Blog at WordPress.com.