A simple PHP site configuration

Source: Internet
Author: User

A simple PHP site configuration
Now let's look at a typical, simple PHP site, how Nginx chooses location for a request to handle:

server {
Listen 80;
server_name example.org www.example.org;
root/data/www;

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

Location ~* \. (gif|jpg|png) $ {
Expires 30d;
}

Location ~ \.php$ {
Fastcgi_pass localhost:9000;
Fastcgi_param Script_filename
$document _root$fastcgi_script_name;
Include Fastcgi_params;
}
}
First, Nginx uses prefix matching to find the most accurate location, and this step Nginx ignores location in the order in which the configuration files appear. In the above configuration, the only prefix matching location is "/", and since it can match any request, it is used as the last choice. Then, Nginx continues to match the location of the regular expression in the order in which it was configured, and then stops the search after matching to the first regular expression. The location to which it is matched will be used. If no location is matched to the regular expression, the location that matches the most accurate prefix just found is used.

Note that all location matching tests use only the URI portion of the request, not the Parameters section. This is because there are many ways to write parameters, such as:

/index.php?user=john&page=1
/index.php?page=1&user=john
In addition, anyone can add a string in the request string as freely as possible:

/index.php?page=1&something+else&user=john
Now let's look at how the request is handled using the above configuration:

The request "/logo.gif" first matches the location "/" and then matches the regular expression "\. (gif|jpg|png) $ ". Therefore, it will be processed by the latter. According to the "root/data/www" directive, Nginx maps the request to the file/data/www/logo.gif "and sends the file to the client.
The request "/index.php" first also matches the location "/" and then matches the regular expression "\". (PHP) $ ". Therefore, it will be processed by the latter and then sent to the FASTCGI server listening on localhost:9000. The FASTCGI_PARAM directive sets the value of the fastcgi parameter script_filename to "/data/www/index.php" and then fastcgi the server to execute the file. The variable $document_root equals the value set by the root instruction, and the value of the variable $fastcgi_script_name is the requested URI, "/index.php".
The request "/about.html" only matches the location "/", so it will use this location for processing. According to the "root/data/www" directive, Nginx maps the request to the file "/data/www/about.html" and sends the file to the client.
The processing of the request "/" is more complex. It can only match the location "/", so it will use this location for processing. The index directive then uses its parameters and the file path of the "root/data/www" directive to detect the existence of the corresponding file. If the file/data/www/index.html does not exist, and/data/www/index.php exists, this instruction will perform an internal redirect to "/index.php", and then Nginx will re-find the match "/index.php" Location, as if this request was sent from the client. As we have seen before, this redirected request is eventually given to the FASTCGI server for processing.

A simple PHP site configuration

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.