2016-01-08 61 views
15

兩個Web應用程序,我有兩個Web應用程序(node.js的表達應用程序),web1web2。這些網絡應用程序預計將託管在通常類似於​​和http://www.web2.com的網站上。我想將它們作爲https://www.example.com/web1https://www.example.com/web2作爲nginx反向代理的後臺託管。我不想將這兩個Web應用程序公開爲example.com上的兩個子域。如何服務於後面的nginx的反向代理

這裏是我的nginx的配置的片段(無SSL終端的細節),我曾希望做到這一點:

server { 
    listen 443; 
    server_name .example.com; 

    location /web1 { 
    proxy_pass http://www.web1.com:80; 
    } 

    location /web2 { 
    proxy_pass http://www.web2.com:80; 
    } 
} 

這工作,不同的是,Web應用程序使用相對鏈接。所以網絡應用程序web1將有一個相關的鏈接,如/js/script.js這將不會被正確處理。

什麼是實現這一目標的最佳/標準的方式?

+0

我不知道ngix,但在Web請求有HTTP參照那就是在網頁「前面的」當前請求。你能用ngix映射引用者嗎? – bdn02

+0

我看過一些地方認爲這是一個潛在的解決方案,但我不確定這可能如何實施。 – Erik

+0

看看這個:http://serverfault.com/questions/177304/different-nginx-rules-based-on-referrer – bdn02

回答

3

您應該能夠通過檢查$http_referer要做到這一點,是這樣的:

location/{ 
    if ($http_referer ~ ^http://(www.)?example.com/web1) { 
    proxy_pass http://www.web1.com:80; 
    } 

    if ($http_referer ~ ^http://(www.)?example.com/web2) { 
    proxy_pass http://www.web2.com:80; 
    } 
} 

瀏覽器將引薦被設置爲http://example.com/web1/some/page時,它請求/js/script.js所以應用程序不應該改變,除非他們需要在內部處理或關心引薦者。

的$ HTTP_REFERER似乎不容易在nginx的文檔找到,但在幾個網站中提到:

1

怎麼樣?

添加add_header Set-Cookie "site=web1;Path=/;Domain=.example.com;";location /web1 {...}(web2的太)。

添加maphttp

map $cookie_site $site { 
    default http://www.web1.com:80; 
    "web2" http://www.web2.com:80; 
} 

默認位置是該

location/{ 
    proxy_pass $site; 
} 

您可以直接cookie的值傳遞給proxy_pass。但是,使用地圖是更安全的方式。