search
HomeBackend DevelopmentPHP Tutorial在Nginx上部署ThinkPHP,解决Pathinfo有关问题

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

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


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


在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";   //访问首页     }

当然,此为普通模式下的访问方式,如果是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";   //跳转首页,访问其他模块的方法

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");

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

$path="./Public/uploads/";     $path=str_replace("\\","/",realpath($path)."/");  //获取绝对路径,并转换分隔符

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

"F:\php\php-cgi.exe" -b 127.0.0.1:9000 -c "F:\php\php.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;         }



延伸阅读

《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实现动态包含文件的方法

24thinkphp实现发送邮件密码找回功能实例

25ThinkPHP实现支付宝接口功能实例

26Thinkphp搜索时首页分页和搜索页保持条件分页的方法

27ThinkPHP入口文件设置及相关注意事项分析

28ThinkPHP模版引擎之变量输出详解

29thinkphp实现上一篇与下一篇的方法

30ThinkPHP中url隐藏入口文件后接收alipay传值的方法

31ThinkPHP打开验证码页面显示乱码的解决方法

32ThinkPHP中使用ajax接收json数据的方法

33ThinkPHP内置jsonRPC的缺陷分析

34thinkphp3.0输出重复两次的解决方法

35thinkPHP实现表单自动验证

36Thinkphp中的curd应用实用要点

37浅谈thinkphp的实例化模型

38ThinkPHP 404页面的设置方法

39THINKPHP内容分页代码分享

40在Nginx上部署ThinkPHP项目教程

41浅析THINKPHP的addAll支持的最大数据量

42ThinkPHP提示错误Fatal error: Allowed memory size的解决方法

43ThinkPHP3.2.2的插件控制器功能

44ThinkPHP3.2.3数据库设置新特性

45ThinkPHP 3.2 版本升级了哪些内容

46thinkPHP学习笔记之安装配置篇

47Thinkphp调用Image类生成缩略图的方法

48解决ThinkPHP关闭调试模式时报错的问题汇总

49ThinkPHP文件缓存类代码分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

Statement
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
PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

How does PHP handle object cloning (clone keyword) and the __clone magic method?How does PHP handle object cloning (clone keyword) and the __clone magic method?Apr 17, 2025 am 12:24 AM

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP vs. Python: Use Cases and ApplicationsPHP vs. Python: Use Cases and ApplicationsApr 17, 2025 am 12:23 AM

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool