Nginx variables

    Nginx variables have only one type of string, and use the $ sign before the variable name to refer to the variable. But it does not support the $ symbol by turning clothes, which can be represented by the ngx_geo module, such as:

    geo $dollar {

        default "$";

    }, you can use $dollar to represent $.

    Nginx's set directive first creates variables when nginx starts and loads the configuration, and the assignment of variables occurs when the request is processed. Its visible scope is the entire Nginx configuration. However, note that although the visible scope of Nginx variable names is the entire configuration, each request has an independent copy of all variables, or an independent copy of the container in which each variable is used to store values, without interfering with each other.

    Internal jump refers to the process of jumping from one location to another during a request, caused by ngx_echo rewrite. External jumps refer to jumps with status codes 301 and 302. In the internal jump, because it is the same request, the variable does not change. Even if the location changes, it is still the same copy.

    There are two types of Nginx variables, one is the variable created by the set and other instructions, which is a user-defined variable, and the other is the built-in variable pre-defined by Nginx, which contains various information of the request or response, such as the $uri return after decoding. A URI with parameters, and $request_uri represents an undecoded URI with parameters. It should be noted that nginx will convert parameter names to lowercase to match. Built-in variables do not have the concept of containers, and they need to be calculated by nginx every time they are used. $args supports write operations to modify URL parameter strings. A variable with a value container is called "indexed" in Nginx core; otherwise, it is called "unindexed". Groups of variables like $arg_XXX with an infinite number of variants are "unindexed". When reading such a variable, it is actually its "fetch handler" that works, that is, it scans the URL parameter string of the current request in real time, and extracts the value of the URL parameter specified by the variable name. 

    The nginx map module directive can be used to define the mapping relationship between two Nginx variables.

        map $args $foo {

            default 0;

            debug 1;

         }, when the value of $args is equal to debug, the value of the variable $foo is 1, otherwise the value of $foo is 0. The ngx_map module considers that the mapping calculation between variables is expensive enough, and needs to automatically cache the calculation result of the dependent variable, so that if the dependent variable is read again during the processing of the current request, Nginx can directly return the cached result. The ngx_geo module also caches the results.

    Requests in Nginx are divided into main requests and sub-requests. The main request is the request initiated by the HTTP client from outside Nginx, and the sub-request is the request made inside Nginx, which is to split the main request into multiple smaller requests for parallel or serial execution. Triggered by echo_location, subrequest smart keys and main request variables have their own independent copies, as do ngx_echo, ngx_lua, etc. The sub-request triggered by ngx_auth_request shares the variable container with the main request, but the response body of the sub-request is ignored.

    The first parameter of echo_location specifies the URI of the "subrequest", and the second parameter specifies the URL parameter string of the "subrequest". But the ones obtained through $request_method and $request_uri are mainly used for the main request, and $echo_request_method is used for sub-requests.

    In Nginx, if the variable is created but not assigned, it is illegal, and if there is no parameter to be obtained in the URL parameter, it is not found. But since there is only one string type in Nginx, they are all converted to empty strings. Obtaining "illegal" variables is indicated in the Nginx log, but "not found" is not. If you distinguish such variables, you can use the nil type in lua to achieve. Such as:

content_by_lua

    'if ngx.var.arg_name == nil then

        ngx.say("name: missing")
    else
        ngx.say("name: [", ngx.var.arg_name, "]")
    end
';

nil when the value of $arg_name is "not found" (or "invalid").

    

    

    

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326963351&siteId=291194637