Execution sequence of Nginx configuration commands (8)

Source: Internet
Author: User
We have discussed in detail the rewrite, access, and content three most common Nginx request processing stages. In this process, this article also introduces many Nginx modules running in these three phases and their configuration instructions. At the same time, we can see that the division of the request processing phase directly affects the execution sequence of the configuration commands.

We have discussed in detail the rewrite, access, and content three most common Nginx request processing stages. In this process, this article also introduces many Nginx modules running in these three phases and their configuration instructions. At the same time, we can see that the division of the request processing phase directly affects the execution sequence of the configuration commands.


We have discussed in detail the rewrite, access, and content processes, which are the most common Nginx request processing stages. In this process, we also introduced many Nginx modules running in these three stages and theirConfigurationCommand. At the same time, we can see that the division of the request processing phase directly affectsConfigurationCommandOfRunSequence, Familiar with these stages for correctConfigurationIt is necessary for different Nginx modules to implement collaboration between them. So next we will discuss the remaining stages.

As mentioned in (1), Nginx processes requests in a total of 11 stages.RunSequenceThe options are post-read, server-rewrite, find-config, rewrite, post-rewrite, preaccess, access, post-access, try-files, content, and log.

FirstRunThe post-read phase of Nginx starts running immediately after Nginx reads and parses the request headers. This phase supports Nginx module registration handler as described earlier in the rewrite phase. For example, the standard module ngx_realip registers the processing program in the post-read phase. Its function is to force Nginx to think that the source address of the current request is the value of a specified request header. The following example uses the set_real_ip_from and real_ip_header provided by the ngx_realip module.ConfigurationCommand:

Server {
Listen 8080;
Set_real_ip_from 127.0.0.1;
Real_ip_header X-My-IP;
Location/test {
Set $ addr $ remote_addr;
Echo "from: $ addr ";

}
}

HereConfigurationIs to let Nginx rewrite the source addresses of all requests from 127.0.0.1 to the value specified by the request header X-My-IP. In addition, the standard built-in variable $ remote_addr is used to output the source address of the current request to confirm whether the request is successfully rewritten.

First, request the/test interface locally:

$ Curl-H 'x-My-IP: 1.2.3.4 'localhost: 8080/test
From: 1.2.3.4

Here, the-H option of the curl tool is used to specify the additional HTTP Request Header X-My-IP: 1.2.3.4. from the output, we can see that the value of the $ remote_addr variable has indeed become the value specified in the X-My-IP request header in the rewrite phase, that is, 1.2.3.4. so when did Nginx rewrite the source address of the current request? The answer is: in the post-read phase. Because the rewrite stage is far behind the post-read stageConfigurationBlock through the setConfigurationCommandWhen you read the $ remote_addr built-in variable, the read source address has been tampered with during the post-read phase.

If the X-My-IP request header is not specified for the/test interface in the request above, or the value of the X-My-IP request header provided is not a legal IP address, nginx will not rewrite the source address, for example:

$ Curl localhost: 8080/test
From: 127.0.0.1

$ Curl-H 'x-My-IP: abc' localhost: 8080/test
From: 127.0.0.1

If you access this/test interface from another machine, Nginx will not be triggered to rewrite the source address even if a valid X-My-IP request header is specified. This is because set_real_ip_from has been used in the above example.CommandIt is specified that the source address rewrite operation only takes effect for those requests from 127.0.0.1. This filtering mechanism can prevent malicious spoofing from other untrusted addresses. Of course, you can also use set_real_ip_fromCommandSpecify an IP network segment (using the "CIDR notation" introduced in (3 "). In additionConfigurationMultiple set_real_ip_from statements are also allowed, so that multiple trusted source addresses or CIDR blocks can be specified. The following is an example:

Set_real_ip_from 10.32.10.5;
Set_real_ip_from 127.0.0.0/24;

Some readers may ask, what is the actual use of the ngx_realip module? Why do we need to rewrite the source address of the request? The answer is: this module becomes particularly useful when requests processed by Nginx are forwarded by an HTTP proxy server. After the original user request is forwarded, the source address of the request received by Nginx is changed to the IP address of the proxy server without exception, therefore, the application behind Nginx and Nginx cannot know the real source of the original request. Therefore, we generally encode the original source address of the request into a special HTTP Request Header in the proxy server before Nginx (for example, the X-My-IP request header in the above example ), then, the address encoded in the request header is restored on the Nginx side. In this way, the subsequent processing phase of Nginx (including the various backend applications behind Nginx) will assume that these requests come directly from those original addresses, and the proxy server does not seem to exist. Because of this requirement, the ngx_realip module needs to register the processing program in the first processing phase, namely the post-read phase, to rewrite the request source as early as possible.

The post-read phase is followed by the server-rewrite phase. We once briefly mentioned in (2) that when the ngx_rewrite ModuleConfigurationCommandDirectly written on the serverConfigurationWhen the block is in the server-rewrite phase. The following is an example:

Server {
Listen 8080;
Location/test {
Set $ B "$ a, world ";
Echo $ B;
}
Set $ a hello;
}

Here,ConfigurationThe statement set $ a hello is directly written on the server.ConfigurationSo it runs in the server-rewrite stage. The server-rewrite stage runs earlier than the rewrite stage, so it is written in locationConfigurationThe statement set $ B "$ a, world" in the block is later than the external set $ a hello statement. The test results in this example prove this point:

$ Curl localhost: 8080/test
Hello, world

Because the server-rewrite stage is after the post-read stageConfigurationSet in the blockCommandIt always runs after the ngx_realip module changes the source address of the request. Let's look at the example below:

Server {
Listen 8080;
Set $ addr $ remote_addr;
Set_real_ip_from 127.0.0.1;
Real_ip_header X-Real-IP;
Location/test {
Echo "from: $ addr ";
}
}

The result of the request/test interface is as follows:

$ Curl-H 'x-Real-IP: 1.2.3.4 'localhost: 8080/test

From: 1.2.3.4

In this exampleCommandWritten in ngx_realipConfigurationCommandBut still later than the ngx_realip ModuleRun. Therefore, the $ addr variable is set in the server-rewrite phase.CommandWhen the value is assigned, the source address read from the $ remote_addr variable has been rewritten.

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.