How does nginx handle a request?

Source: Internet
Author: User

 

Original article: http://nginx.org/en/docs/http/request_processing.html

 

 

 

Name-based Virtual Server

 

Nginx first determines which server should process the request. Let's start with a simple example: three virtual servers listening to port 80

 

Server {

Listen 80;

Server_name nginx.org www.nginx.org;

...

}

 

Server {

Listen 80;

Server_name nginx.net www.nginx.net;

...

}

 

Server {

Listen 80;

Server_name nginx.com www.nginx.com;

...

}

 

In this configuration, nginx only uses the header line "Host" to determine which server to process the request.

If the "Host" header line does not match any server_name, or the server_name is not included in the user request,

Then nginx will forward this request to the default server. In the preceding configuration, the first one is the default server (which is the default configuration rule of nginx ).

If you do not want to use the first server as the default server, you can add the default_server parameter to the listen command to specify

As follows:

Server {

Listen 80 default_server;

Server_name nginx.net www.nginx.net;

...

}

 

 

PS: nginx versions 0.8.21 and later support the "default_server" parameter.

The default_server parameter is an attribute of the listening port, not the server name.

 

 

 

How can I block requests without a specified server name?

If you do not want to process an unspecified "Host" header line request, you need to define a server that discards the request.

As follows:

Server {

Listen 80;

Server_name "";

Return 444;

}

 

If server_name is null, a request without "Host" header line can be matched. A specific code 444 is returned and the client connection is disconnected.

 

After version 0.8.48, nginx has the ability to process the preceding request, so this setting can be ignored in the configuration file.

 

 

 

Virtual servers with mixed server names and IP addresses

 

Let's look at a more complex configuration. The virtual server listens to ports of different addresses.

As follows:

 

Server {

Listen 192.168.1.1: 80;

Server_name nginx.org www.nginx.org;

...

}

 

Server {

Listen 192.168.1.1: 80;

Server_name nginx.net www.nginx.net;

...

}

 

Server {

Listen 192.168.1.2: 80;

Server_name nginx.com www.nginx.com;

...

}

 

 

In this configuration, nginx first checks the IP address and port corresponding to the "listen" command in the "server" module.

Then, check the "server_name" item of the "server" module in the "Host" header line of the request.

If the host name cannot be found, the request will be forwarded to the default server.

For example:

192.168.1.1: Port 80 will forward the request to 192.168.1.1: 80 of the first server after receiving a request from www.nginx.com,

Even if the first server does not specify www.nginx.com

 

As mentioned earlier, the default server is the listener port attribute. Different default servers may be specified with different listener ports.

For example:

Server {

Listen 192.168.1.1: 80;

Server_name nginx.org www.nginx.org;

...

}

 

Server {

Listen 192.168.1.1: 80 default_server;

Server_name nginx.net www.nginx.net;

...

}

 

Server {

Listen 192.168.1.2: 80 default_server;

Server_name nginx.com www.nginx.com;

...

}

 

 

 

Configuration of a simple PHP site

Now let's look at an example of how to select location for nginx to handle a typical simple PHP site request:

Server {

Listen 80;

Server_name nginx.org www.nginx.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;

}

}

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.