Naftuli Kay
Naftuli Kay

Reputation: 91790

Proxying Jenkins with nginx

I'd like to proxy Jenkins using nginx. I already have a working version of this using this configuration file in /etc/sites-available/jenkins:

server {
   listen 80;
   listen [::]:80 default ipv6only=on;

   location / {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_pass http://127.0.0.1:8080;
   }
}

However, what I'd like to do is host Jenkins at a relative url, like /jenkins/. However, when I change my location directive to point at /jenkins/, it breaks everything. How can I make this (hopefully simple) change?

Upvotes: 2

Views: 7557

Answers (1)

cobaco
cobaco

Reputation: 10556

the problem lies in

proxy_pass http://127.0.0.1:8080;

you're not setting a uri in this proxy_pass which according to http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass means:

If proxy_pass is specified without URI, a request URI is passed to the server in
the same form as sent by a client when processing an original request or the full
normalized request URI is passed when processing the changed URI

in other words it's passing on the /jenkins to your app

I think adding a slash to the proxy_pass should work, as follows:

location /jenkins/ {
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_pass http://127.0.0.1:8080/;
}

as that would be a request with a uri which according to the link above means:

If proxy_pass is specified with URI, when passing a request to the server, part 
of a normalized request URI matching the location is replaced by a URI specified 
in the directive

If adding the slash does not work, you'll have to change it at the other end by configuring jenkins to expect the /jenkins/ url's

Upvotes: 8

Related Questions