ThinkPHP的四种URL模式:0(普通模式);1(PATHINFO模式);2(REWRITE模式);3(兼容模式)
nginx需要PATHINFO模式,但需要更改nginx配置文件让其支持PATHINFO模式。
系统环境:
系统:CentOS-6.4-x86_64
web服务器:nginx1.2.7
PHP版本:PHP5.3.17
数据库版本:MySQL5.5.28
一、安装LNMP1.0一键安装包:
http://lnmp.org/install.html
按照以上版本安装环境
1.修改php配置文件php.ini,将其中cgi.fix_pathinfo = 0,值改为
重启php-fpm
2.修改ngnix配置文件cd /usr/local/nginx/conf/ngnix.conf
原代码
server
{
listen 80 default;
#listen [::]:80 default ipv6only=on;
server_name www.lnmp.org;
index index.html index.htm index.php;
root /home/wwwroot/default/www;
#error_page 404 /404.html;
//将这段修改成下面的代码
location ~ [^/]\.php(/|$)
{
# comment try_files $uri =404; to enable pathinfo
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fastcgi.conf;
#include pathinfo.conf;
}
修改后的代码
//被修改的代码
if ( !-e $request_filename ) {
rewrite ^/(.*)$ /index.php/$1 last;
break;
}
location ~* ^.+\.php
{
fastcgi_pass unix:/tmp/php-cgi.sock;
include fastcgi.conf; //导入文件 都在nginx.conf 同目录下
include pathinfo.conf; //导入文件
}
2.导入文件中代码
fastcgi.conf 中代码如下
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
pathinfo.conf 中代码如下
set $script $uri;
set $path_info "/";
if ($uri ~ "^(.+\.php)(/.+)") {
set $script $1;
set $path_info $2;
}
fastcgi_param PATH_INFO $path_info;
fastcgi_param SCRIPT_FILENAME $document_root/$script;
fastcgi_param SCRIPT_NAME $script;
3.重启ngnix. /root/lnmp restart
ThinkPHP就可以在nginx中运行了。