2012-10-24 90 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 /'前綴,但除此之外效果非常好:) –