您的位置:首页 > 运维架构 > Nginx

codeigniter在nginx下控制器404错误

2014-03-04 09:29 204 查看
本文章并非原创,是收集网友的解决办法。

1.。。。。。

经查询资料发现nginx不支持path_info

解决的办法:

修改配置文件nginx.config

location ~ \.php($|/) {
root /usr/local/website;
set  $script     $uri;
set  $path_info  "";
if ($uri ~ "^(.+\.php)(/.+)") {
set  $script     $1;
set  $path_info  $2;
}
fastcgi_pass   127.0.0.1:9000;
include        fastcgi_params;
fastcgi_param  PATH_INFO                $path_info;
fastcgi_param  SCRIPT_FILENAME          $document_root$script;
fastcgi_param  SCRIPT_NAME              $script;
}


除了root以外,别的需要一样,特别是location ~ \.php($|/)

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

2.。。。。。。。。。。

nginx版本:1.2.1 codeigniter:2.1.2

nginx需要增加以下:

location ~ \.php {
#root           ;
fastcgi_pass   127.0.0.1:9000;
include fastcgi_params;
fastcgi_index  index.php;
set $path_info "";
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param PATH_INFO  $path_info;
fastcgi_param SCRIPT_NAME $real_script_name;
#fastcgi_param SCRIPT_FILENAME $real_script_name;
#fastcgi_param REQUEST_URI $real_script_name;
}


注意:

1.是~\.php而不是~\.php$

2.include fastcgi_params需要放在特殊设置的变量之前

说明:codeigniter内部是根据SCRIPT_NAME和REQUEST_URI来识别controller和method的。

具体是根据REQUEST_URI-SCRIPTNAME,剩下的字符串作为directory/class/method的各个字段,以‘/’分割,

所以我们需要设置REQUEST_URI和SCRIPT_NAME

上述代码片段是将url中的.php之前的作为SCRIPT_NAME,整个作为REQUEST_URI,

这样codeigniter就可以正常识别了。

-----------

3.。。。。。。。。。

最近研究CI框架,发现这个框架的路由功能在Nginx下有问题,报404错误,后来在网上查资料,发现需要开启PATH_INFO。在nginx7.16以后貌似就支持PATH_INFO了,只需要在配置文件中开启即可。

打开nginx.conf文件,在你的虚拟主机下增加重写规则,代码如下:

server {
listen       80;
server_name    www.ci.com;
location / {
root   d:/www/Codeigniter_2.0.1/;
index  index.html index.htm index.php;
rewrite ^/$ /index.php last;
rewrite ^/(?!index\.php
24000
|robots\.txt|images|js|styles)(.*)$ /index.php/$1 last;
}
location ~ ^(.+\.php)(.*)$ {
root       D:/www/Codeigniter_2.0.1/;
fastcgi_index    index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param    SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param    PATH_INFO        $fastcgi_path_info;
fastcgi_param    PATH_TRANSLATED    $document_root$fastcgi_path_info;
fastcgi_pass    127.0.0.1:9002;
include    fastcgi_params;
}
}
4.。。。。。。。。。

还提供一个更简单的方法

今天整了一下Nginx+CodeIgniter,发现不管你在 URL 里面写什么都只是出现缺省页面,后来想了一下Nginx不支持PATH_INFO变量,解决这个问题的第一步是打开application/config/config.php 文件,查找 URI
Protocol 信息。在那里推荐你去尝试一些其他的设置方法。如果这些方法都无效,你就需要让 CodeIgniter 去强行加一个问号去标记你的 URL。为了做到这点,打开你的 application/config/config.php 文件把里面的:

修改成:

5.。。。。。。。。

codeigniter(ci)在nginx下返回404的处理方法即codeigniter在nginx下配置方法

进入nginx的配置文件 加上一句(本来就有这句,只需要修改一下就行了)

location / {

  index index.php;

  if (!-e $request_filename) {

    rewrite ^/(.*)$ /index.php?$1 last;

    break;

  }

}


附送一篇老外写的CodeIgniter
NGINX Rewrite Rules

If you are a CodeIgniter enthusiast, you know that finding rewrite rules for Apache (htaccess file) is relatively easy, but doing the same for Nginx rewrite rules is not all that simple. Use these Nginx rewrite rules to get your next CodeIgniter project up
and running.

So I had a little fun with Nginx this
weekend, I setup a rackspace
server, installed Nginx (with PHP and MySQL), and installed CodeIgniter.

If you are a CodeIgniter enthusiast, you know that finding
rewrite rules for Apache(htaccess file) is relatively easy, but doing the same for Nginx rewrite rules is not all that simple. The rules that follow are from my original article: CodeIgniter
htaccess (for Apache)and are an exact translation into Nginx rewrite rules.


CodeIgniter Nginx Config

Here is the entire config file that I use. Remember that some of these rules are commented out, you will have to explicitly enabled them by removing the comments (removing the “#” from in front of the rule).


Some Very Important Notes

I spent nearly half a day testing out these rules, so here are some important things I noticed:

The PHP
DOCUMENT_ROOT
environment
variable is derived from the
root
parameter
and setting
root
on the
server
level
vs within a
location
block is important.

Using the rewrite rules at the
server
level
vs within
location
block is also
key in their proper functionality, certain rules will not work properly otherwise.


Nginx Rewrite Rule Breakdown

The first set of rules allow you to enforce “www” or enforce NO “www”, you will have to pick which you prefer and enable one or the other. Additionally, for those who may have subdomains, adding the subdomains to the list (if enforcing www) will safely exclude
those subdomains from being redirected.

I’ve also made some additions in an attempt to Canonicalize some
of the CodeIgniter URL end points. The key benefit of a Canonicalized URL is that your search engine page ranking (page juice) is not spread across several pages, but instead, targeted to a single page.

In your CodeIgniter project you will typically have a default
controller and you will be able to access this controller at the following URLs:

That’s a total of 6 URL end points pointing to the front controller. Additionally other controllers you’ve created will also have a default “index” method, consider the following:

Again, a total of 3 URL end points where there should only be one. These next set of rewrite rules prevent this:

With these rules the front controller will always be accessed at “/” and any other URLs pointing to a controller’s “index” method will be accessed at “/controller”. Notice that I also use a 301
redirect, this aids in maintaining or passing any existing search engine ranking (page juice) to the final redirected page.

So these:

… would simple become:

… and:

… would become:

Another important thing to note: for controllers with an “index” method which take any sort of parameter, these URLs will still work as such:

To be able to remove the “index” (or “welcome/index”) and still have parameters passed in, you will need to configure your routes
file.

The next rule prevents access to the
system
directory
and additionally allows you to have a
System.php
controller
file.

And the final rule is the catch all to route all requests to the bootstrap file.


Apply These Final Touches to Clean Up the URL

Remember, once you have your CodeIgniter Nginx config setup, you will want to go into your “/system/application/config/config.php”,
find the following:

and change it to:

Because you are using Nginx rewrites, the above will set your config file so that “index.php” will NOT show up as part of the URL as such:

instead your URLs will be cleaner:

If you have comments or improvement suggestions please let me know.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: