2016-12-26 80 views
1

我有一個開發服務器,我上傳客戶端網站,每個WordPress站點都有自己的DB和目錄(每個站點都是獨立的)。我有一個服務器塊。我的問題是與漂亮的永久鏈接,這個工作對我來說:NGINX配置爲子目錄中的多個WordPress網站自動漂亮鏈接

server_name dev.example.tld; 

    location = /favicon.ico { log_not_found off; access_log off; } 
    location = /robots.txt { log_not_found off; access_log off; allow all; } 

    location/{ 
      try_files $uri $uri/ =404; 
      add_header 'Cache-Control' 'no-store, no-cache, must-revalidate, proxy-revalidate, max-$ 
      expires off; 
    } 

    location /site_1/ { 
      index index.php; 
      try_files $uri $uri/ /site_1/index.php?$args; 
    } 

    location /site_2/ { 
      index index.php; 
      try_files $uri $uri/ /site_2/index.php?$args; 
    } 

我一直在尋找一種方式來做到這一次,而不是添加位置塊爲每個站點,但沒有運氣的。

回答

1

你可以改變location爲正則表達式和捕捉站點名稱:

location ~ ^(?<site>/[^/]+) { 
    try_files $uri $uri/ $site/index.php?$args; 
} 

另外,通過使用重寫達到相同的效果:

location/{ 
    try_files $uri $uri/ @rewrite; 
} 
location @rewrite { 
    rewrite ^(/[^/]+) $1/index.php last; 
    return 404; 
} 
+0

了'site'變量是url例如** dev.example.tld **? 當我用它顯示一個錯誤: – Ritterwise

+0

對不起,第二個例子中的剪切和粘貼錯誤,它當然應該是'$ 1'。 '$ site'或'$ 1'是URI的第一個路徑段 - 例如'/ site_1'或'/ site_2'在你的情況下。如果您需要服務器名稱,請使用:'$ host','$ server_name'或'$ http_host'(這些都略有不同 - 請參閱[此鏈接瞭解詳情](http://nginx.org/en/docs/http /ngx_http_core_module.html#var_host)) –

相關問題