For details about how to configure ThinkPHP5 in Nginx, nginxthinkphp5

Source: Internet
Author: User

For details about how to configure ThinkPHP5 in Nginx, nginxthinkphp5

This article describes how to configure ThinkPHP5 in Nginx for your reference. I will not talk about it here. Let's take a look at the details:

Hide the public directory in the url

For security reasons, the entrance file of TP5 is changed to public, this prevents malicious users from accessing files in the "/thinkphp/" and "/vendor/" directories. Therefore, when you configure the website documentroot as the project root directory, you need to add/public/behind the url for access. Of course, some children's shoes may also put the entrance file back to the root directory, and then access the file in the form of version 3.x.

But obviously, this is not so scientific.

Assume that the project directory is "/web/wwwroot/augsky.com", we will configure root in the nginx configuration file of the website as follows:

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

However, remember to set open_basedir to the root directory of the upper-level project. Otherwise, the application will not have the permission to call other files except the public directory, and the website will report 500 inaccessible. (Open_basedir is configured in php by default. ini, but if it is in multiple virtual machine environments, it may be in the user of each website. in the INI file, this depends on your actual situation .) The specific configuration is as follows:

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

Hide the entry file index. php:

Location/{try_files $ uri // index. php? S = $ uri & $ args; # if the request is not a file or directory, the uri is handed over to index. php for processing and the parameter is retained}

Try_files:

Try_files Syntax: try_files file1 [file2... filen] uri OR try_files file1 [file2... filen] = code default value: no scope: server location

Try_files supports multiple parameters. Each parameter represents a file. The system checks the existence of these files in sequence and runs them directly if they exist. The slash "/" at the end of the slash indicates a directory. If none of them exist, the object to which the last parameter points or the specified http status code is returned.

Pathinfo Configuration

Add this section in the configuration file (if you are an lnmp one-key package user, skip this section and go down ):

Location ~ [^/] \. Php (/| $) {set $ path_info ""; # defines the variable $ real_script_name, which is used to store the real address set $ real_script_name $ fastcgi_script_name; # if the address matches the Regular Expression in 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_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 describes the fastcgi_pass settings, which must be set based on your actual php version and installation directory.

Okay. After that, save and restart nginx and php. If you are an lnmp one-key package user, you don't need to set it like this. Let's look at it below:

Setting pathinfo in one-click lnmp package

In fact, lnmp one-click package pathinfo brother has been written, we only need to include enable-php.conf; modified to include enable-php-pathinfo.conf; then restart lnmp to get it done.

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

This is simple. The following describes how to hide a module in a url.

Url hiding default module index

It's easy to define the constant BIND_MODULE in the entry file for the module you are using. Take the default index as an example:

define('BIND_MODULE', 'index');

In this way, there will be no module name level directory in the url. You must know that URLs with fewer levels are good for SEO.

Below are the location matching rules in nginx

Matching rules for Nginx location

  • ~ Tilde indicates performing a regular match, case sensitive
  • ~ * Indicates executing a regular expression matching, Which is case insensitive.
  • ^ ~ ^ ~ It indicates normal character match. If this option matches, it only matches this option and does not match other options. It is generally used to match directories.
  • = Exact matching of common characters
  • @ # "@" Defines a named location, which is used for internal targeting, such as error_page and try_files.

Location matching priority

  • = Exact match is the first to be processed. If exact match is found, nginx stops searching for other matches.
  • Regular character matching: Regular Expression rules and long block rules are preferentially matched with the query. That is to say, if this match is performed, you need to check whether there is a regular expression matching or a longer match.
  • ^ ~ Only match this rule. nginx stops searching for other matches. Otherwise, nginx will continue to process other location commands.
  • The final match includes "~ "And "~ * "Command. If a match is found, nginx stops searching for other matches. If no regular expression or no regular expression is matched, the command with the highest matching degree will be used.

Summary

The above is all the content of this article. I hope the content of this article will help you in your study or work. If you have any questions, please leave a message to us. Thank you for your support.

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.