使用nginx代理Jenkins

Naf*_*Kay 2 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;
   }
}
Run Code Online (Sandbox Code Playgroud)

但是,我想做的是主持詹金斯的相对网址,比如/jenkins/.但是,当我将location指令更改为指向时/jenkins/,它会破坏所有内容.我怎样才能做到这一点(希望简单)改变?

cob*_*aco 8

问题在于

proxy_pass http://127.0.0.1:8080;
Run Code Online (Sandbox Code Playgroud)

你没有在这个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
Run Code Online (Sandbox Code Playgroud)

换句话说,它正在将/ 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/;
}
Run Code Online (Sandbox Code Playgroud)

因为这将是一个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
Run Code Online (Sandbox Code Playgroud)

如果添加斜杠不起作用,则必须通过配置jenkins以期望/ jenkins/url来在另一端更改它