CodeIgniter在Nginx下的路由问题与PATH_INFO支持
近期深入探究CodeIgniter(CI)框架时,注意到其路由机制在Nginx环境下存在兼容性问题,常引发404错误,经查阅相关资料后找到解决方案。
关键在于必须启用PATH_INFO支持。据悉,Nginx在7.16版本之后已具备对该特性的支持能力,仅需在配置文件中进行相应开启操作即可。
编辑nginx.conf配置文件,在对应的虚拟主机配置块内添加必要的URL重写规则,具体代码实现如下所示。
# 复制代码
具体配置代码如下:
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|robots\.txt|images|js|styles)(.*)$ /index.php/$1last;
}
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;
}
}