Use of nginx custom variables and built-in predefined variables

Use of nginx custom variables and built-in predefined variables

Overview

Nginx can use variables to simplify configuration and improve configuration flexibility. All variable values ​​can be referenced in this way:

$variable name

There are two types of variables in nginx: custom variables and built-in predefined variables

Built-in variables

statement

You can use the set command (non-unique) to declare variables in tags such as server, http, location, etc. The syntax is as follows

set $variable name variable value

Note that all variables in nginx must start with $.

Visibility

All variables used in the nginx configuration file must be declared, otherwise nginx will fail to start and print related exception logs

An interesting feature of nginx variables is that none of them are globally visible, yet they are not global variables. For example, the following example

location a/
 return 200 $a
}

location b/ {
 set $a hello nginx
 return 200 $a
}

Since the variable is globally visible, nginx will not report an error when it starts. However, the specific value of $a is unknown in the first location, so the returned response result is an empty string.

The visibility rules for variables declared in tags at different levels are as follows:

  • The variables declared in the location tag are visible to this location block.
  • Variables declared in the server tag are visible to the server block and all sub-blocks in the server block.
  • Variables declared in the http tag are visible to the http block and all sub-blocks in the http block.

Built-in predefined variables

Built-in predefined variables are variables that can be used without declaration, usually including the value of a part of the content in an HTTP request or response. The following are some commonly used built-in predefined variables

Variable Name definition
$arg_PARAMETER The value of the parameter named PARAMETER in the GET request.
$args This variable is equal to the parameter in the GET request. For example, foo=123&bar=blahblah; this variable can only be modified
$binary_remote_addr The client address in binary format.
$body_bytes_sent The number of bytes of the transferred page
$content_length The Content-length field in the request header.
$content_type The Content-Type field in the request header.
$cookie_COOKIE The value of the cookie COOKIE.
$document_root The value specified in the root directive for the current request.
$document_uri Same as $uri.
$host The Host header field in the request. If the Host header in the request is not available or empty, the name of the server processing the request (the value of the server_name directive of the server processing the request). The value is lowercase and does not include the port.
$hostname The machine name is the value of the gethostname system call.
$http_HEADER The content in the HTTP request header, HEADER is the content in the HTTP request converted to lowercase, - becomes _ (dash becomes underscore), for example: $http_user_agent (the value of Uaer-Agent);
$sent_http_HEADER The content in the HTTP response header, HEADER is the content in the HTTP response converted to lowercase, - becomes _ (dash becomes underscore), for example: $sent_http_cache_control, $sent_http_content_type…;
$is_args If $args is set, the value is "?", otherwise it is "".
$limit_rate This variable can limit the connection rate.
$nginx_version The currently running nginx version number.
$query_string Same as $args.
$remote_addr The IP address of the client.
$remote_port The client's port.
$remote_user The username that has been authenticated by the Auth Basic Module.
$request_filename The file path of the current connection request, generated by the root or alias directive and the URI request.
$request_body This variable (0.7.58+) contains the main information of the request. It makes sense in locations that use the proxy_pass or fastcgi_pass directives.
$request_body_file The temporary file name of the client request body information.
$request_completion Set to "OK" if the request was successful; set to empty if the request was not completed or was not the last in a series of requests.
$request_method This variable is the action requested by the client, usually GET or POST. In versions 0.8.20 and earlier, this variable is always the action in the main request. If the current request is a subrequest, the action of the current request is not used.
$request_uri This variable is equal to the original URI including some client request parameters. It cannot be modified. See $uri to change or rewrite the URI.
$scheme The protocol used, such as http or https, such as rewrite ^(.+)$ $scheme://example.com$1 redirect;
$server_addr Server address. This value can be determined after completing a system call. If you want to bypass the system call, you must specify the address in listen and use the bind parameter.
$server_name Server name.
$server_port The port number on which the request arrived at the server.
$server_protocol The protocol used in the request, usually HTTP/1.0 or HTTP/1.1.
$uri The current URI in the request (without request parameters, the parameters are in a r g s ) , No same At Browse View Device pass Deliver of args), which , and it can be modified through internal redirection or using the index directive. Do not include the protocol and hostname, e.g. /foo/bar.html

This is the end of this article about the use of nginx custom variables and built-in predefined variables. For more relevant nginx custom variables and built-in predefined variables, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed explanation and isolation of nginx built-in variables for simple interception
  • Share the latest version of nginx built-in variables
  • A brief analysis of the writing and use of variables in the Nginx configuration file
  • Summary of nginx global variables
  • Summary of global variables in Nginx

<<:  MySQL trigger principle and usage example analysis

>>:  Practice of Vue global custom instruction Modal drag

Recommend

CSS Pick-up Arrows, Catalogs, Icons Implementation Code

1. CSS Miscellaneous Icons There are three ways t...

How to use squid to build a proxy server for http and https

When we introduced nginx, we also used nginx to s...

Summary of some efficient magic operators in JS

JavaScript now releases a new version every year,...

Dealing with the problem of notes details turning gray on web pages

1. In IE, if relative positioning is used, that is...

js to achieve simple accordion effect

This article shares the specific code of js to ac...

A Deeper Look at the Differences Between Link and @import

There are three main ways to use CSS in a page: ad...

Detailed explanation of where the image pulled by docker is stored

20200804Addendum: The article may be incorrect. Y...

Web Design Help: Web Font Size Data Reference

<br />The content is reproduced from the Int...

UDP connection object principle analysis and usage examples

I wrote a simple UDP server and client example be...

Analysis of the process of building a cluster environment with Apache and Tomcat

In fact, it is not difficult to build an Apache c...

MySQL database master-slave replication and read-write separation

Table of contents 1. Master-slave replication Mas...

Solution to index failure in MySQL due to different field character sets

What is an index? Why create an index? Indexes ar...

How to set static IP in CentOS7 on VirtualBox6 and what to note

Install CentOS 7 after installing VirtualBox. I w...