Nginx request processing

Source: Internet
Author: User

Nginx request processing

Directory

  1. Name-based VM
  2. How to avoid processing requests without server names
  3. Hybrid name-based and IP-based virtual servers
  4. Simple PHP site configuration

1. Name-based VM

Nginx first determines the host that processes the request. Let's start with a simple configuration of three VM listeners at port 80.

Server {

Listen 80;

Server_name example.org www.example.org;

...

}

 

Server {

Listen 80;

Server_name example.net www.example.net;

...

}

 

Server {

Listen 80;

Server_name example.com www.example.com;

...

}

In this configuration, nginx only checks the "Host" information in the request header to determine which server the request should be sent to. If this value does not match any server name, or the request does not contain the header Host information, nginx uses the default server to process this request. In the above configuration, the default server is the first (Standard nginx behavior). You can also specify the server as the default server precisely. You only need to add the default_server parameter after the listen command.
Server {
Listen 80 default_server;
Server_name example.net www.example.net;
...
}
Note: default_server is only the listener port attribute, not the server name.

2. How to avoid handling requests without server names
If the request does not contain the "Host" information, the request is not processed. The server can terminate the request by dropping it.
Server {
Listen 80;
Server_name "";
Return 444;
}
Here, the server name is set to an empty string and will match any request without the "Host" header, and then return the Non-Standard Code 404 to the client nginx.

3. Hybrid name-based and IP-based virtual servers
Let's take a look at the following more complex configuration, the different addresses that the virtual server listens.
Server {
Listen 192.168.1.1: 80;
Server_name example.org www.example.org;
...
}
Server {
Listen 192.168.1.1: 80;
Server_name example.net www.example.net;
...
}
Server {
Listen 192.168.1.2: 80;
Server_name example.com www.example.com;
...
}
In this configuration, nginx first tests the requested IP address and port. Then test the "Host" information in the header. If the server name (server name) is not found, the request
Will be processed by the default server. For example, if a request www.example.com is received at 192.168.1.1: 80, it will be processed by the default server at 192.168.1.1: 80.
Www.example.com is not defined on this port.
As mentioned above, the default server is only an attribute of the listening port. Different servers can be defined for different ports.
Server {
Listen 192.168.1.1: 80;
Server_name example.org www.example.org;
...
}
Server {
Listen 192.168.1.1: 80 default_server;
Server_name example.net www.example.net;
...
}
Server {
Listen 192.168.1.2: 80 default_server;
Server_name example.com www.example.com;
...
}

4. Simple PHP site configuration
Now let's take a look at how nginx chooses a location to handle typical simple PHP sites.
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;
}
}

Nginx first looks for the most detailed specified prefix in the order listed. In the above configuration, the unique prefix is "/", and it matches any request, all of which will be used for final processing. Then nginx checks the location of the regular expression according to the order in the configuration file. After the first match, the system will stop searching down and use the location. If no regular expression is matched, nginx uses the most common and oldest location.
Note: All types of location test only some Uris, and do not test the URI parameters. Because parameters can be queried in strings in many ways, such

/Index. php? User = john & page = 1

/Index. php? Page = 1 & user = john

In addition, anyone can request anything like this.

/Index. php? Page = 1 & something + else & user = john

Now let's see how the above configuration handles the request

  • First, the "/logo.gif" request will be matched by the prefix location "/", and then the regular expression "\. (gif | jpg | png) ", so it will be processed by the subsequent location. If the command "root/data/www" is used, this request will be reflected in the/data/www/logo.gif file and then the file will be sent to the client.
  • The request "/index. php" will also be matched by "/" and then matched by the regular expression "\. (php) $. Therefore, it will be processed by the following match, and then the request will be transmitted to the FastCGI server listening on port 9000. The fastcgi_param command sets the FastCGI parameter SCRIPT_FILENAME to "/data/www/index. php ", and then the FastCGI server executes this file. The variable $ document_root is equivalent to the value of the root command. The variable $ fastcgi_script_name is equivalent to the request URI,"/index. php"
  • The request "/about.html" is only matched by the prefix "/", so it is processed only in this location. Run the "root/data/www" command to map the request to the/data/www/about.html file, and then send the file to the client.
  • Processing requests "/" is more complicated. It only matches "/", so it is processed by this location. Then the index Command tests whether the index file exists. If the file/data/www/index.html does not exist, the file/data/www/index. if php exists, run the command to perform an internal redirection to/index. php, then nginx looks for location again, as if the client has sent a request. As shown above, the redirect request will eventually be processed by the FastCGI server.

 

Deployment of Nginx + MySQL + PHP in CentOS 6.2

Build a WEB server using Nginx

Build a Web server based on Linux6.3 + Nginx1.2 + PHP5 + MySQL5.5

Performance Tuning for Nginx in CentOS 6.3

Configure Nginx to load the ngx_pagespeed module in CentOS 6.3

Install and configure Nginx + Pcre + php-fpm in CentOS 6.4

Nginx installation and configuration instructions

Nginx log filtering using ngx_log_if does not record specific logs

Nginx details: click here
Nginx: click here

This article permanently updates the link address:

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.