2

我想使用 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;
   }
}

但是,我想做的是在相对 url 上托管 Jenkins,比如/jenkins/. 但是,当我将位置指令更改为指向时/jenkins/,它会破坏一切。我怎样才能做出这个(希望是简单的)改变?

4

1 回答 1

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

于 2012-10-25T10:31:22.610 回答