Detailed explanation of nginx built-in variables

The Ngx_http_core_module module supports built-in variables, and their names are consistent with the built-in variables of apache. Humanization, understanding these variables will make it very convenient for us to configure,


For example: header forwarding in load balancing allows the backend server to obtain the real ip requested by the client.  

First, let's look at a basic request header. This is my request to www.baidu.com. The request header is as follows:

Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8  
Accept-Encoding gzip, deflate, br  
Accept-Language zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3  
Connection  keep-alive  
Host    www.baidu.com  
Referer https://www.baidu.com/s?wd=aaa&rsv_spt=1&rsv_iqid=0x80378a480003084f&issp=1&f=8&rsv_bp=0&rsv_idx=2&  
ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_sug3=4&rsv_sug1=2&rsv_sug7=100&rsv_sug2=0&inputT=1804&rsv_sug4=1805  
User-Agent  Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:47.0) Gecko/20100101 Firefox/47.0  



Why post this? Because the built-in variables of nginx that I will introduce next are related to them. It's easier to understand while looking at it, isn't it?
The list of commonly used built-in variables is as follows.

$args   
This variable is equal to the parameters in the request line. Note the referer in my request. This variable indicates that all parameters of my request

$content_length are
equal to the value of "Content_Length" of the request line.

$content_type
is equivalent to the value of "Content_Type" in the request header

$document_root is
equivalent to the value specified by the root directive of the current request

$document_uri
Before explaining this variable, you need to explain nginx normalization. The
so-called normalization is to first put the URI in the form of "%XX" The encoded characters of the URI are decoded, and then the relative path "." and ".." parts in the URI are parsed. In addition, two or more adjacent slashes may be compressed into one slash,
which is like www.xxx The reason why .com///index.html can also be accessed. The url before the
specification is www.xxx.com///index.html and it is stored in $request_uri
while the url after the specification is www.xxx.com/index .html stored in $uri nginx will compress two or more adjacent slashes into a single slash

$host
and the value specified by the "Host" line in the request header or the name of the server the request reaches (without the Host line ) like the above example does not reflect

$limit_rate
The connection rate that is allowed to be limited is generally not commonly used.

$request_method
is equivalent to the method of the request, usually "GET" or "POST"

$remote_addr
client ip When doing load balancing, if you want to get the client's request ip, you need this

$remote_port
client The terminal port

$remote_user is
equivalent to the user name, authenticated by the ngx_http_auth_basic_module

$request_filename
The path name of the file currently requested, which is composed of root or alias and URI request


$request_body_file
The file in the request body

$request_uri
contains the complete initial URI of the parameters as in the above example that is

https://www.baidu.com/s?wd=aaa&rsv_spt=1&rsv_iqid=0x80378a480003084f&issp=1&f=8&rsv_bp=0&rsv_idx=2&  
ie=utf-8&tn=baiduhome_pg&rsv_enter=1&rsv_sug3=4&rsv_sug1=2&rsv_sug7=100&rsv_sug2=0&inputT=1804&rsv_sug4=1805  

$query_string
is the same as $args The parameter in the request

$sheeme
http mode (http, https)

$server_protocol
is equivalent to the request protocol, use "HTTP/ or "HTTP/


$server_addr request
to reach the ip of the server, generally get the value of this variable The purpose is to make a system call. In order to avoid system calls, it is necessary to specify ip in the listen command and use the bind parameter.

$server_name
The name of the server where the request arrives

$server_port
The port number of the server where the request arrives

$uri
is equal to the URI in the current request, and can be different from the initial value, such as internal redirection or using index


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324510342&siteId=291194637
Recommended