Running Jenkins behind Nginx

Version 5 by Erik Zaadi
on Oct 23, 2011 15:54.

compared with
Current by Joe Littlejohn
on Nov 08, 2013 13:27.

Key
This line was removed.
This word was removed. This word was added.
This line was added.

Changes (11)

View Page History
In situations where you have existing web sites on your server, you may find it useful to run Hudson Jenkins (or the servlet container that Hudson Jenkins runs in) behind [Nginx|http://nginx.org/], so that you can bind Hudson Jenkins to the part of a bigger website that you may have. This document discusses some of the approaches for doing this.

When a request arrives for certain URLs, Nginx becomes a proxy and further forward that request to Hudson, Jenkins, then it forwards the response back to the client. A typical set up for mod_proxy would look like this:
{noformat}server {
listen 80; # Listen on port 80 for IPv4 requests
server_name jenkins.example.com;

#this is the jenkins web root directory (mentioned in the /etc/default/jenkins file)
root /var/run/jenkins/war/;

access_log /var/log/nginx/hudson_access.log;
error_log /var/log/nginx/hudson_error.log;

location ~ ^/static/[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]\/(.*)$ {
location ~ "^/static/[0-9a-fA-F]{8}\/(.*)$" {

#rewrite all static files into requests to the root
#E.g /static/12345678/css/something.css will become /css/something.css
rewrite "^/static/[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]\/(.*)" "^/static/[0-9a-fA-F]{8}\/(.*)" /$1 last;
}

}
sendfile on;
expires 31d;
}

sendfile off;
proxy_pass http://127.0.0.1:8080;
proxy_redirect off; default;

proxy_set_header Host $host;

location / {
#this is the jenkins web root directory (mentioned in the /etc/default/jenkins file)
root /var/run/jenkins/war/;

# Optional configuration to detect and redirect iPhones
{noformat}

This assumes that you run Hudson Jenkins on port 8080.

{note}For this set up to work, the context path of Hudson Jenkins must be the same between your Nginx and Hudson Jenkins (that is, you can't run Hudson Jenkins on [http://localhost:8081/ci] and have it exposed at [http://localhost:80/hudson]). [http://localhost:80/jenkins]).

Set the context path by modifying the hudson.xml configuration file and adding \--prefix=/hudson (or similar) to the <arguments> entry.{note}