Deploy Thinkphp website in Nginx and Tengine environments

Source: Internet
Author: User
Tags php file php and php framework php script phpinfo regular expression

Before deploying Thinkphp3.2.3 in Tengine, wenwushuang completed the perfect deployment according to the methods in thinkphp official thinkphp3.2.3 document. However, a thinkphp3.1 project was maintained later and cannot be deployed in nginx according to the official deployment documents. Thinkphp's official website and du Niang have also checked a lot of information, so they won't be able to take a lot of detours. After a week, I finally solved the problem perfectly. The Thinkphp3.1 project is successfully deployed in the Tengine environment. This method is also suitable for later Nginx environments.

As shown in the figure, wenwushuang found a perfect solution to this problem on Baidu. Since the author has already written a solution to the problem, I will not go over it again. I just want to summarize the tengine configuration file. In addition, we should summarize the problem of PATH_INFO, that is, the principle of solving the problem. This solution involves several knowledge points that need to be remembered:
First Knowledge Point: uri and url
Uri is the abbreviation of Universal Resource Identifier. uri indicates the uniform Resource Identifier, which is equivalent to the ID of a webpage Resource and can uniquely identify a webpage Resource.
The url is the abbreviation of the Universal Resource Locator. The url is the meaning of the unified Resource Locator, which can provide the path to find the webpage Resource.
To put it simply, the URL itself locates a resource, so the URL itself is a URI. However, the URI is not necessarily a URL, so the URI contains a URL.
Second knowledge point: tengine and nginx try_files commands
The core function of the try_files command is to replace the rewrite function of nginx,
Syntax: try_files file... Uri or try_files file... = Code
Default value: None
Scope: server location
Command: try_files $ uri/index. php $ uri. This command checks the existence of $ uri in sequence. If yes, the first file found is returned. The slash $ uri/at the end of the slash is a folder. If none of the files can be found, an internal redirection will be made to the last parameter/index. php $ uri.
Description: all static resources in thinkphp can be found based on this syntax. The php file will be redirected to/index. php $ uri by the try_files command because of the routing. Because the URI is added with the/index. php path, it will be matched by the following regular expressions in. + \. php ($ |.
This command perfectly solves the thinkphp website. Static resources are accessed using normal URLs, and php script files are reversed to the thinkphp framework for parsing through tengine.
Third knowledge point: location regular expression. + \. php ($ |/)
This regular expression indicates matching any uri containing. php or scripts ending with. php.
". +" Indicates that any character is repeated more than once .. Php ($ |/) indicates. php or. php/, that is, the. php/script file is included in the end or URI of. php.
Fourth knowledge point: PATH_INFO and phpinfo () functions
PATH_INFO is mentioned in the configuration file shown in the preceding figure. The name indicates the path of the script. PATH_INFO is a CGI 1.1 standard. In PHP, use the global variable $ _ SERVER ['path _ info'] to obtain the file PATH information. Many PHP frameworks will use it to beautify the website URL. Example:
Http://www.test.com/index.php/foo/bar.html? C = index & m = search
We can get $ _ SERVER ['path _ info'] = '/foo/bar.html ', in this case, $ _ SERVER ['query _ string'] = 'C = index & m = search ';
Phpinfo () is a function of php and is not a function of nginx. It returns the path information of the script file in the form of an array.
Nginx does not set the PATH_INFO environment variable value by default. You need to use cgi in the php configuration file. fix_pathinfo = 1 to obtain the path information, but this will also cause security risks. fix_pathinfo = 0 is set to 0, so that php cannot obtain the PATH_INFO information. The php framework that relies on PATH_INFO for URL beautification becomes invalid.
In the new version of nginx, you can use the fastcgi_split_path_info command to set PATH_INFO. The old method, for example, using if to judge is no longer recommended.
Fifth knowledge point: fastcgi_split_path_info command of nginx
This command separates PATH_INFO from the URI;
Sixth knowledge point: fastcgi_param PATH_INFO $ fastcgi_path_info
This line of code sets a PATH_INFO parameter with the variable name $ fastcgi_path_info;
The last line of the configuration file, include fastcgi. conf. The default fastcig. conf file is loaded, and The PATH_INFO split from URI is set to the parameter $ fastcgi_path_info of PATH_INFO, which is passed to the php-fpm in the background for processing. In fact, it is handed over to the php framework and parsed according to the framework's custom route.

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.