Heim > Artikel > Backend-Entwicklung > 在Nginx上配置ThinkPHP项目,nginxthinkphp项目_PHP教程
前段时间用Apache配合TP开发了一个小型网站,也算是我使用TP的第一次实战。我习惯使用pathinfo模式,本地运行一切正常,然而部署到服务器上时,由于对方使用的是Nginx,默认是不支持ThinkPHP的pathinfo模式,所以导致路径错误,出现了页面循环跳转的问题,于是就把nginx下载下来在本地亲自测试。
其实,要解决nginx不支持pathinfo的问题,有两个解决思路,一是不使用pathinfo模式,二是修改nginx的配置文件,使它支持pathinfo。为了使问题简单化,我选择了第一种方式,因为就第二种方式,我查了很多资料,发现大家的方法不尽相同,有的还差别很大,容易造成误导,所以我选择从简出发,选择普通模式,虽然有一定的风险。当把index.php对应的前台代码修改完毕之后,发现前台基本正常,可是后台仍然出现重定向的问题。折腾了半天之后,我才想到看一下日志文件,原来是编辑器的问题,看来日志文件真的很重要,以前一直不重视。在config.php文件的第一行出现了输出,
1.在登录的时候,我是通过外部js文件发送Ajax请求进行验证的,在js与ThinkPHP模块函数通信遇到了点问题,一直不知道正确的路径该怎么写,也没有查到相关资料,只能各种试,好在找到了解决办法,通过直接带上入口文件名的方式,代码如下
<span> 1</span><span> var url="system.php?m=Login&a=doLog"; </span><span> 2</span> $.post(url,{"staffname":<span>$staffname</span>,"staffpwd":<span>$staffpwd</span>,"verifycode":<span>$verifycode</span>},<span>function</span><span>(data){ </span><span> 3</span> <span>if</span>(data=="codeerr"<span>){ </span><span> 4</span> alert("验证码错误!"<span>); </span><span> 5</span> }<span>else</span> <span>if</span>(data=="authempty"<span>){ </span><span> 6</span> alert("请输入用户名或密码!"<span>) </span><span> 7</span> }<span>else</span> <span>if</span>(data=="autherr"<span>){ </span><span> 8</span> alert("用户名或密码错误!"<span>); </span><span> 9</span> }<span>else</span> <span>if</span>(data=="success"<span>){ </span><span>10</span> alert("登录成功!"<span>); </span><span>11</span> <span> location.href="system.php?m=Index&a=index";</span> <span>//</span><span>访问首页</span> <span>12</span> }
当然,此为普通模式下的访问方式,如果是pathinfo的话,只需要把红色部分如下修改即可
<span> 1</span><span> var url="doLog"; </span><span> 2</span> $.post(url,{"staffname":<span>$staffname</span>,"staffpwd":<span>$staffpwd</span>,"verifycode":<span>$verifycode</span>},<span>function</span><span>(data){ </span><span> 3</span> <span>if</span>(data=="codeerr"<span>){ </span><span> 4</span> alert("验证码错误!"<span>); </span><span> 5</span> }<span>else</span> <span>if</span>(data=="authempty"<span>){ </span><span> 6</span> alert("请输入用户名或密码!"<span>) </span><span> 7</span> }<span>else</span> <span>if</span>(data=="autherr"<span>){ </span><span> 8</span> alert("用户名或密码错误!"<span>); </span><span> 9</span> }<span>else</span> <span>if</span>(data=="success"<span>){ </span><span>10</span> alert("登录成功!"<span>); </span><span>11</span> <span>location.href="../Index/index";</span> <span>//跳转</span><span>首页,访问其他模块的方法</span>
2.下载文件的时候,总是莫名多出许多html的东西,原因是缓冲区没有清空,可以通过以下代码进行修改,不过这种方式实际上是下载的仍然是html格式的文件,只不过改了一下后缀名为xls而已,因而用excel打开的时候会提示格式问题,忽略即可。同时需要注意使用 icov()函数转换编码,因为xls默认编码格式并非utf-8.
<span>ob_start</span><span>(); </span><span>ob_end_clean</span><span>(); </span><span>Header</span>( "Content-type: application/octet-stream"<span>); </span><span>Header</span>( "Accept-Ranges: bytes "<span>); </span><span>Header</span>( "Content-type:application/vnd.ms-excel;charset=gb2312"<span>); </span><span>Header</span>( "Content-Disposition:attachment;filename={<span>$filename</span>}.xls");
3.在删除文件时会遇到路径问题,因为项目中使用的较多的是相对路径,即相对入口文件而言,但是删除文件则需要使用绝对路径,我并没有找到合适的解决方法,只好用了比较保守的方式,代码如下
<span>1</span> <span>$path</span>="./Public/uploads/"<span>; </span><span>2</span> <span>$path</span>=<span>str_replace</span>("\\","/",<span>realpath</span>(<span>$path</span>)."/"); <span>//</span><span>获取绝对路径,并转换分隔符</span>
4.在配置nginx和php方面,我使用了fastCGI的方式,将如下代码保存为cmd文件,直接点击运行就可以了
"F:\php\php-cgi.exe" -b 127.0.0.1:9000 -c "F:\php\php.ini" //后面是php文件的路径
然后在nginx的配置文件里加上几句话
<span> 1</span> location ~ \.php/?.*<span> { </span><span> 2</span> <span> root myapplications; </span><span> 3</span> fastcgi_pass 127.0.0.1:9000<span>; </span><span> 4</span> fastcgi_index index.<span>php; </span><span> 5</span> fastcgi_param SCRIPT_FILENAME <span>$document_root$fastcgi_script_name</span><span>; </span><span> 6</span> <span>include</span><span> fastcgi_params; </span><span> 7</span> <span> 8</span> <span>#</span><span>定义变量 $path_info ,用于存放pathinfo信息</span> <span> 9</span> set <span>$path_info</span> ""<span>; </span><span>10</span> <span>#</span><span>定义变量 $real_script_name,用于存放真实地址</span> <span>11</span> set <span>$real_script_name</span> <span>$fastcgi_script_name</span><span>; </span><span>12</span> <span>#</span><span>如果地址与引号内的正则表达式匹配</span> <span>13</span> <span>if</span> (<span>$fastcgi_script_name</span> ~ "^(.+?\.php)(/.+)$"<span>) { </span><span>14</span> <span>#</span><span>将文件地址赋值给变量 $real_script_name</span> <span>15</span> set <span>$real_script_name</span> $1<span>; </span><span>16</span> <span>#</span><span>将文件地址后的参数赋值给变量 $path_info</span> <span>17</span> set <span>$path_info</span> $2<span>; </span><span>18</span> <span> } </span><span>19</span> <span>#</span><span>配置fastcgi的一些参数</span> <span>20</span> fastcgi_param SCRIPT_FILENAME <span>$document_root$real_script_name</span><span>; </span><span>21</span> fastcgi_param SCRIPT_NAME <span>$real_script_name</span><span>; </span><span>22</span> fastcgi_param PATH_INFO <span>$path_info</span><span>; </span><span>23</span> }