Nginx配置CI框架问题(Linux平台下Centos系统)

CI框架:官方文档 http://codeigniter.org.cn/user_guide/index.html

CI框架的数据流程图如下:

其中:index.php作为入口文件,在安装好CI框架后,index.php文件一般放置在Nginx服务器(其他服务器相同)所配置的web根目录下,Nginx配置文件在 xxx/nginx/conf/nginx.conf文件中,其中xxx为安装路径,如配置.php的解析文件可用如下模板:

 1 server {
 2         listen       80;  // 监听的端口
 3         root /dir_1/dir_2/dir_3; 
 4         server_name www.example.com;
 5         index index.html index.htm index.php;
 6 
 7         location /
 8         {
 9             try_files $uri $uri/ /index.php?$args;
10         }
11         location ~ .php$
12         {
13             fastcgi_pass 127.0.0.1:9000;
14             fastcgi_index index.php;
15             include fastcgi.conf;
16         }
17 }

其中index.php文件要放置在根目录 /dir_1/dir_2/dir_3 下。URL为www.example.com:port/index.php/class/function/arg或者www.example.com/class/function/arg,其中www.example.com可换成ip地址加端口号,Nginx默认监听端口号为80,所以当端口为80时可不加,其他端口要在URL中明确指明

————————————————————————————————————————————————————————

而当有多个CI框架项目都需要布置在Nginx服务器是时,有两种方法:

1. 在Nginx配置文件中再配置一个虚拟机,并重新设置根目录,监听尚未使用的端口号(不推荐)

2. 不需要更改根目录,需要使用rewrite 指令和修改CI框架中的路由规则,即$route数组,具体如下:

例如当index文件所在位置为 /dir_1/dir_2/dir_3/test1/test2/test3/index.php

首先:将Nginx配置文件改为:

server {
          listen       80;  // 监听的端口
          root /dir_1/dir_2/dir_3; 
          server_name www.example.com;
          index index.html index.htm index.php;
  
          location /
          {
              try_files $uri $uri/ /index.php?$args;
          }
          location ~ .php$
          {
              fastcgi_pass 127.0.0.1:9000;
              fastcgi_index index.php;
              include fastcgi.conf;
          }
          location /test1/test2/test3/
         {
             rewrite ^/test1/test2/test3/(.*)$ /test1/test2/test3/index.php/$1 break;
             fastcgi_index index.php;
             fastcgi_pass  127.0.0.1:9000;
             include fastcgi.conf;    // fastcgi.conf为php解析库相关文
     }
 }

其中fastcgi.conf为自己定义的,如若无,可换成:

1 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
2 include fastcgi_params;

PS: 每次配置好Nginx后,记得要重启Nginx,使配置生效!

然后,修改CI框架中路由规则,在CI的Application/conf目录下,找到routes.php文件,在末尾添加:

$route['test1/test2/test3/(.+)'] = "$1";

之后 URL可为www.example.com:port/test1/test2/test3/class/function/arg,查看php的www.access.log可发现,nginx已经将链接重写为www.example.com:port/test1/test2/test3/index.php/class/function/arg,同样的由于Nginx默认监听端口号为80,所以当端口为80时可不加,其他端口要在URL中明确指明。

只为训练自己,时刻锤炼一个程序员最基本的技能!
原文地址:https://www.cnblogs.com/coding-wtf/p/7219191.html