nginx 子文件夹 URL 重写

nginx 子文件夹 URL 重写

我有 2 个子文件夹,它们在 my_url/test1 和 my_url/test2 上打开

location /test1 {
    index index.html index.php; ## Allow a static html file to be shown first
    try_files $uri $uri/ /test1 /index.php;
    expires 30d; ## Assume all files are cachable
}

location /test2 {
    index index.html index.php; ## Allow a static html file to be shown first
    try_files $uri $uri/ /test2/index.php;
    expires 30d; ## Assume all files are cachable
}

我想添加 3 个额外的测试子文件夹(test3、test4、test5)我可以继续写:

location /test3 {
    index index.html index.php; ## Allow a static html file to be shown first
    try_files $uri $uri/ /test3/index.php;
    expires 30d; ## Assume all files are cachable
}

等等。但可能有一些标准方法可以避免重复配置吗?比如

location /(subfolder) {
    index index.html index.php; ## Allow a static html file to be shown first
    try_files $uri $uri/ /(subfolder)/index.php;
    expires 30d; ## Assume all files are cachable
}

答案1

您几乎在最后一个位置语句中得到了它,只需替换/(subfolder)/,并将行上的子文件夹字符串替换try_files为为/$uri/index.php而不是/(subfolder)/index.php

location / {
  index index.html index.php;
  try_files $uri $uri/ /$uri/index.php;
  expires 30d;
}

相关内容