openresty development series 33--2 rewrite the assignment phase of the implementation process of openresty

openresty development series 33--2 rewriting the assignment process executed openresty stage

a) Phase assignment rewritable

1) set_by_lua

Syntax: set_by_lua $ res <lua-script -str> [$ arg1 $ arg2 ...]
Context: server, server if , location, location if
stage: rewrite

setting nginx variables, set of instructions that we use even if the instruction is difficult to achieve with the assignment logic responsible;

parameter passed to the specified execution lua script code and get the return value to the res.
<lua-script-str> The code that takes the input parameters from ngx.arg table (index order starting from 1).

This directive is to perform short-term, fast running code because event processing loop during operation nginx is in a blocked state.
Time-consuming code should be avoided.
Prohibited at this stage following the API:
. 1, Output API (ngx.say and ngx.send_headers);
2, Control API (ngx.exit);
. 3, subrequest API (ngx.location.capture and ngx.location.capture_multi) ;
. 4, cosocket API (ngx.socket.tcp and ngx.req.socket);
. 5, SLEEP API (ngx.sleep)

A, nginx.conf profile

location / lua {
    $ Jump the SET "1";
    echo $ Jump;
}

the SET command to assign variables, but some scenes assignment business is more complex, the need to use lua script
so used set_by_lua

b, additional knowledge point:

ngx.var.arg and ngx .req.get_uri_args distinction, are capable of acquiring request parameters

ngx.var.arg_xx and ngx.req.get_uri_args [ "xx"] for both the acquisition request uri parameters
e.g. http://pureage.info?strider = 1
to obtain the input parameters Strider, the following two methods are:

local Strider = ngx.var.arg_strider

local Strider ngx.req.get_uri_args = [ "Strider"]

the difference is that, when a plurality of the same name in the request uri parameters, ngx.var.arg_xx approach is to take a first value appears
ngx.req_get_uri_args [ "xx"] is to return to a table, the table is stored in all of the values of the parameters

, for example, when the request for the uri: http: / /pureage.info?strider=1&strider=2&strider=3&strider=4 when

ngx.var.arg_strider is "1", and ngx.req.get_uri_args [ "strider"] Table value [ "1", "2", "3", "4"]。
Therefore, ngx.req.get_uri_args belong ngx.var.arg_ enhanced.

-------------------------------
Case Requirements: Bookstore reclaim skuid before for eight commodities, to a previous request page, for the nine requests to the new page
of the book product details page has been transformed, beautify a bit; the line on time, do not suddenly switch landscaping concept AB page ;; do
the goods enter the new book of new goods details page, before maintenance book product details page with the old page

previous id book for eight new book id is 9

    root / the Data / the WWW / HTML;

        LOCATION / book {

                set_by_lua $ to_type '
                        local skuid = NGX .var.arg_skuid
                        ngx.log (ngx.ERR, "skuid =", skuid)
                        local = R & lt ngx.re.match (skuid, "^ [0-9] {} $. 8")
                        local K = ngx.re. match (skuid, "^ [0-9] {} $. 9")
                        IF the then R & lt
                                return ". 1"

                        the then K IF
                                return "2"
                        End;
                ';

                IF (to_type $ = ". 1") {
                        echo "skuid 8-bit";
                        proxy_pass http://127.0.0.1/old_book/$arg_skuid.html;
                }
                IF ($ to_type = "2") {
                        echo "skuid as 9";
                        proxy_pass http://127.0.0.1/new_book/$arg_skuid.html;
                }

        }


# specific web content
[root @ node5 data] # tree / data / www / HTML /
/ Data / WWW / HTML /
├── new_book
│ └── 123456789.html
└── old_book
    12345678.html └──

2 Directories, 2 Files
[@ Node5 the root Data] /data/www/html/old_book/12345678.html CAT #
<h1 of> Old Book </ h1 of>
[@ Node5 the root Data] # CAT / Data /www/html/new_book/123456789.html
<h1> new new </ h1> Book


# test visit: http:? //10.11.0.215/book skuid = 123456789
will jump to new_book / 123456789.html

visit: http: / /10.11.0.215/book?skuid=12345678
will jump to new_book / 12345678.html

=============================== ========

2) set_by_lua_file

grammar set_by_lua_file $ var lua_file arg1 arg2 ...;

in lua code can achieve all complex logic, but to perform fast, do not block;

LOCATION / lua_set_1 {
    default_type "text / HTML ";
    set_by_lua_file $ NUM /usr/local/luajit/test_set_1.lua;
    echo $ NUM;
}

2.1, test_set_1.lua

local uri_args = ngx.req.get_uri_args ()
local uri_args I = [ "I"] or 0
local uri_args J = [ "J"] or 0

return I + J

to obtain parameters addition request and then returns.

As http://192.168.31.138/lua_set_1?i=1&j=10 access for testing. If we use pure set of instructions it can not be achieved.

3) Note that this command can only be written once nginx a variable, but the interface may be used to solve this problem ngx.var:

LOCATION / foo {
    SET the diff $ '';
    set_by_lua $ SUM '
        local = 32 A
        local B = 56 is
        NGX. var.diff = a - b; - written in the diff $
        return a + b; - is returned to the $ sum
    ';
    echo "SUM = $ sum, the diff the diff $ =";
}

Guess you like

Origin www.cnblogs.com/reblue520/p/11446354.html