Record of problems encountered in the use of Nginx

Question one, about spaces

The nginx configuration is very sensitive to spaces. Before and after keywords and symbols, be sure to remember spaces (or line breaks). A typical scenario is the if {} statement, with spaces before and after the braces, otherwise unexpected behavior may occur.

Question 2: About the problem of server block

When all the server blocks in the http block cannot match the current request, nginx will use the first server block as the matching selection item in the pocket.

Therefore, in most cases, a common server block needs to be placed at the top of the most server block to handle requests without any matching, such as returning 404.

http {

    server {
        listen 80;
        server_name _;
        return 404;
    }

        ...
}

Question three: Server block matching rules, that is, how does nginx decide which server block to use to process the request

What works is the two configurations listen and server_name in the server block.

Among them, listen defines what [IP]: [PORT] requests the current server block will handle, and server_name defines what conditions the host header needs to satisfy in the current server block request.

Here is an excerpt from the description of the big brother, the original link is here :


  1. When trying to determine which server block to send a request to, Nginx will first try to listen to the specificity of the instruction using the following rules:
    1) Nginx replaces all incomplete leasten instructions with the default default values ​​(complete: IP + port The default value of the combination), so the listen command of each server block can be regarded as a combination of IP address and port. Examples of such conversions are:
      a. Blocks without listen instructions use this value 0.0.0.0:80.
           b. The block set to 111.111.111.111 with no port IP address becomes 111.111.111.111:80
           c. The block set to 8888 with no IP address port becomes 0.0.0.0:8888
    2) Next Nginx will try to collect one A list of server blocks. This list is based on the specific IP and port best match. In other words, if the matching server block has a specific IP address, it will not match the server block with 0.0.0.0 as the default IP address. In any case, in the process of Nginx selecting the server block, the ports must match exactly.
    3) If there is only one most specific match, then the server block will be used to provide the request. If there are multiple server blocks with specific matches at the same level, then Nginx needs to continue to evaluate the server_name instruction.

  2. It is important to note that Nginx will continue to evaluate the server_name instruction only when the listen instruction has multiple matching server blocks at the same level.
  3. Next, to further evaluate requests with the same specific listen instruction, Nginx checks the request's "host" header, which contains the domain or IP address that the client actually tried to access.
    Nginx looks at its server_name instruction in each candidate server module and tries to find the best match. Nginx is evaluated by the following formula:
    1) Nginx first finds the server module whose server_name exactly matches the requested Host header information. If this server module is found, it will be used to serve client requests. If multiple specific matches are found, the first one will be used to provide services.
    2) If no exact match is found, Nginx will next try to find the server module whose server_name matches the preceding wildcard character (indicated by * at the beginning of the name in the configuration). Once one is found, this server module will be used to provide services to the client. If multiple matches are found, the server module with the longest match will be used to provide services.
    3) If no match is found using the preceding wildcard, Nginx will next try to find the server module whose server_name matches the following wildcard (the end of the name in the configuration is indicated by *). Once one is found, this server module will be used to provide services to the client. If multiple matches are found, the server module with the longest match will be used to provide services.
    4) If no match is found after using the wildcard, Nginx will next evaluate the server module that defines server_name with a regular expression (denoted by ~ in front of the name). The first server_name with a regular expression matching the Host header will be used to provide services.
    5) If no matching server module with server_name defined by regular expression is found, Nginx will then use the server module with default IP and port.

 

Guess you like

Origin www.cnblogs.com/duanzi6/p/12694662.html