我有兩個域website1.com
和website2.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不管請求的文件是什麼。
待辦事項WEBSITE1和網站2沒有單獨的服務器塊? – MatTheWhale
沒有。他們應該嗎?這是我第一次使用nginx進行嵌套。 –
如果你願意,你不必擁有所有'if($ http_host ==)'塊。他們可能是問題的原因 – MatTheWhale