本文介绍了转换Nginx的重写从"如果"到" try_files"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下NGINX重写规则这对于安装在一个子文件夹一个PHP脚本的伟大工程:

I have the following NGINX rewrite rule which works great for a PHP script installed in a subfolder:

location /subfolder/ {
  if (!-e $request_filename){
    rewrite ^/subfolder/(.*) /subfolder/index.php?do=/$1;
  }
}

但Nginx的维基说,使用如果是邪恶 http://wiki.nginx.org/IfIsEvil所以我尝试了以下

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

,但它不工作来代替上述之一,然而它可以用于字preSS和大部分的PHP脚本。如果有一种方法来翻译它使用try_files?

but it doesn't work to replace the one above, although it works for WordPress and most of the PHP scripts. If there a way to translate it to use "try_files"?

感谢您!

推荐答案

您想做的重写仅在文件不存在,把它作为在 try_files回退命名位置

You want to do the rewrite only if the file doesn't exist, use it as a named location for the fall back in try_files

location /subfolder {
    try_files $uri $uri/ @rewrite;
}

location @rewrite {
    rewrite ^/subfolder/(.*) /subfolder/index.php?do=/$1;
}

这篇关于转换Nginx的重写从"如果"到" try_files"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-25 01:34