Solve the problem that nginx does not support pathinfo in thinkphp, nginxthinkphp

Source: Internet
Author: User

Solve the problem that nginx does not support pathinfo in thinkphp, nginxthinkphp

The following small series will be explained in detail through text and code. The specific content is as follows:

In fact, to solve the problem that nginx does not support pathinfo, there are two solutions: one is not to use the pathinfo mode, and the other is to modify the nginx configuration file so that it supports pathinfo. In order to simplify the problem, I chose the first method, because I checked a lot of information and found that the methods were different, and some were quite different, it is easy to mislead, so I chose to start from simple mode, although there is a certain risk. After the front-end code corresponding to index. php is modified, it is found that the front-end is basically normal, but there is still a redirection problem in the background. After a long time, I thought about viewing the log file. It turned out to be an editor problem. It seems that the log file is really important and I have never paid much attention to it before. Output is displayed in the first line of the config. php file,

Under sublime, the BOM header is usually added to the UTF-8 file, this BOM header is usually invisible under the window, you can view it through other editors, Linux can also be seen directly, it is usually displayed as a garbled character. just delete it, or simply press enter in the first line and then delete it. At this point, the background is basically accessible.

1. when I log on, I sent an Ajax request through an external js file for verification. I encountered some problems in the function communication between js and the ThinkPHP module, and I never knew how to write the correct path, I have not found any relevant information and can only try it. Fortunately, I have found a solution by directly attaching the file name of the portal.

Var url = "system. php? M = Login & a = doLog "; $. post (url, {"staffname": $ staffname, "staffpwd": $ staffpwd, "verifycode": $ verifycode}, function (data) {if (data = "codeerr") {alert ("Verification code error! ");} Else if (data =" authempty ") {alert (" Enter your username or password! ")} Else if (data =" autherr ") {alert (" incorrect user name or password! ");} Else if (data =" success ") {alert (" Logon successful! "); Location. href =" system. php? M = Index & a = index "; // visit the home page}

Of course, this is the access method in normal mode. If it is pathinfo, you only need to modify the red part as follows.

Var url = "doLog"; $. post (url, {"staffname": $ staffname, "staffpwd": $ staffpwd, "verifycode": $ verifycode}, function (data) {if (data = "codeerr") {alert ("Verification code error! ");} Else if (data =" authempty ") {alert (" Enter your username or password! ")} Else if (data =" autherr ") {alert (" incorrect user name or password! ");} Else if (data =" success ") {alert (" Logon successful! "); Location. href =" ../Index/index "; // redirect to the home page and access other modules

2. when downloading files, there is always a lot of html content, because the buffer zone is not cleared and can be modified using the following code. However, this method is actually to download html files, the suffix is changed to xls. Therefore, when you open the file in excel, a format error is prompted. Ignore the format. At the same time, you must use the icov () function to convert the encoding because the default xls encoding format is not 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. when you delete a file, you may encounter a path problem because the project uses a lot of relative paths, that is, relative to the entry file, but the absolute path is required to delete the file, I didn't find a proper solution, so I had to use a conservative method.
Copy codeThe Code is as follows:
$ Path = "./Public/uploads /";
$ Path = str_replace ("<a target =" _ blank "href =" file: // "style =" color: rgb (51,119,170); text-decoration: none "> \", "/", realpath ($ path ). "/</a>"); // obtain the absolute path and convert the Separator

4. For nginx and php configuration, I used fastCGI to save the following code as a cmd file and click Run.
"F: \ php \ php-cgi.exe"-B 127.0.0.1: 9000-c "F: \ php. ini" // the path of the php file is
Then add a few words to the nginx configuration file.

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; # defines the variable $ path_info, which is used to store pathinfo information set $ path_info "; # defines the variable $ real_script_name, used to store the real address set $ real_script_name $ fastcgi_script_name; # if the address matches the regular expression in the quotation marks, if ($ fastcgi_script_name ~ "^ (. +? \. Php )(/. +) $ ") {# assign the file address to the variable $ real_script_name set $ real_script_name $1; # assign the parameter after the file address to the variable $ path_info set $ path_info $2 ;} # configure fastcgi parameters fastcgi_param SCRIPT_FILENAME $ document_root $ real_script_name; fastcgi_param SCRIPT_NAME $ real_script_name; fastcgi_param PATH_INFO $ path_info ;}

The above content is for deploying ThinkPHP on Nginx to solve the Pathinfo problem and hope to help you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.