使用nginx代理Jenkins

时间:2012-10-24 18:39:16

标签: nginx jenkins

我想使用nginx代理Jenkins。我已在/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;
   }
}

但是,我想做的是在相对网址上托管Jenkins,例如/jenkins/。但是,当我将我的位置指令更改为指向/jenkins/时,它会破坏所有内容。我怎样才能做到这一点(希望很简单)?

1 个答案:

答案 0 :(得分:8)

问题在于

proxy_pass http://127.0.0.1:8080;

你没有在这个proxy_pass中设置一个uri,根据http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass表示:

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

换句话说,它将/ jenkins传递给你的应用

我认为在proxy_pass中添加斜杠应该可以正常工作,如下所示:

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/;
}

因为这将是一个uri的请求,根据上面的链接意味着:

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

如果添加斜杠不起作用,则必须通过配置jenkins以期望/ jenkins / url

来在另一端更改它。