2017-08-07 60 views
1

我有兩個域website1.comwebsite2.com鏈接到我的服務器。nginx重定向取決於主機

我嘗試做以下重寫規則:

http://website1.com/   --> /website1/ (static) 
http://website2.com/   --> /website2/ (static) 

http://website1.com/app/  --> http://localhost:8080/web-app/web1/ 
http://website2.com/app/  --> http://localhost:8080/web-app/web2/ 

用戶會被重定向到nginx的或根據URL的應用程序服務器提供一個靜態的網站。

這裏是我試過到目前爲止:

location/{ 
     root html; 
     index index.html index.htm; 

     if ($http_host = website1.com) { 
      rewrite/ /website1/index.html break; 
      rewrite (.*) /website1/$1; 
     } 
     if ($http_host = website2.com) { 
      #same logic 
     } 
    } 

    location /app/ { 
     proxy_pass http://localhost:8080/web-app/; 
     proxy_set_header Host $host; 
     proxy_set_header X-Real-IP $remote_addr; 
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 

     if ($http_host = website1.com) { 
     rewrite /app/(.*) /$1   break; 
     rewrite /app  /index.html; 
     } 
     if ($http_host = website2.com) { 
      #same logic 
     } 
    } 

靜態部分看起來做工精細,但重定向的Web應用程序的一部分,似乎服務的index.html不管請求的文件是什麼。

+0

待辦事項WEBSITE1和網站2沒有單獨的服務器塊? – MatTheWhale

+0

沒有。他們應該嗎?這是我第一次使用nginx進行嵌套。 –

+0

如果你願意,你不必擁有所有'if($ http_host ==)'塊。他們可能是問題的原因 – MatTheWhale

回答

1

這不是一個明確的答案,而只是我對如何讓nginx代理工作的解釋。

root html; 
index index.html index.htm; 

server { 

    listen 80; 
    server_name website1.com; 
    location/{ 
     alias html/website1; 
    } 

    location /app/ { 
     proxy_pass http://localhost:8080/web-app/web1/ 
    } 
} 

server { 

    listen 80; 
    server_name website2.com; 
    location/{ 
     alias html/website2; 
    } 

    location /app/ { 
     proxy_pass http://localhost:8080/web-app/web2/ 
    } 
} 

問題看起來像是被這些重寫造成的:

rewrite /app/(.*) /$1   break; 
rewrite /app  /index.html; 

使用與server_name的服務器塊和alias指令,我們可以與需要使用那麼多的邏輯做了。讓我知道是否還有什麼還不清楚。

+0

我發現一個解決方案與這個稍有不同..因爲我發佈問題後我的需求發生了變化。你的答案似乎是正確的,謝謝。 –

1

我覺得你做錯了。如果主機之間存在如此多的差異,那麼擁有兩種不同的配置會更清晰和更高效,每個主機都有一個配置。另一方面,如果您的意圖是擁有多個幾乎完全相同的配置,那麼正確的解決方法可能是map,而不是if

返回到您的配置 - 我試着運行它只是爲了看看它是如何工作的,以及一件事,你可能會注意到的是,你的proxy_pass中指定的路徑實際上變成一個空操作,一旦內的$host特異性rewrite相同的上下文涉及到更改$uri - 這是設計,並在http://nginx.org/r/proxy_pass「當URI在代理位置內使用重寫指令」「更改時)非常清晰地記錄。

所以,實際上,使用下面的配置確實出現了堅持你的規格:

%curl -H "Host: host1.example.com" "localhost:4935/app/" 
host1.example.com/web-app/web1/ 

%curl -H "Host: host2.example.com" "localhost:4935/app/" 
host2.example.com/web-app/web2/ 

%curl -H "Host: example.com" "localhost:4935/app/" 
example.com/web-app/ 

下面是我使用的配置:

server { 
    listen [::]:4935; 
    default_type text/plain; 
    location/{ 
     return 200 howdy; 
    } 
    location /app/ { 
     proxy_set_header Host $host; 
     proxy_pass http://localhost:4936/web-app/;#path is NOOP if $uri get changed 
     if ($host = host1.example.com) { 
      rewrite /app/(.*) /web-app/web1/$1   break; 
      rewrite /app  /web-app/index.html; 
     } 
     if ($host = host2.example.com) { 
      rewrite /app/(.*) /web-app/web2/$1   break; 
      rewrite /app  /web-app/index.html; 
     } 
    } 
} 

server { 
    listen [::]:4936; 
    return 200 $host$request_uri\n; 
}