This article details the method of nginx server responding to and processing http and other requests, and explains the configuration method of nginx virtual host. Friends in need can refer to it.
First, nginx name-based virtual host
Nginx first selects which virtual host to handle the request.
Start with a simple configuration (where all 3 virtual hosts are listening on port *:80):
Copy code Code example:
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 the request should be handled by. If the Host header does not match any virtual host, or the request does not contain a Host header at all, nginx will distribute the request to the default virtual host defined on this port. In the above configuration, the first virtual host listed is nginx's default virtual host - this is nginx's default behavior. Moreover, you can explicitly set a host as the default virtual host, that is, set the "default_server" parameter in the "listen" command:
Copy code Code example:
server {
listen 80 default_server;
server_name jbxue.net www.jbxue.net;
…
}
The "default_server" parameter is available starting from version 0.8.21. In previous versions, the "default" parameter should be used instead.
Please note that "default_server" is an attribute of the listening port, not the host name. More on this later.
How to prevent the processing of requests with undefined host names
If the "Host" header is missing from the request, you can define the following host and discard these requests:
Copy codeCode example:
server {
Listen 80; er server_name "" "";
Return 444;
}
Virtual host based on mixed domain name and IP
Copy codeCode example:
server {
listen 192.168.1.1:80;
server_name jbxue.org www.jbxue.org;
...
}
listen 192.168.1.1:80;
server_name jbxue. net www.jbxue.net;
...
}
listen 192.168.1.2:80;
server_name jbxue.com www.jbxue.com;
...
}
Copy codeCode example:
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;
...
}
Second, a simple PHP site configuration
In a typical, simple PHP site, how nginx selects location to process a request:
Copy codeCode example:
server {
Listen 80;
server_name jbxue.org www.jbxue.org;
root /data/www;
location / {
index index.html index.php;
}
location ~* .(gif|jpg|p ng)$ {
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 out The most accurate location. In this step, nginx will ignore the order in which locations appear in the configuration file.
In the above configuration, the only prefix matching location is "/", and because it can match any request, it is used as the last choice.
Then, nginx continues to match the locations of the regular expressions in the order in the configuration, and stops searching after matching the first regular expression.
The matched location will be used. If there is no location matching the regular expression, the location with the most accurate prefix match just found is used.
Please note that all location matching tests only use the URI part of the request, not the parameter part. 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 them at will in the request string. String:
/index.php?page=1&something+else&user=john
Let’s see how the request is processed 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" command, nginx maps the request to the file /data/www/logo.gif" and sends this 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 at localhost:9000. The fastcgi_param directive sets the value of the FastCGI parameter SCRIPT_FILENAME to " /data/www/index.php", and then the FastCGI server executes this file. The variable $document_root is equal to the value set by the root command, and the value of the variable $fastcgi_script_name is the requested uri, "/index.php".
Request "/about .html" can only match the location "/", so it will use this location for processing. According to the "root /data/www" instruction, nginx maps the request to the file "/data/www/about.html", and Send this file to the client.
The processing of the request "/" is more complicated. It can only match the location "/", so it will be processed using this location.
Then, the index directive uses its parameters and " root /data/www" command to detect whether the corresponding file exists.
If the file /data/www/index.html does not exist and /data/www/index.php exists, this command will be executed once Internal redirection to "/index.php", then nginx will re-find the location matching "/index.php", as if the request was sent from the client
As seen before, this redirection. The request is finally handed over to the FastCGI server for processing
The above has introduced a detailed explanation of nginx's method of responding to and processing requests, including relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.