How to correctly configure Nginx + PHP (optimal allocation)

For many people, nothing less than to configure Nginx + PHP search tutorial, then copy and paste. Sounds seems to be no problem, but in fact a lot of information on the network itself disrepair, full of loopholes, if you superficial understanding, blindly copy and paste, sooner or later will pay the price.

 

Suppose we implemented a PHP front-end controller, or bluntly put, it is a unified entrance: the PHP requests are sent to the same file, then this file is routed through analytical achieve "REQUEST_URI."

At this point a lot of this tutorial will teach you to configure Nginx + PHP:

server {
    listen 80;
    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 bad taste of the place, we look at a few can be found.

We need to first understand what inheritance Nginx configuration file directive: Nginx configuration file is divided into many blocks, common from outside to inside in turn is "http", "server", "location" and so on, the default inheritance that is to say the inner block will automatically obtain the value of the outer blocks from outside to inside as a default value (exceptions, see reference).

Let's start with the " index " command to start it, the problem is in the configuration in which the definition of "location" in the:

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

Once the future need to add new "location" inevitable "index" command to repeat the definition, this is because more "location" is the same level of relations, inheritance does not exist at this time should be defined "index in the" server "in "With inheritance," index "command can take effect in all of the" location "in.

Then take a look at " if " command, it is our most misunderstood instructions Nginx is not too much:

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

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

try_files $ on $ a / /index.php;

In addition, beginners tend to think that " if " command is a kernel-level instruction, but in fact it is part of the rewrite module, coupled with the fact Nginx configuration is declarative rather than procedural, so when it and non-rewrite module instruction mix, the result may be non you wish.

Let's look at "fastcgi_params" configuration file:

include fastcgi_params;

Nginx fastcgi have two profiles, respectively, "fastcgi_params" and "fastcgi.conf", which is not much difference, the only difference is that the latter more than the former definition line "SCRIPT_FILENAME" in:

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

NOTE: There is between $ document_root and $ fastcgi_script_name /.

Nginx had only "fastcgi_params", and later found that many people use the hard-coded in the definition of "SCRIPT_FILENAME" So in order to standardize the usage will be introduced "fastcgi.conf."

But this is the case a question arises: Why is it necessary to introduce a new profile instead of modifying the old configuration file? This is because "fastcgi_param" command is an array type, and general instruction is the same: to replace the inner layer; and a general instruction is different: when used multiple times in the same level, is new and not replaced. In other words, if at the same level twice the definition of "SCRIPT_FILENAME", then they will be sent to the back-end, which may cause some potential problems, in order to avoid such situations, it introduces a new configuration file.

In addition, we also need to consider a security problem: On the case of "cgi.fix_pathinfo" of, PHP might put the wrong type of file to parse as PHP files in PHP. If Nginx and PHP installed on the same server, then the easiest solution is to use " try_files " command to do a filter:

try_files $uri =404;

In accordance with the above analysis, given a modified version of, is not it refreshing than a lot of versions starting at:

server {
    listen 80;
    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;
    }
}

In fact there are some flaws, mainly " try_files " and " fastcgi_split_path_info " is not compatible, even though it can solve, but the program relatively ugly, concrete is not to say

Added: because "location" has done limited, so "fastcgi_index" In fact, there is no need.

Guess you like

Origin www.cnblogs.com/longqin/p/11613934.html