Configure ThinkPHP project on Nginx, nginxthinkphp project_php tutorial

Source: Internet
Author: User
Configure the ThinkPHP project and nginxthinkphp project on Nginx. Configure the ThinkPHP project on Nginx. the nginxthinkphp project developed a small website with Apache and TP some time ago. this is my first practice in using TP. I am used to configuring ThinkPHP projects and nginxthinkphp projects on Nginx in pathinfo mode.

Some time ago, I developed a small website with Apache and TP, which is my first practice in using TP. I am used to the pathinfo mode, and the local operation is normal. However, when deployed on the server, because the other side uses Nginx, the pathinfo mode of ThinkPHP is not supported by default, resulting in path errors, when the page jumps cyclically, nginx is downloaded and tested locally.

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,

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 found a solution by directly attaching the entry file name. The code is as follows:

1 var url = "system. php? M = Login & a = doLog "; 2 $. post (url, {"staffname": $ staffname, "staffpwd": $ staffpwd, "verifycode": $ verifycode}, function (data) {3 if (data = "codeerr") {4 alert ("verification code error! "); 5} else if (data =" authempty ") {6 alert (" enter your username or password! ") 7} else if (data =" autherr ") {8 alert (" incorrect user name or password! "); 9} else if (data =" success ") {10 alert (" logon successful! "); 11 location. href =" system. php? M = Index & a = index "; // visit homepage 12}

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

1 var url = "doLog"; 2 $. post (url, {"staffname": $ staffname, "staffpwd": $ staffpwd, "verifycode": $ verifycode}, function (data) {3 if (data = "codeerr") {4 alert ("verification code error! "); 5} else if (data =" authempty ") {6 alert (" enter your username or password! ") 7} else if (data =" autherr ") {8 alert (" incorrect user name or password! "); 9} else if (data =" success ") {10 alert (" logon successful! "); 11 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. the code is as follows:

1 $ path = ". /Public/uploads/"; 2 $ path = str_replace (" \ ","/", realpath ($ path ). "/"); // 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.

1 location ~ \. Php /?. * {2 root myapplications; 3 fastcgi_pass 127.0.0.1: 9000; 4 fastcgi_index index. php; 5 fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name; 6 include fastcgi_params; 7 8 # Definition variable $ path_info, used to store pathinfo information 9 set $ path_info "; 10 # Definition variable $ real_script_name, used to store the real address 11 set $ real_script_name $ fastcgi_script_name; 12 # if the address matches the regular expression in the quotation marks 13 if ($ fastcgi_script_name ~ "^ (. +? \. Php )(/. +) $ ") {14 # assign the file address to the variable $ real_script_name15 set $ real_script_name $1; 16 # assign the parameter value after the file address to the variable $ path_info17 set $ path_info $2; 18} 19 # configure some parameters of fastcgi 20 fastcgi_param SCRIPT_FILENAME $ document_root $ real_script_name; 21 fastcgi_param SCRIPT_NAME $ real_script_name; 22 fastcgi_param PATH_INFO $ path_info; 23}

I developed a small website with Apache and TP some time ago, which is also my first practice in using TP. I used to use the pathinfo mode...

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.