2017-01-12 32 views
2

這已經被問過SO before但答案還沒有被推廣。所以在這裏。傳遞到腳本前如何刪除網址的子目錄?

我有兩個不同的網絡應用程序。他們被製作在不同的服務器上。但現在,他們都將被放置到子目錄在同一臺服務器上。下面的腳本發送帶有URI對腳本一起子目錄。這是個問題。

舊網址:

新網址:

該商店網站看到/store/items/1時,它只想看到/items/1。後臺網站也是如此。

我嘗試:

location /store { 
      try_files @store_site; 
    } 

    location /backoffice { 
      try_files @backoffice_site; 
    } 

    location @store_site { 
      include uwsgi_params; 
      uwsgi_pass unix:/var/run/uwsgi/store_site.sock; 
      proxy_set_header Host $host; 
    } 

    location @backoffice_site { 
      include uwsgi_params; 
      uwsgi_pass unix:/var/run/uwsgi/backoffice_site.sock; 
      proxy_set_header Host $host; 
    } 

再次,商店網站越來越網址與/store前綴和後臺越來越/backoffice。這些網站被編碼爲不期望這些前綴。我需要發送到現場前,除去/store/backoffice前綴。

這是一個重寫規則的事情嗎?

我試過此基礎上this SO page並沒有奏效。我不能理解語法的一些東西。

location /store { 
     rewrite ^/store/(.*) /$1; 
     try_files $uri @store_site; 
} 

更新

顯然,這個工作(加入break;)這是一個完整的解決方案?

location /store { 
     rewrite ^/store/(.*) /$1 break; 
     try_files $uri @store_site; 
} 
+0

更多或更少的完整的解決方案。取決於你的網站的細節。 – Dayo

回答

2

你需要重寫的URI,而不會觸發重定向通過使用break指出。

根據您設立的細節,一切是否應該去後臺或Nginx的是否應該成爲一些要求,如靜態文件,你需要或者......

location /store { 
    try_files $uri @store_site; 
} 

location /backoffice { 
    try_files $uri @backoffice_site; 
} 

location @store_site { 
    rewrite ^/store/(.*) /$1 break; 
    include uwsgi_params; 
    uwsgi_pass unix:/var/run/uwsgi/store_site.sock; 
} 

location @backoffice_site { 
    rewrite ^/backoffice/(.*) /$1 break; 
    include uwsgi_params; 
    uwsgi_pass unix:/var/run/uwsgi/backoffice_site.sock; 
} 

...或

location /store { 
    rewrite ^/store/(.*) /$1 break; 
    include uwsgi_params; 
    uwsgi_pass unix:/var/run/uwsgi/store_site.sock; 
} 

location /backoffice { 
    rewrite ^/backoffice/(.*) /$1 break; 
    include uwsgi_params; 
    uwsgi_pass unix:/var/run/uwsgi/backoffice_site.sock; 
} 
+0

根據我的回答,如果你沒有結尾斜槓,nginx匹配/ store和/ store5,和/ storewhatever等。 – gubble

1

我建議你試試這一個項目:

location /store/ { 
    rewrite ^/store/(.*) /$1 break; 
    include uwsgi_params; 
    proxy_set_header Host $host; 
    uwsgi_pass unix:/var/run/uwsgi/store_site.sock; 
} 

沒有測試過,但我認爲它應該工作。

+0

我想避免在這裏提到商店的頂級網址。這是因爲它們中有很多,我最終會枚舉它們。有沒有辦法只是刪除/存儲部分,並一起發送? – 010110110101

+0

我明白了。我已經稍微改變了它,但是肯定商店的位置應該在雙方都有/斜槓,否則/ storeajshas也會匹配 – gubble