Jenkins Behind Apache | How to Configure Apache as a Proxy for Jenkins

How to Route Jenkins HTTP Requests Through Apache

Using Apache to Reverse Proxy Jenkins HTTP Requests
If Jenkins is part of your toolset, it pays to run it behind Apache.

1. You do not want http://your-domain.com:8080/
2. So change it to http://your-domain.com/jenkins

The clutch of tools in the build-business-websites.co.uk domain all share the “behind Apache” middleware pattern.

This page covers HTTP traffic to and from jenkins. You can find HTTPS configuration right here.

Shelter Jenkins Behind Apache in 4 Steps

It only takes 4 steps to shelter Jenkins (and/or Jenkins 2) behind Apache. Here we go.

 

Step 1 == Enable Apache’s Proxy Modules

We want to configure Apache as a proxy for HTTP traffic to and from Jenkins. These commands will enable the modules that do the actual routing.

    sudo a2enmod proxy
    sudo a2enmod proxy_http

Expect the response to resemble

Enabling module proxy.
To activate the new configuration, you need to run:
  service apache2 restart

 

Step 2 == Tell Apache What Requests Go Where

First, do not worry about the below URLs like [http://localhost:8080/jenkins].

You might (quite rightly) think that the Jenkins URL is [http://localhost:8080/]. So the commands appear to tell Apache to send /jenkins traffic to the wrong place.

It makes sense after Step 3.

Tell Apache To Act As A Proxy For Jenkins Traffic

We want Apache to route incoming traffic on port 80 to the localhost port 8080 and then act as a reverse proxy (leaving the client unaware).

The reverse proxy takes the response from port 8080 and ships it back to the client. This configuration is done in

/etc/apache2/sites-available/000-default.conf

Look at the full 000-default.conf Apache configuration file here.

No Changes Needed

No changes are needed to the below configuration unless Jenkins and its Apache proxy are installed on different machines.

The name localhost works for same machine installs. If you want a different url appendage (like /ci-server or /builds) then remember to make the corresponding changes in this and the next step.

This configuration goes just below the closing VirtualHost xml tag in the 000-default.conf file.


##### ############################################################ ####
##### ============================================================ ####
##### The Build Business Websites Jenkins and Apache Configuration ####
##### ============================================================ ####
##### ############################################################ ####

ProxyPass         /jenkins  http://localhost:8080/jenkins nocanon
ProxyPassReverse  /jenkins  http://localhost:8080/jenkins

ProxyRequests     Off
AllowEncodedSlashes NoDecode

<Proxy http://localhost:8080/jenkins*>
  Order deny,allow
  Allow from all
</Proxy>

##### ============================================================ ####
##### ############################################################ ####

Don’t worry about the slash jenkins destination, it looks wrong, but it’s not a typo.

You can restart Apache if you wish, but it won’t work yet because Jenkins is still at http://localhost:8080/

 

Step 3 == Tell Jenkins About Slash Jenkins

Jenkins pages will lack images and css files (stylesheets) – until you tell Jenkins about slash jenkins.

You do this in /etc/default/jenkins by passing an argument to the java run command that gets jenkins going.

You can see the full jenkins configuration file in /etc/default here.

The only change is [--prefix=/jenkins] to tell Jenkins to stick the prefix on whenever it creates URLs for images, stylesheets and whatnot.

##### ############################################################ ####
##### ============================================================ ####
##### The Build Business Websites Jenkins and Apache Configuration ####
##### ============================================================ ####
##### ############################################################ ####

JENKINS_ARGS="--webroot=/var/cache/$NAME/war --httpPort=$HTTP_PORT --prefix=/jenkins"

##### ============================================================ ####
##### ############################################################ ####

Were you worried about the ProxyPass /jenkins http://localhost:8080/jenkins line? Now it makes sense because after the restart, Jenkins will actually respond from [http://localhost:8080/jenkins] and not just [http://localhost:8080/].

Let’s restart the servers.

 

Step 4 == Restart Jenkins and Apache

In step 1 we modified Apache’s behaviour and in step 2 we modified Jenkin’s behaviour. So in step 3 we restart them both.

sudo service jenkins restart
sudo service apache restart

Now you can enjoy Jenkins without the pesky port number. Just like this. Lights out and away you go!

2 comments

Leave a Reply

Your email address will not be published. Required fields are marked *