Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Ngnix handle http requests

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "how Ngnix handles http requests". The content is simple and clear. I hope it can help you solve your doubts. Let me lead you to study and learn this article "how Ngnix handles http requests".

Nginx processing http requests is the most important and complex part of nginx.

I. name-based virtual server

Nginx first determines which server should process the request. Let's start with a simple configuration where all three virtual servers are listening on port *: 80:

Server {listen 80th serverSecretname example.org www.example.org;...} server {listen 80th serverSecretname example.net www.example.net;...} server {listen 80th serverSecretname example.com www.example.com;...}

In this configuration, nginx only tests the header field "host" of the request to determine which server the request should be routed to. If its value does not match any server name, or if the request does not contain this header field at all, nginx routes the request to the default server on that port. In the above configuration, the default server is the first server-this is the standard default behavior of nginx. You can also use the default_server parameter in the listen directive to explicitly set which server should be the default server:

Server {listen 80 default_server;server_name example.net www.example.net;...}

Since version 0.8.21, the default_server parameter is available. In previous versions, the default parameters should be used instead.

Note that the default server is the property of the listening port, not the server name. I'll explain it in more detail later.

2. How to block http requests with undefined server names

If requests without the "host" header field are not allowed, you can define a server that only discards the request:

Server {listen 80server_name "; return 444;}

Here, the server name is set to an empty string, which will match the request without the "Host" header field and return the non-standard code 444 of the special nginx to close the connection.

Starting with version 0.8.48, this is the default setting for server names, so server_name "" can be omitted. In previous versions, the hostname of the computer was used as the default server name.

Virtual server based on a mixture of name and IP address

Let's look at a more complex configuration where some virtual servers listen to different addresses:

Server {listen 192.168.1.1 example.org www.example.org;...} server {listen 192.168.1.1 VR 80th serverSecretname example.net www.example.net;...} server {listen 192.168.1.2 Velcro 80th serverSecretname example.com www.example.com;...}

In this configuration, nginx first tests the requested IP address and port according to the listening instructions of the server block. It then tests the requested "host" header field based on the server_name entry of the server block that matches the IP address and port. If the server name is not found, the default server processes the request. For example, requests for www.example.com received on port 192.168.1.1 will be processed by the default server (that is, the first server) on port 192.168.1.1, because there is no www.example.com defined for this port.

As mentioned earlier, the default server is the property of the listening port, and different default servers can be defined for different ports:

Server {listen 192.168.1.1 server {listen 192.168.1.1 server 80 default_server;server_name example.net www.example.net;...} server {listen 192.168.1.2 VR 80 default_server;server_name example.com www.example.com;...} IV, a simple PHP site configuration

Now, let's see how nginx chooses a location to handle requests from a typical simple PHP site:

Server {listen 80server_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;}}

Regardless of the order in which it is listed, nginx first searches for the most specific prefix position given by the text string. In the above configuration, the only prefix position is "/" and because it matches any request, it will be used as a last resort. Nginx then checks the location specified by the regular expression in the order listed in the configuration file. The first matching expression stops the search, and nginx uses this location. If no regular expression matches the request, nginx uses the most specific prefix location discovered earlier.

Note that all types of locations test only the URI portion of the request line that has no parameters. This is done because the parameters in the query string can be given in several ways, such as:

/ index.php?user=john&page=1/index.php?page=1&user=john

In addition, anyone can request anything in the query string:

/ index.php?page=1&something+else&user=john

Now, let's take a look at how the request is handled in the above configuration:

The request "/ logo.gif", first matches the prefix position "/", then matches the regular expression ". (gif | jpg | png) $", so it is processed by the latter location. Use the directive "root / data/www" to map the request to the file / data/www/logo.gif, and then send the file to the client.

The request "/ index.php" also matches the prefix position "/" first, and then the regular expression ". (php). Therefore, it is processed by the latter location and passes the request to the server listening on:. The instruction sets the parameter to, and then the server executes the file. The variable document_root is equal to the value of the root instruction, and the variable $fastcgi_script_name is equal to the request URI, that is," / index.php ".

The request "/ about.html" only matches the prefix position "/", so it is processed at that location. Use the directive "root / data/www" to map the request to the file / data/www/about.html, and then send the file to the client.

Processing the request "/" is more complex. It only matches the prefix position "/", so it is handled by that location. The index instruction then tests the existence of the index file based on its parameters and the "root / data/www" instruction. If the file / data/www/index.html does not exist and the file / data/www/index.php exists, the directive redirects internally to "/ index.php" and the nginx searches for the location again if the request is sent by the client. As mentioned earlier, the request for redirection will eventually be processed by the FastCGI server.

The above is all the content of the article "how Ngnix handles http requests". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report