最近公司需要使用Thinkphp来做一个项目。开始为了方便,我选择了,apache的集成包。并配置了URL_MODEL=2,一切工作正常。
本地开发结束后需要将项目迁移到服务器,但服务器使用的是nginx。迁移之后发现apache下正常的功能都无法使用。nginx不支持Thinkphp 的工作模式
Windows下
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#这里是对URL的路径的解析,以及处理。
#
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
#如果只是简单的使用php,我们需要将这里注释掉.
# 这里配置当路径中包含.php的时候,将会交由php的CGI进行处理。
#所以我们在使用,跟姐此时必须先启动php的CGI服务器
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
更改为:
location / {
root html;
index index.html index.htm;
#如果请求既不是一个文件,也不是一个目录,则执行一下重写规则
if (!-e $request_filename)
{
#地址作为将参数rewrite到index.php上。
#rewrite ^/(.*)$ /index.php/$1;
#若是子目录则使用下面这句,将subdir改成目录名称即可。例如我的子项目名入口文件为localhost/abc/index.php,这里为
rewrite ^/abc/(.*)$ /abc/index.php/$1;
#如果还有另一个子项目,入口文件为localhost/test/efg/index.php,则为
rewrite ^/test/eft/(.*)$ /test/eft/index.php/$1;
}
}
#去掉$是为了不匹配行末,即可以匹配.php/,以实现pathinfo
#如果你不需要用到php5后缀,也可以将其去掉
location ~ .php
{
#原有代码
#定义变量 $path_info ,用于存放pathinfo信息
set $path_info "";
#定义变量 $real_script_name,用于存放真实地址
set $real_script_name $fastcgi_script_name;
#如果地址与引号内的正则表达式匹配
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
#将文件地址赋值给变量 $real_script_name
set $real_script_name $1;
#将文件地址后的参数赋值给变量 $path_info
set $path_info $2;
}
#配置fastcgi的一些参数
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
}
之后重新加载下配置文件, nginx -s reload
Linux下在CGI略有不同
#fastcgi_pass 127.0.0.1:9000; // pass 不是这个
fastcgi_pass unix:/dev/shm/php-cgi.sock; // 而是这个或者
fastcgi_pass unix:/xxx/xxx/php-cgi.sock //视情况而定.参考默认。