Home > Jenkins > Using IIS as a reverse proxy for Jenkins

Using IIS as a reverse proxy for Jenkins

Jenkins is one of the most popular build servers and it runs on a wide variety of platforms (Windows, Linux, Mac OS X) and can build software for most programming languages (Java, C#, C++, …). And best of all, it is fully open source and free to use.

By default Jenkins runs on the port 8080, which can be troublesome as this not the standard port 80 used by most web applications. But running on port 80 is in most cases not possible as the webserver is already using this port. Luckily IIS has a neat feature that allows it to act as a reverse proxy.

The reverse proxy mode allows to forward traffic from IIS to another web server (Jenkins in this example) and send the responses back through IIS. This allows us to assign a regular DNS address to Jenkins and use the standard HTTP port 80. In this guide, I will explain you how you can set this up.

What is required?

You need an installation of IIS 7 or higher and you need to install the additional modules “URL Rewrite and “Application Request Routing”. The easiest way to install these modules is through the Microsoft Web Platform Installer.

Configuring IIS

Once the two necessary modules are installed, you have to create a new website in IIS. In my example I bind this website to the DNS alias “Jenkins.test.intranet”. You can bind this of course to the DNS of your choice (or to no specific DNS entry).

2015-02-10 16_23_54-srv-THC-BUILD2 - Remote Desktop Connection

Next you must copy the following web.config to the root of newly created website.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <system.webServer>
        <rewrite>
            <rules>
                <rule name="Reverse Proxy to Jenkins" stopProcessing="true">
                    <match url="(.*)" />
                    <action type="Rewrite" url="http://localhost:8080/{R:1}" />
                </rule>
            </rules>
        </rewrite>
    </system.webServer>
</configuration>

This rule forwards all the traffic to http://localhost:8080/, the address on which Jenkins is running. It is also possible to configure this through the GUI with the URL Rewrite dialog boxes.

I you are not forwarding to a localhost address, you need to go into the dialogs of Application Requet Routing and check the “Enable proxy” property.

2015-02-10 16_53_25-srv-THC-BUILD2 - Remote Desktop Connection

Advertisements
Categories: Jenkins Tags:
  1. PH
    March 24, 2015 at 10:06
  2. March 30, 2015 at 12:16

    Good clear post Pieter.
    I do think the title should actually be: Using IIS as a reverse proxy for Jenkins 🙂

    • pieterderycke
      March 30, 2015 at 12:25

      thanks 😉

  3. July 11, 2015 at 09:53

    Pieter,

    Great post. Fingers crossed it works as expected when I implement it tomorrow 😃

    Web.config is displaying html codes instead of the character. e.g. < instead of <.

    Tim

  4. July 13, 2015 at 21:02

    >> I you are not forwarding to a localhost address, you need to go into the dialogs of Application Requet Routing and check the “Enable proxy” property. <<

    For localhost addresses I had to Enable Proxy on a Windows Server 2012 R2 box.

  5. February 24, 2016 at 12:01

    I took a slightly different approach, one I’m more familiar with:

    1) Create a folder under the Default Web Sites root folder (usually C:\inetpub\wwwroot) called jenkins
    2) Convert that folder to an application with the IIS GUI
    3) Copy the web.config into the folder
    4) Enable Web Proxy (required even though it was on localhost)

  6. July 20, 2016 at 18:57

    Did anyone run into “No valid crumb was included in the request” errors when saving items in Jenkins? I’m reading that I need to disable some cross-site security but can’t get it to work in IIS.

  7. allison richards
    August 23, 2016 at 13:11

    fuckme this web.config you pasted in:

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
    <system.webServer>
    <rewrite>
    <rules>
    <rule name="Reverse Proxy to Jenkins" stopProcessing="true">
    <match url="(.*)" />
    <action type="Rewrite" url="http://localhost:8080/{R:1}" />
    </rule>
    </rules>
    </rewrite>
    </system.webServer>
    </configuration>

    WHAT THE FUCK IS ALL THIS ABOUT>????? SORT YOUR ENCODING OUT U PRICK!!

  8. dicconb
    September 7, 2016 at 13:30

    Hi, thanks very much for this!

    In case anyone else has the same problem, I was getting the “No valid crumb was included in the request” error on pages that perform asynchronous updates (eg the Console Output page for a running job). I think the problem is caused because IIS (like nginx) doesn’t like headers that start with a full stop/period (in this case “.crumb”), so the CSRF protection was breaking. I fixed by using this command line argument to customize the name of the CSRF crumb header to one that didn’t start with a dot:

    -Dhudson.security.csrf.requestfield=X-Jenkins-Crumb

    As I deployed Jenkins using the MSI installer I had to add it to the element in C:\Program Files (x86)\Jenkins\jenkins.xml

    I am using IIS 8.5 on Server 2012 R2

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: