2016-12-26 65 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)) –

相关问题