我的 htaccess mod_rewrite 的 nginx 等于什么?

Aru*_*vid 3 php nginx .htaccess codeigniter apache-2.2

以前,我在我的 CodeIgniter 网站上使用运行 Apache 的 linode VPS。今天我安装了 nginx,我的网站登陆页面即将到来,但其他使用 htaccess 重写 URL 的页面却没有出现。这是我的htaccess,

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1
Run Code Online (Sandbox Code Playgroud)

我的 htaccess 的 nginx 等价物是什么?我还需要在我的 codeigniter 配置和应用程序中进行任何更改吗??..

对于 codeigniter 配置中的 URI 协议,我正在使用,

$config['uri_protocol'] = 'PATH_INFO';
Run Code Online (Sandbox Code Playgroud)

这可以与 nginx 一起使用吗??..

cyb*_*x86 7

由于没有对应的 .htaccess 文件(即没有目录级别的配置文件),您需要更新主配置并重新加载 nginx 以使任何更改生效。

上面的 Apache 配置本质上是“如果提供的路径不是现有文件或目录,则重定向到 index.php,附加路径”。

在 Nginx 中,您将使用该try_files指令来完成同样的事情:

location / {
        try_files $uri $uri/ @ci_index;
}

location @ci_index{
        rewrite ^(.*) /index.php?$1 last;
}
Run Code Online (Sandbox Code Playgroud)

与 Apache 不同 -在 Nginx 配置中最好避免使用if 语句。

在某些设置中,您可能能够避免命名索引和重写,而只需将其/index.php用作第三条路径try_files(并且 codeigniter 应该从$_SERVER[$config['uri_protocol']].

至于 PATH_INFO 的使用 - 检查您的 fastcgi_params 文件(您希望包含在您的 php 位置块中)并查看:

fastcgi_param  PATH_INFO          $fastcgi_path_info;
Run Code Online (Sandbox Code Playgroud)

您也可以使用 $config['uri_protocol'] = "REQUEST_URI"

对于您选择的任何选项,请检查 的输出print_r($_SERVER)以验证已设置哪些服务器变量,以及已设置的内容(它们应与您在 PHP 位置块和 fastcgi_params 文件中指定的任何内容相匹配)。