: This article describes nginx response and request handling methods. For more information about PHP tutorials, see. This article describes how to respond to and process http requests on the nginx server, and explains how to configure the nginx virtual host. For more information, see.
1. nginx name-based virtual host
Nginx first selects the VM to process the request.
Start with a simple configuration (all three of them are listening on port *: 80:
Sample code:
Server {
Listen 80;
Server_name jbxue.org www.jbxue.org;
...
}
Server {
Listen 80;
Server_name jbxue.net www.jbxue.net;
...
}
Server {
Listen 80;
Server_name jbxue.com www.jbxue.com;
...
}
In this configuration, nginx only checks the "Host" header of the request to determine which virtual Host should process the request. If the Host header does not match any virtual Host, or the request does not contain the Host header, nginx will distribute the request to the default virtual Host defined on this port. In the preceding configuration, the first virtual host to be listed is the default virtual host of nginx-this is the default behavior of nginx. Additionally, you can explicitly set a host as the default virtual host, that is, set the "default_server" parameter in the "listen" command:
Sample code:
Server {
Listen 80 default_server;
Server_name jbxue.net www.jbxue.net;
...
}
The "default_server" parameter is available from version 0.8.21. In earlier versions, the "default" parameter should be used.
Note that "default_server" is the listener port attribute, not the host name attribute. More information will be provided later.
How to prevent requests with undefined host names from being processed
If the "Host" header is not allowed in the request, you can define the following Host and discard these requests:
Sample code:
Server {
Listen 80;
Server_name "";
Return 444;
}
Here, the Host name is set to a null string to match the request with the undefined "Host" header, and an nginx-specific non-http standard 444 return code is returned, which can be used to close the connection.
Starting from version 0.8.48, this has become the default settings for host names, so server_name "" can be omitted "". In earlier versions, the host name of the machine is used as the default value of the host name.
Virtual host based on hybrid domain name and IP address
A complex configuration, in which several virtual hosts listen on different addresses:
Sample code:
Server {
Listen 192.168.1.1: 80;
Server_name jbxue.org www.jbxue.org;
...
}
Server {
Listen 192.168.1.1: 80;
Server_name jbxue.net www.jbxue.net;
...
}
Server {
Listen 192.168.1.2: 80;
Server_name jbxue.com www.jbxue.com;
...
}
In this configuration, nginx first tests whether the requested IP address and port match the listen command configuration in a server configuration block. Then nginx continues to test whether the requested Host header matches a server_name value in the server block. If the host name is not found, nginx will send this request to the default virtual host for processing. For example, a request to access www.jbxue.com received from Port 192.168.1.1: 80 will be listened to by the default VM of port 192.168.1.1: 80 for processing. In this example, it is the first server, because no virtual host named www.jbxue.com is defined on this port.
The default server is the listener port attribute, so you can set different default servers for different listening ports:
Sample code:
Server {
Listen 192.168.1.1: 80;
Server_name jbxue.org www.jbxue.org;
...
}
Server {
Listen 192.168.1.1: 80 default_server;
Server_name jbxue.net www.jbxue.net;
...
}
Server {
Listen 192.168.1.2: 80 default_server;
Server_name jbxue.com www.jbxue.com;
...
}
2. a simple PHP site configuration
In a typical and simple PHP site, how does nginx select location for a request:
Sample code:
Server {
Listen 80;
Server_name jbxue.org www.jbxue.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. in this step, nginx will ignore the order in which location appears in the configuration file.
In the above configuration, the unique prefix matching location is "/", and is used as the last choice because it can match any request.
Then, nginx continues to match the location of the regular expression in sequence according to the configuration order, and then stops searching after matching the first regular expression.
The matched location will be used. If the location of the regular expression is not matched, use the location with the most accurate prefix.
Note that all location matching tests only use the URI part of the request, rather than the parameter part. This is because there are many methods to write parameters, such:
/Index. php? User = john & page = 1
/Index. php? Page = 1 & user = john
In addition, anyone can add strings as needed in the request string:
/Index. php? Page = 1 & something + else & user = john
Let's take a look at how the request is processed using the above configuration:
Request "/logo.gif" first matches location "/", then matches regular expression "\. (gif | jpg | png) $ ". Therefore, it will be processed by the latter. According to the "root/data/www" command, nginx maps requests to the file/data/www/logo.gif "and sends the file to the client.
The request "/index. php" first matches 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 command sets the value of the FastCGI parameter SCRIPT_FILENAME to "/data/www/index. php", and then runs the file on the FastCGI server. The variable $ document_root is equal to the value set by the root command. The value of the variable $ fastcgi_script_name is the requested uri, "/index. php ".
The request "/about.html" can only match the location "/", so it uses this location for processing. According to the "root/data/www" command, nginx maps requests 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 location "/", so it will use this location for processing.
Then, the index command uses the file path composed of its parameters and the "root/data/www" command to check whether the corresponding file exists.
If the file/data/www/index.html does not exist, and/data/www/index. php exists. this command will execute an internal redirection to "/index. php ", then nginx will search for matching Again"/index. php "location, as if this request was sent from the client.
As we can see before, this redirection request is finally handed over to the FastCGI server for processing.
The above describes the nginx response and request processing methods, including the content, hope to be helpful to friends who are interested in the PHP Tutorial.