How to properly configure nginx+php

Source: Internet
Author: User
Tags inheritance php file

For many people, configuring nginx+php is nothing more than searching for a tutorial and then copying and pasting. It sounds like there is no problem, but in fact, many of the data on the network itself is dilapidated, porous, and if everyone superficial understanding, blindly paste, sooner or later will pay the price.

Let's say we've implemented a front-end controller in PHP, or, to put it bluntly, a unified Portal: sending PHP requests to the same file, and then implementing routing through parsing "request_uri" in this file.

At this point many tutorials will teach you how 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 the bad taste of the place, we can see how many.

...

It is necessary for us to understand the inheritance of the instructions in the Nginx configuration file: 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 relationship is from outside to inside, which means that the inner block automatically gets the value of the outer block as the default (with the exception of the reference).

Reference: Understanding the NGINX CONFIGURATION inheritance MODEL

...

Let's start with the "index" directive, which is defined in "location" in the problem configuration:

Location/{

Index index.html index.htm index.php;

}

Once the future needs to join the new "location", there will inevitably be duplicate defined "index" instructions, because multiple "location" is a peer relationship, there is no inheritance, at this time should be defined in "server" "index", with the help of inheritance relationships, " The index"directive takes effect in all"location".

Reference: Nginx Pitfalls

...

Next look at the "if" directive, saying that it is the deepest misunderstanding of the Nginx directive is not too much:

if (!-e $request _filename) {

Rewrite. /index.php last;

}

Many people like to use the "if" command 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 think that the "if" instruction 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 mixes with non-rewrite module instructions, the result may not be what you want.

Reference: Ifisevil and how nginx ' location if ' works

...

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

Include Fastcgi_params;

Nginx has two fastcgi profiles, "fastcgi_params" and "fastcgi.conf", which are not much different, the only difference being that the latter has a one-line "script_filename" definition than the former:

Fastcgi_param script_filename $document _root$fastcgi_script_name;

Note: There is no/_script_name between the $document _root and the $fastcgi.

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

However, this raises the question of why it is important to introduce a new configuration file instead of modifying the old one. This is because the "fastcgi_param" instruction is an array type, the same as a normal instruction: The inner layer replaces the outer layers, and the ordinary instructions are different: When used on multiple levels, is new and not replaced. In other words, if you define two times "script_filename" at the sibling, they are sent to the backend, which may lead to some potential problems, and in order to avoid such situations, a new configuration file is introduced.

Reference: Fastcgi_params versus FASTCGI. Conf–nginx CONFIG HISTORY

...

In addition, we also need to consider a security issue: In the case of PHP open "cgi.fix_pathinfo", PHP may be the wrong file type as a PHP file to resolve. If Nginx and PHP are installed on the same server, the simplest solution is to use the "try_files" directive to do a filter:

Try_files $uri = 404;

Reference: Nginx file type Error Resolution vulnerability

...

According to the previous analysis, given an improved version, is not more refreshing than the initial version of a lot:

server {

Listen 80;

server_name foo.com;

Root/path;

Index index.html index.htm index.php;

Location/{

Try_files $uri $uri//index.php;

}

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, although it can be resolved, but the scheme is more ugly, specifically do not say more, interested can refer to the problem description.

Add: Because "location" has been limited, so "fastcgi_index" is not really necessary.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.