现在使用的nginx,这里以版本为1.10.3的nginx为例,现在增加了try_files语法,比原来的rewrite路由的方式更加高效。
CI框架使用MVC框架,框架需要单一入口来处理请求,对于不存在的文件要做一下重新路由的功能,这是必需的。
为了支持CodeIgniter框架, linux的 nginx 增加了一项配置,如下:
server {
listen 8000 default_server;
server_name newcar.site;
root /var/www/newcar;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
}
}
其中标黄的部分表示的意思其实就是rewrite机制,如果请求的文件不存在而且文件夹也不存在时,才会跳转到/index.php页面
Windows下nginx的配置:
server {
listen 80;
server_name newcar.xin.com ;
root "D:/phpStudy/WWW/x.xin.com";
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php(.*)$ {
include uxin-conf.d/www_newcar.conf;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.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;
}
}