Home > Backend Development > PHP Tutorial > Deploy ThinkPHP on Nginx and solve the Pathinfo problem_PHP tutorial

Deploy ThinkPHP on Nginx and solve the Pathinfo problem_PHP tutorial

WBOY
Release: 2016-07-13 09:45:55
Original
1049 people have browsed it

在Nginx上部署ThinkPHP,解决Pathinfo问题

在Nginx上部署ThinkPHP,解决Pathinfo问题

 


其实,要解决nginx不支持pathinfo的问题,有两个解决思路,一是不使用pathinfo模式,二是修改nginx的配置文件,使它支持pathinfo。为了使问题简单化,我选择了第一种方式,因为就第二种方式,我查了很多资料,发现大家的方法不尽相同,有的还差别很大,容易造成误导,所以我选择从简出发,选择普通模式,虽然有一定的风险。当把index.php对应的前台代码修改完毕之后,发现前台基本正常,可是后台仍然出现重定向的问题。折腾了半天之后,我才想到看一下日志文件,原来是编辑器的问题,看来日志文件真的很重要,以前一直不重视。在config.php文件的第一行出现了输出,
Deploy ThinkPHP on Nginx and solve the Pathinfo problem_PHP tutorial

在sublime下,一般会为UTF-8文件添加BOM头,这个BOM头在window下通常是看不见的,可以通过其他的编辑器查看到,Linux下也可以直接看到,通常显示出来是一个乱码字符,把这个字符删除即可,或者简单一点,直接在第一行回车,再删除就可以了。到这里,后台基本可以访问了。

