抛出问题
由于
CodeIgniter
当初是设计在apache
的,而apache
对pathinfo
是支持比较好的,所以一切都很nice。但是当你把写好的代码放到nginx上,傻眼了,可能出了CodeIgniter的welcom之外,其他都是404错误。
问题原因
原因是默认
Nginx
不支持pathinfo
这种格式,当你浏览器里输入http:\xxx.xxx.com\index.php\pages\home
的时候,Nginx会认为你要访问index.php目录下的pages文件夹里的home
,所以会报404 not found错误
解决方法
解决方法肯定是要修改服务器的重定向规则,大概有两种思路,一种是改nginx安装目录里的
nginx.conf
文件,如果你用了虚拟主机的话就去nginx安装目录下的vhosts下找对应的*.conf更改即可。另外一种思路修改CI目录下的.htaccess
文件,参见:http://blog.csdn.net/freshlover/article/details/8977111
本文是第一种思路。在修改之前先来看下原始的conf文件
:
server{
listen 80;
server_name a.b.com;
access_log /data/wwwlogs/a.b.com.log combined;
index index.html index.htm index.php;
include /usr/local/nginx/conf/rewrite/none.conf;
include /usr/local/nginx/conf/rewrite/static.conf;
root /data/wwwroot/a.b.com;
location ~ [^/]\.php(/|$) {
#fastcgi_pass remote_php_ip:9000;
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
expires 30d;
access_log off;
}
location ~ .*\.(js|css)?$ {
expires 7d;
access_log off;
}
location ~ /\.ht {
deny all;
}
}
server {
listen 443;
server_name a.b.com;
ssl on;
access_log /data/wwwlogs/a.b.com.log combined;
index index.html index.htm index.php;
include /usr/local/nginx/conf/rewrite/none.conf;
include /usr/local/nginx/conf/rewrite/static.conf;
root /data/wwwroot/a.b.com;
ssl_certificate cert/a.b.com/channelcs.pem;
ssl_certificate_key cert/a.b.com/channelcs.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location ~ [^/]\.php(/|$) {
#fastcgi_pass remote_php_ip:9000;
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_read_timeout 1000;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
expires 30d;
access_log off;
}
location ~ .*\.(js|css)?$ {
expires 7d;
access_log off;
}
location ~ /\.ht {
deny all;
}
}
修改
php
支持pathinfo
再修改conf之前,找到php的
php.ini
文件(可能在php安装目录的etc目录也可能在lib文件夹下,看自己的配置),搜索:cgi.fix_pathinfo
将注释放开,并置为1:cgi.fix_pathinfo=1
修改conf之前有个问题要明确,那就是CI的根目录 是不是web的root目录,如果是的话,如下修改:
只需要增加如下即可,在location ~ .php$
之前增加一段:
//这个意思是,如果你浏览器里要访问的文件不存在时,会自动路由至web根目录下的index.php进行访问。
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
- 修改完毕之后
server{
listen 80;
server_name a.b.com;
access_log /data/wwwlogs/a.b.com.log combined;
index index.html index.htm index.php;
include /usr/local/nginx/conf/rewrite/none.conf;
include /usr/local/nginx/conf/rewrite/static.conf;
root /data/wwwroot/a.b.com;
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
location ~ [^/]\.php(/|$) {
#fastcgi_pass remote_php_ip:9000;
fastcgi_pass unix:/dev/shm/php-cgi.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
expires 30d;
access_log off;
}
location ~ .*\.(js|css)?$ {
expires 7d;
access_log off;
}
location ~ /\.ht {
deny all;
}
}