转载:Nginx部署CodeIgniter的转发配置

来源:互联网 发布:mac设置airdrop 编辑:程序博客网 时间:2024/05/17 02:01

原文地址:http://blog.csdn.net/yanzi1225627/article/details/49699483

由于CodeIgniter当初是设计在apache的,而apache对pathinfo是支持比较好的,所以一切都很nice。但是当你把写好的代码放到nginx上,傻眼了,可能出了CodeIgniter的welcom之外,其他都是404错误。而我惊奇的发现,CodeIgniter的官方文档竟然对在Nginx上的配置只字不提。而你百度”CodeIgniter Nginx 404”又能搜到一堆一堆的文章,奇葩的是几乎每个文档的配置方法貌似还不大一样。如果你搞好了还罢,搞不好就是配几个晚上都搞不定,像我一样。(本文服务器环境:CentOS,nginx-1.4.7,PHP-5.4.24,CodeIgniter3.0.2--当前最新版本)

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文件:

{       listen 80;       server_name 1.abc.com 2.abc.com;        root /a/domains/1.abc.com/public_html;        index index.html index.htm index.shtml index.php;        error_page  404               /404.html;    #Custom rules Start        #Custom rules End    location = /500.html {        root   /usr/share/nginx/html;    }    location ~ \.php$ {        fastcgi_pass   unix:/dev/shm/php.sock;        include        fastcgi_params;        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;        access_log     /a/apps/nginx/logs/1.abc.com.access.log main;    }    location ~ /\.ht {        deny  all;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

PS:上面为了隐私,我把server_name改为1.abc.com了,这个对应自己的server_name.
下面介绍修改方法,在修改conf前切记将自己的conf,cp为conf.back备份一份。
下面为修改方法:
1,修改php支持pathinfo
再修改conf之前,找到php的php.ini文件(可能在php安装目录的etc目录也可能在lib文件夹下,看自己的配置),搜索:cgi.fix_pathinfo
将注释放开,并置为1:cgi.fix_pathinfo=1

2,修改conf之前有个问题要明确,那就是CI的根目录 是不是web的root目录,如果是的话,如下修改:
只需要增加如下即可:
在location ~ .php$之前增加一段:

     if (!-e $request_filename) {          rewrite ^.*$ /index.php last;     }
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

这个意思是,如果你浏览器里要访问的文件不存在时,会自动路由至web根目录下的index.php进行访问。
当然上面这段话也可以用下面的来代替:

    location / {        try_files $uri</span> <span class="hljs-variable">$uri/ /index.php;    }
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

完整的配置为:

server{    listen 80;    server_name 1.sod90.com app.sod90.com;    root /a/domains/1.sod90.com/public_html;    index index.html index.htm index.shtml index.php;    error_page  404               /404.html;    #Custom rules Start        #Custom rules End        location = /500.html {        root   /usr/share/nginx/html;    }    #location / {    #    try_files $uri $uri/ /index.php;    #}     if (!-e $request_filename) {          rewrite ^.*$ /city52/index.php last;     }    location ~ \.php$ {        fastcgi_pass   unix:/dev/shm/php.sock;        include        fastcgi_params;        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;        access_log     /a/apps/nginx/logs/app.sod90.com.access.log main;    }    location ~ /\.ht {        deny  all;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33

注意:上面的try_files完全可以代替rewrite,参见链接
除了加这个重定向之外,conf里不需要再加任何奇奇怪怪的东西。
然后检查CI的config文件里以下三个参数:

$config['base_url'] = 'http://1.abc.com/';$config['index_page'] = '';$config['uri_protocol']    = 'REQUEST_URI';
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

这三个参数比较关键,其中第一个是web根目录对应的域名 ,index_page要为”,不要为默认值 ‘index.php’.
经过以上设置就ok了,url地址里不需要写index.php了。

延伸

现在考虑这种情况,如果一个后台,分支持app和web的,有时候用不同的框架也是在所难免。把所有框架都放在根目录下也不太好看。如果我的CI的根目录不是web的根目录,而是如public_html下的xxx文件夹,此时只需将conf里的try_files语句里路由的/index.php改为/xxx/index.php即可。如下:

    location / {        try_files $uri</span> <span class="hljs-variable">$uri/ /xxx/index.php;    }
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

再CI的config.php里,写成

$config['base_url'] = 'http://1.abc.com/';
  • 1
  • 1

或者:

$config['base_url'] = 'http://app.sod90.com/xxx/';
  • 1
  • 1

都是可以的,只要路由对了,就没问题。但是为了保险起见,base_url如后者比较好,然后url里就不要再带index.php了。两者的区别还体现在当使用CI的函数如base_url()时得到的值将会不一样。参考链接 。

附一个国外比较靠谱点的链接:
http://stackoverflow.com/questions/8182868/nginx-configuration-avoid-codeigniter-rewrite-rules
CI的多目录:http://blog.sina.com.cn/s/blog_6ec2ae900101kbx9.html

补充说明,一般来说conf里如下三句话是关键:

    fastcgi_param  PATH_INFO $fastcgi_path_info;    fastcgi_split_path_info ^(.+\.php)(.*)$;    fastcgi_param  PATH_TRANSLATED $document_root</span><span class="hljs-variable">$fastcgi_path_info;
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3

但是我的conf里只用了下面一句:

fastcgi_param  SCRIPT_FILENAME  $document_root</span><span class="hljs-variable">$fastcgi_script_name;
  • 1
  • 1
原创粉丝点击