有几个小问题需要说明一下(参考:http://www.lai18.com/content/368727.html)

1.在登录的时候,我是通过外部js文件发送Ajax请求进行验证的,在js与ThinkPHP模块函数通信遇到了点问题,一直不知道正确的路径该怎么写,也没有查到相关资料,只能各种试,好在找到了解决办法,通过直接带上入口文件名的方式,代码如下

var url=system.php?m=Login&a=doLog;

 $.post(url,{staffname:$staffname,staffpwd:$staffpwd,verifycode:$verifycode},function(data){

     if(data==codeerr){

           alert(验证码错误!);

     }else if(data==authempty){

           alert(请输入用户名或密码!)

     }else if(data==autherr){

           alert(用户名或密码错误!);

     }else if(data==success){

           alert(登录成功!);

            location.href=system.php?m=Index&a=index;   //访问首页

     }
Copy after login

当然,此为普通模式下的访问方式,如果是pathinfo的话,只需要把红色部分如下修改即可

var url=doLog;

 $.post(url,{staffname:$staffname,staffpwd:$staffpwd,verifycode:$verifycode},function(data){

       if(data==codeerr){

               alert(验证码错误!);

       }else if(data==authempty){

               alert(请输入用户名或密码!)

       }else if(data==autherr){

               alert(用户名或密码错误!);

       }else if(data==success){

               alert(登录成功!);

               location.href=../Index/index;   //跳转首页,访问其他模块的方法
Copy after login

2.下载文件的时候,总是莫名多出许多html的东西,原因是缓冲区没有清空,可以通过以下代码进行修改,不过这种方式实际上是下载的仍然是html格式的文件,只不过改了一下后缀名为xls而已,因而用excel打开的时候会提示格式问题,忽略即可。同时需要注意使用 icov()函数转换编码,因为xls默认编码格式并非utf-8.

ob_start();

ob_end_clean();

Header( Content-type: application/octet-stream); 

Header( Accept-Ranges: bytes ); 

Header( Content-type:application/vnd.ms-excel;charset=gb2312);   

Header( Content-Disposition:attachment;filename={$filename}.xls);
Copy after login

3.在删除文件时会遇到路径问题,因为项目中使用的较多的是相对路径,即相对入口文件而言,但是删除文件则需要使用绝对路径,我并没有找到合适的解决方法,只好用了比较保守的方式,代码如下

$path=./Public/uploads/;    

 $path=str_replace(Deploy ThinkPHP on Nginx and solve the Pathinfo problem_PHP tutorial,/,realpath($path)./);  //获取绝对路径,并转换分隔符
Copy after login

4.在配置nginx和php方面,我使用了fastCGI的方式,将如下代码保存为cmd文件,直接点击运行就可以了

F:phpphp-cgi.exe -b 127.0.0.1:9000 -c F:phpphp.ini //后面是php文件的路径

然后在nginx的配置文件里加上几句话
location ~ .php/?.* {

            root           myapplications;

            fastcgi_pass   127.0.0.1:9000;

             fastcgi_index  index.php;

             fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;

            include        fastcgi_params;

            #定义变量 $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;

         }
Copy after login


 

延伸阅读

《PHP框架ThinkPHP学习》系列技术文章整理收藏

1Thinkphp实现MySQL读写分离操作示例

2使用ThinkPHP+Uploadify实现图片上传功能

3ThinkPHP调用百度翻译类实现在线翻译

4Thinkphp使用mongodb数据库实现多条件查询方法

5ThinkPHP实现多数据库连接的解决方法

6改写ThinkPHP的U方法使其路由下分页正常

7ThinkPHP实现将SESSION存入MYSQL的方法

8ThinkPHP连接数据库及主从数据库的设置教程

9ThinkPHP中pathinfo的访问模式、路径访问模式及URL重写总结

10ThinkPHP基于PHPExcel导入Excel文件的方法

11thinkphp获取栏目和文章当前位置的方法

12ThinkPHP结合AjaxFileUploader实现无刷新文件上传的方法

13ThinkPHP无限级分类原理实现留言与回复功能实例

14ThinkPHP在新浪SAE平台的部署实例

15ThinkPHP控制器间实现相互调用的方法

16ThinkPHP实现带验证码的文件上传功能实例

17ThinkPHP写数组插入与获取最新插入数据ID实例

18ThinkPHP水印功能实现修复PNG透明水印并增加JPEG图片质量可调整

19thinkphp使用literal防止模板标签被解析的方法

20ThinkPHP中RBAC类的四种用法分析

21ThinkPHP中__initialize()和类的构造函数__construct()用法分析

22ThinkPHP自动完成中使用函数与回调方法实例

23ThinkPHP’s method of dynamically including files

24thinkphp implements sending email password retrieval function example

25ThinkPHP implementation example of Alipay interface function

26 How to maintain conditional paging on home page and search page during Thinkphp search

27ThinkPHP entry file settings and related precautions analysis

28Detailed explanation of variable output of ThinkPHP template engine

29thinkphp method to implement the previous and next articles

30 How to receive the value passed by alipay after hiding the entry file in url in ThinkPHP

31ThinkPHP solution to the problem that garbled characters appear when opening the verification code page

32 How to use ajax to receive json data in ThinkPHP

33Defect analysis of ThinkPHP’s built-in jsonRPC

34 Solution to the problem that thinkphp3.0 output is repeated twice

35thinkPHP implements automatic form verification

36 Practical points of curd application in Thinkphp

37 A brief discussion on thinkphp’s instantiation model

38ThinkPHP 404 page setting method

39THINKPHP content pagination code sharing

40 tutorial on deploying ThinkPHP project on Nginx

41 A brief analysis of the maximum data amount supported by THINKPHP’s addAll

Solution to 42ThinkPHP prompt error Fatal error: Allowed memory size

43ThinkPHP3.2.2 plug-in controller function

44ThinkPHP3.2.3 new database setting features

45What has been upgraded in ThinkPHP 3.2 version

46thinkPHP Study Notes Installation and Configuration

47Thinkphp calls the Image class method to generate thumbnails

48 Summary of solving the problem of error when ThinkPHP closes debugging mode

49ThinkPHP file caching code sharing

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1036921.htmlTechArticleDeploy ThinkPHP on Nginx to solve the Pathinfo problem Deploy ThinkPHP on Nginx to solve the Pathinfo problem In fact, there is no need to solve nginx There are two solutions to the problem of supporting pathinfo. One is not to use...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template