The configuration method of ThinkPHP5 under Nginx

Source: Internet
Author: User
This article introduces the content of Nginx under the ThinkPHP5 configuration method, has a certain reference value, now share to everyone, the need for friends can refer to

Today, a friend asked Tp5 support PathInfo the Nginx configuration How to write the problem, so think about their own summary sharing, the following this article is mainly about the ThinkPHP5 in the Nginx configuration method, the need for friends can reference, the following words do not say much, Come and see it together.

This article mainly introduces to you about the Nginx under the ThinkPHP5 configuration method, shares out for everyone reference study, the following words do not say, come together to see the detailed introduction:

Hiding the public directory in the URL

For security reasons, TP5 's entry file is changed to public, because it prevents malicious users from accessing files in these directories such as "/thinkphp/", "/vendor/", and so on. So when you configure the site DocumentRoot as the root of the project in the previous habit, you will need to add/public/after the URL to access it. Of course, there may also be children's shoes to put the entry file back to the root directory, and then in the form of a previous version of 3.x access.

But obviously, it's not that much science.

Assuming the project directory is "/web/wwwroot/augsky.com", then we configure the root in the Nginx configuration file of the website to:

Root/web/wwwroot/augsky.com/public;

But be sure to remember to set the Open_basedir to the root of the previous level of the project, or the application will not have permission to invoke other files in the public directory, the site will report 500 unreachable. (Open_basedir configuration By default in PHP.ini, but if it is more than one virtual machine environment may be in the User.ini files of each website, this should be based on their actual situation. The specific configuration is as follows:

open_basedir=/web/wwwroot/augsky.com:/tmp/:/proc/

Hide Portal File index.php:

Location/  {    try_files $uri $uri//index.php?s= $uri & $args;    #如果请求不是文件或目录, the URI is passed to index.php processing while the parameter is reserved  }

Say Try_files:

Try_files syntax: try_files file1 [file2 ... filen] URI     OR  try_files file1 [file2 ... filen] =code default: No scope: Server loc ation

Try_files support multiple parameters, each parameter represents a file, the system will check the existence of these files sequentially, the existence of direct execution, slash "/" at the end of the directory, if none exist, will be redirected to the last parameter point to the file or return the specified HTTP status code.

PathInfo Configuration

Add this paragraph to the config file (if you are a LNMP user, please skip this section):

Location ~ [^/]\.php (/|$) {  set $path _info "";  #定义变量 $real _script_name for storing the real address  set $real _script_name $fastcgi _script_name;  #如果地址与引号内的正则表达式匹配  if ($fastcgi _script_name ~ "^ (. +?\.php) (/.+) $") {  #将文件地址赋值给变量 $real _script_name  set $ Real_script_name $;  #将文件地址后的参数赋值给变量 $path _info  set $path _info;  }  #配置fastcgi的一些参数  fastcgi_pass Unix:/tmp/php-cgi.sock;  Fastcgi_index index.php;  Fastcgi_param script_filename $document _root$real_script_name;  Fastcgi_param script_name $real _script_name;  Fastcgi_param path_info $path _info;  Include  Fastcgi_params;}

The above need to explain is the Fastcgi_pass settings, this will be based on your actual PHP version and installation directory to set.

OK, after this, save, restart Nginx and PHP will be effective. If you are LNMP a button package users without the above settings, look down:

LNMP settings for one-click Package pathinfo

In fact, lnmp a key pack inside the PathInfo Army brother has been written, we only need to include the enable-php.conf, modified to include enable-php-pathinfo.conf, and then restart the LNMP is done.

#include enable-php.conf; or, you directly comment out this line, add a new line below include enable-php-pathinfo.conf;

It's that simple. Here is a way to hide the module in the URL

URL Hide Default Module index

It's easy to define a constant in the portal file Bind_module for the module you are using, with the default index as an example:

Define (' Bind_module ', ' Index ');

In this way, the URL will not be in the module name of the level directory. You know, the low-level URLs are good for SEO.

Here are the rules for the location of Nginx

Matching rules for Nginx location

    • ~ Wavy line to perform a regular match, case-sensitive

    • ~* indicates that a regular match is performed, is case-insensitive

    • ^~ ^~ represents normal character matching, and if this option matches, matches only that option, and does not match the other options, typically used to match the directory

    • = Exact Match of ordinary characters

    • @ # "@" defines a named location, using internal orientation, such as Error_page, Try_files

Location-matching Priority

    • = Exact match will be processed first. If an exact match is found, Nginx stops searching for other matches.

    • Normal character matching, regular expression rules and long block rules will be matched by precedence and query, that is, if the match needs to be seen there are no regular expression matches and longer matches.

    • ^~ only matches this rule, Nginx stops searching for other matches, otherwise Nginx will continue to process other location instructions.

    • Finally, the matching manager with "~" and "~*" instructions, if the corresponding match, the Nginx stop searching for other matches, when there is no regular expression or no regular expression is matched, then the most matched verbatim matching instructions will be used.

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.