Correct idea and process for configuring Nginx + PHP, and idea for configuring nginxphp _ PHP Tutorial

Source: Internet
Author: User
Correct idea and process for configuring Nginx + PHP and nginxphp. The correct idea and process of configuring Nginx + PHP, and the idea of configuring nginxphp is nothing more than searching for a tutorial and then copying and pasting Nginx + PHP. It seems that there is no correct idea and process for configuring Nginx + PHP, and the idea for configuring nginxphp

For many people, to Configure Nginx + PHP, simply search for a tutorial and copy and paste it. It seems that there is no problem. Unfortunately, in fact, many materials on the network have been out of repair for a long time. if you just copy and paste them, you will pay a price one day later.

How to correctly Configure Nginx + PHP

Suppose we use PHP to implement a front-end controller, or simply put, it is a unified portal: Send all PHP requests to the same file, and then implement routing by parsing "REQUEST_URI" in this file.

Generally, this configuration

At this time, many tutorials will teach you how to configure Nginx + PHP in this way:

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 many errors, or at least bad ones. you can see a few.

It is necessary to first understand the inheritance relationship of commands in the Nginx configuration file:

The Nginx configuration files are divided into multiple parts. common from the outside to the inside are "http", "server", and "location". the default inheritance relationship is from the outside to the inside, that is to say, the inner block automatically obtains the value of the outer block as the default value.

Let's start with the index command.

In problem configuration, it is defined in location:

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

If you need to add a new "location" in the future, the "index" command will be repeatedly defined. this is because multiple "locations" are in a hierarchical relationship and there is no inheritance, in this case, define "index" in "server". with the inheritance relationship, the "index" command takes effect in all "locations.

Next let's take a look at the "if" command

It is the most misunderstood Nginx command:

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

Many people prefer to use the "if" command for a series of checks, but this is actually the responsibility of the "try_files" command:

Try_files $ uri // index. php;
In addition, beginners often think that the "if" command is a kernel-level command, but it is actually part of the rewrite module, and Nginx configuration is actually declarative, instead of procedural, when it is mixed with the commands of the non-rewrite module, the results may not be as expected.

Next let's take a look at the configuration file fastcgi_params.

Include fastcgi_params;
Nginx has two fastcgi configuration files: "fastcgi_params" and "fastcgi. conf. The only difference is that the latter has a line of "SCRIPT_FILENAME" more than the former:

Fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
Note: There is no/between $ document_root and $ fastcgi_script_name /.

Originally, Nginx only had "fastcgi_params". later, it was found that many people used hard encoding when defining "SCRIPT_FILENAME". Therefore, "fastcgi. conf" was introduced for standard usage 」.

In this case, the question is: why should we introduce a new configuration file instead of modifying the old configuration file? This is because the "fastcgi_param" command is of the array type, which is the same as the common command: the inside layer replaces the outer layer; and the common command is different: when it is used multiple times at the same level, is New rather than replacement. In other words, if two "SCRIPT_FILENAME" statements are defined at the same level, they are all sent to the backend, which may cause some potential problems. to avoid such problems, A new configuration file is introduced.

In addition, we also need to consider a security issue: when "cgi. fix_pathinfo" is enabled in PHP, PHP may parse the wrong file type as a PHP file. If Nginx and PHP are installed on the same server, the simplest solution is to use the "try_files" command for filtering:

Try_files $ uri = 404;

Improved version

According to the previous analysis, a modified version is provided, which is much easier than the previous version:

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;  }}

How to correctly Configure Nginx + PHP should be well understood by everyone!

For many users, configuring Nginx + PHP is simply to search for a tutorial and copy and paste it. It sounds like nothing...

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.