将 apache 重写转换为 nginx 重写

lov*_*esh 2 rewrite nginx

我有一个项目,我必须将所有未找到的文件重定向到 index.php。我在 apache 中做了这个,在我的项目文件夹中放置了一个 .htaccess 文件。文件的内容是——

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Run Code Online (Sandbox Code Playgroud)

现在我想在 nginx 中使用同样的方法。这是我的 nginx.conf 的样子

root   /usr/local/apache2/htdocs;
index  index.php index.html index.htm;
location /project/ {
            root   /usr/local/apache2/htdocs;
            index  index.php index.html index.htm;
            try_files $uri $uri/ index.php;
}
Run Code Online (Sandbox Code Playgroud)

现在每当我提出这样的请求时,http://localhost/project/hello这个请求应该去,http://localhost/project/index.php但这说File not found

现在为什么我认为这会起作用是因为由于rootlocation 块内的指令覆盖root了块外的指令(尽管值相同),重写模块将查找/usr/local/apache2/htdocs/project/index.php. 我在这里缺少什么?

更新

这个配置有效

root   /usr/local/apache2/htdocs;
location /project/ {
            index  index.php index.html index.htm;    #effectively root here is /usr/local/apache2/htdocs
            try_files $uri $uri/ /project/index.php;
 }
Run Code Online (Sandbox Code Playgroud)

但我不明白为什么这应该工作,因为作为文档指出,这里的位置块匹配目录附加到的价值root指令

Jef*_*and 5

 location /project/ {
             root   /usr/local/apache2/htdocs;
             index  index.php index.html index.htm;
-            try_files $uri $uri/ index.php;
+            try_files $uri $uri/ /project/index.php;
 }
Run Code Online (Sandbox Code Playgroud)

也许您需要在 URL 中指定根?否则,该 URL 没有参考点。服务器无法猜测它最后进入的目录或用户想要的目录。如果这不能完成您想要的,请发表评论,我将启动服务器进行快速测试。