2012-10-24 66 views
2

我想用nginx代理Jenkins。我已经在/etc/sites-available/jenkins在使用这个配置文件的这个工作版本:用nginx代理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/时,它将打破一切。我怎样才能做到这一点(希望很简单)?

回答

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 
在它传递的/詹金斯到您的应用程序。换句话说

我想添加一个斜线到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 

如果添加斜线不工作,你必须通过配置詹金斯在另一端,以改变它预计/詹金斯/网址

+1

非常正确。您还需要重新配置Jenkins以将所有网址指向'/ jenkins /'前缀,但除此之外效果非常好:) –