2

我无法绕过 Nginxs' try_files。最近我在我的开发机器上安装了 Nginx 以支持 Apache。我以前的工作方式是这样的:我会在我的 http 目录的子文件夹中引导一个项目。如果这涉及 laravel、wordpress、codeigniter 或任何其他使用前端控制器使 URL 更具可读性的框架,我会在该目录中添加一个 .htaccess 以将所有不存在的路径重写到 index.php。

据说,这在 Nginx 上非常简单:try_files $uri $uri/ /index.php?$query_string它应该可以工作。然而,这一切只是将所有内容重定向到$document_root. 例子:

文件结构:

  1. http/clients/cms/public/index.php前控制器
  2. http/clients/cms/public/some/application/url请求带有参数的someuriapplicationurl.
  3. http/clients/cms/public/images/image.png一个静态文件。
  4. http/clients/blog/index.php另一个前端控制器
  5. http/clients/blog/wp-content/image.png另一个静态文件
  6. http/clients/blog/some-article-title一个应该被定向到的“漂亮的网址”4

现在,当向 发出请求时2,try_files 检测到 /some、/some/application 和 /some/application/url 不存在。它现在将重定向到 /index.php。我希望它会重定向到public/目录中的 index.php,但事实并非如此。相反,它重定向到 $document_root 中的 index.php(恰好包含一个phpinfo();调用,因此很容易看出它出错了..)。

一个解决方案是在我的 Nginx 配置中创建多个位置块,但我不喜欢这样。它会给我的引导过程增加大量额外的工作。

有没有办法在目录中使用 try_files 以便它像 apache 替代品一样工作?

我的配置文件:

server {
    listen              80;
    server_name         localhost,imac.local;

    access_log          /var/log/nginx/access.log;
    error_log           /var/log/nginx/error.log;
    rewrite_log         on;

    root                /Volumes/Storage/Fabian/Dropbox/Sites;
    index               index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
        #echo $request_filename;
        #break;
    }

    location ~ \.php$ {
        fastcgi_pass                127.0.0.1:9000;
        fastcgi_index               index.php;
        #fastcgi_split_path_info        ^(.+\.php)(.*)$;
        include                     fastcgi_params;
        fastcgi_param               SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location ~ /\. {
        deny all;
        access_log off;
        log_not_found off;
    }
}

谢谢!

4

0 回答 0