Discuz教程网

让Nginx支持ThinkPHP的URL重写和PATHINFO的方法分享

[复制链接]
authicon dly 发表于 2011-9-3 18:58:49 | 显示全部楼层 |阅读模式
网上搜了很多方法都不奏效,研究了一天,发现通过以下的配置可以完美支持 'URL_MODEL' => 2 的情况了
代码如下:

  1. location /project/ {
  2. index index.php;
  3. if (!-e $request_filename) {
  4. rewrite ^/project/(.*)$ /project/index.php/$1 last;
  5. break;
  6. }
  7. }

  8. location ~ .+\.php($|/) {
  9. set $script $uri;
  10. set $path_info "/";
  11. if ($uri ~ "^(.+\.php)(/.+)") {
  12. set $script $1;
  13. set $path_info $2;
  14. }

  15. fastcgi_pass 127.0.0.1:9000;
  16. fastcgi_index index.php?IF_REWRITE=1;
  17. include /APMServ/nginx/conf/fastcgi_params;
  18. fastcgi_param PATH_INFO $path_info;
  19. fastcgi_param SCRIPT_FILENAME $document_root/$script;
  20. fastcgi_param SCRIPT_NAME $script;
  21. }
复制代码




这里先把project下的请求都转发到index.php来处理,亦即ThinkPHP的单一入口文件;然后把对php文件的请求交给fastcgi来处理,并且添加对PATH_INFO的支持。
重启Nginx以后,http://localhost/project/Index/insert, http://localhost/project/index.php/Index/delete 这样的URL都可以正确访问了。
还有一个地方需要注意的是,Nginx配置文件里 if 和后面的括号之间要有一个空格,不然会报unknown directive错误。




上一篇:ThinkPHP与PHPExcel冲突解决方法
下一篇:PHP 防注入函数(格式化数据)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 20:08

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表