nginx subrequest

#main request & subrequests
>main request:HTTP requests initiated from the client side
>A subrequest is an abstract invocation for decomposing the task of the main request into smaller “internal requests” that can be served independently by multiple different location blocks, either in series or in parallel
>the communication of location blocks via subrequests is limited within the same server block (i.e., the same virtual server configuration)
#Variables with the same name between a parent request and a subrequest will generally not interfere with each other
location /main {
set var main;  echo_location /foo; echo_location /bar;  echo "main: var”;

location /foo {
set var foo; echo "foo: var”;

location /bar {
set var bar; echo "bar: var”;

 curl 'http://localhost:8080/main' foo: foo bar: bar</div> <div>main: main</div> <div><b> </b></div> <div><b>Exception: </b><b>Shared Variable Containers among Requests </b></div> <div><b> </b></div> <div>location /main { set var main;
auth_request /sub;
echo “main: var"; }  location /sub { set var sub;
echo “sub: var";</div> <div>}</div> <div>================================</div> <div> curl ‘http://localhost:8080/main’
main: sub
#build-in variables in subrequest
>sensitive to subrequest
args</div> <div>uri
echo_request_method</div> <div>>main request onlyrequest_method

nginx variables


#set variables
set a "hello world";  <strong>#Nginx variable creation and assignment happen at completely different phases</strong>  1.Nginx creates all the Nginx variables while loading the configuration file (or in other words, at "configuration time")  2.variable assignment occurs when requests are actually being served  Once an Nginx variable is created, it is visible to the entire configuration, even across different virtual server configuration blocks, regardless of the places it is declared at.  <strong>#internal redirection</strong> when visit /foo , page will redirect to /bar internally.and you can accessa also.
server {
listen 8080;
location /foo {
set a hello; rewrite ^ /bar; } location /bar { echo "a = [a]”;

#build-in variables
uri  request_uri(include query string)
arg_XXX   [argumets' name based on the url query string (<span style="color: #ff0000;">case insensitive</span>  eg. Name ,NAME ==> arg_name)] <a href=""></a>  >most build-in variables are <span style="color: #ff0000;">read-only</span>, but there are some exceptionargs(when you change args manually, it will influencesargs in everywhere)

#variables get handlers & set handlers
in order to save memory, not-indexed variables in Nginx only have one copy , with lots of pointer pointing to them. Every time you need to fetch the variables , you need to parse them from the beginning.

#map for caching
use as cache,run get handlers once . read once ,then the value fixs.
(put outside the server configuration block, that is, it is defined directly within the outermost http configuration block)
map args foo {
default     0;
debug       1;
when args == default ,foo = 0;when args == debug ,foo = 1

#lazy evaluation of variables value
assign value when it needs

#special value invaild & not found
>invaild: when a variable is created but no assigned yet
>not found: read an arg which is not existed

>use lua to distinguish
if ngx.var.arg_name == nil then
     ngx.say(“name: missing”)