1. 程式人生 > >Nginx配置CI框架問題(Linux平臺下Centos系統)

Nginx配置CI框架問題(Linux平臺下Centos系統)

項目 末尾 規則 -c 官方文檔 nbsp fas src 路由

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中明確指明。

Nginx配置CI框架問題(Linux平臺下Centos系統)