Configuring the correct thinking and process of nginx+php, configuring nginxphp ideas _php Tutorials

Source: Internet
Author: User

Configure the correct thinking and process of nginx+php, configure nginxphp thinking


For many people, configuring nginx+php is no different than searching for a tutorial and then copying and pasting. It seems that there is no problem, unfortunately in fact, a lot of information on the network itself in disrepair, flawed, if everyone superficial understanding, blindly copy paste, sooner or later, will pay the price.

How to properly configure nginx+php

Let's say we implement a front-end controller with PHP, or the straightforward point is the unified Portal: Send PHP requests to the same file, and then parse "request_uri" in this file to implement the route.

Generally this configuration

At this time many tutorials will teach you to configure nginx+php:

server {  listen;  server_name foo.com;  Root/path;  Location/{    index index.html index.htm index.php;    if (!-e $request _filename) {      rewrite./index.php last;    }  }  Location ~/.php$ {    include fastcgi_params;    Fastcgi_param Script_filename/path$fastcgi_script_name;    Fastcgi_pass 127.0.0.1:9000;    Fastcgi_index index.php;  }}

There are a lot of mistakes, or at least a bad taste, you can see a few.

It is necessary for us to understand the inheritance relationship of the instructions in the Nginx configuration file:

Nginx configuration file is divided into a lot of pieces, the common from the outside to the inside is "http", "server", "location" and so on, the default inheritance is from the outside to the inside, that is, the inner block will automatically get the value of the outer block as the default value.

Let's start with the "index" directive.

In the problem configuration it is defined in "location":

Location/{  index index.html index.htm index.php;}

Once the future needs to join the new "location", there is bound to be a duplicate definition of the "index" directive, this is because multiple "location" is a peer relationship, there is no inheritance, it should be defined in "server" "index", with the help of inheritance relations, " The index"directive can take effect in all"location".

Now look at the "if" directive.

Said it was the deepest misunderstanding of the Nginx command is no more than:

if (!-e $request _filename) {  rewrite./index.php last;}

Many people like to use "if" instructions to do a series of checks, but this is actually the responsibility of the "try_files" Directive:

Try_files $uri $uri//index.php;
In addition, beginners tend to assume that the "if" directive is a kernel-level instruction, but in fact it is part of the rewrite module, and the Nginx configuration is actually declarative rather than procedural, so when it is mixed with non-rewrite module instructions, the result may not be what you want.

Here's a look at the "fastcgi_params" configuration file

Include Fastcgi_params;
Nginx has two copies of the fastcgi configuration files, respectively, "fastcgi_params" and "fastcgi.conf", they are not much different, the only difference is that the latter is more than the former definition of a row of "script_filename":

Fastcgi_param script_filename $document _root$fastcgi_script_name;
Note: There is no/between the $document _root and $fastcgi _script_name.

Originally Nginx only "fastcgi_params", later found that many people in the definition of "script_filename" use hard-coded way, so in order to standardize the use of the introduction of "fastcgi.conf".

However, this raises the question of why it is necessary to introduce a new configuration file instead of modifying the old one. This is because the "fastcgi_param" instruction is an array type, and the same as the normal instruction: The inner layer replaces the outer layers, and the ordinary instruction is different: when used in the same class multiple times, is added instead of replaced. In other words, if you define two "script_filename" at the same level, then they will be sent to the backend, which may cause some potential problems, and in order to avoid such situations, a new configuration file is introduced.

In addition, we need to consider a security issue: PHP may use the wrong file type as a php file when PHP "cgi.fix_pathinfo" is turned on. If Nginx and PHP are installed on the same server, then the simplest solution is to use the "try_files" command to do a filter:

Try_files $uri = 404;

Modified version

According to the previous analysis, given a modified version, is not a lot more refreshing than the beginning version:

server {  listen;  server_name foo.com;  Root/path;  Index index.html index.htm index.php;  Location/{    try_files $uri $uri//index.php$is_args$args;  }  Location ~/.php$ {    try_files $uri =404;    Include fastcgi.conf;    Fastcgi_pass 127.0.0.1:9000;  }}

How to properly configure Nginx + PHP, I believe we should have their own understanding of it!

http://www.bkjia.com/PHPjc/1125885.html www.bkjia.com true http://www.bkjia.com/PHPjc/1125885.html techarticle Configure the correct thinking and process of nginx+php, configure nginxphp ideas for many people, configuration nginx+php is a search for a tutorial, and then copy and paste. It doesn't sound like much .

  • Related Article

    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.