当前位置:网站首页>openresty ngx_lua執行階段

openresty ngx_lua執行階段

2022-07-05 02:17:00 o_瓜田李下_o


openresty ngx_lua執行階段

       

                

                                

nginx http執行階段

          

nginx http執行階段

# ngx_http_core_module.h
typedef enum {
    NGX_HTTP_POST_READ_PHASE = 0,

    NGX_HTTP_SERVER_REWRITE_PHASE,

    NGX_HTTP_FIND_CONFIG_PHASE,
    NGX_HTTP_REWRITE_PHASE,
    NGX_HTTP_POST_REWRITE_PHASE,

    NGX_HTTP_PREACCESS_PHASE,

    NGX_HTTP_ACCESS_PHASE,
    NGX_HTTP_POST_ACCESS_PHASE,

    NGX_HTTP_PRECONTENT_PHASE,

    NGX_HTTP_CONTENT_PHASE,

    NGX_HTTP_LOG_PHASE
} ngx_http_phases;

          

執行階段說明

NGX_HTTP_POST_READ_PHASE = 0,
* 接受並讀取請求

NGX_HTTP_SERVER_REWRITE_PHASE,
* 修改url階段,通常有重定向、變量設置

NGX_HTTP_FIND_CONFIG_PHASE,
* 查找url對應的配置,如匹配location

NGX_HTTP_REWRITE_PHASE,
* 匹配到location後,可能會再次進入NGX_HTTP_SERVER_REWRITE_PHASE

NGX_HTTP_POST_REWRITE_PHASE,
* 檢查url是否執行過階段4,如果執行了,再次進入階段3
* 最大檢查次數為10,超過會報錯

NGX_HTTP_PREACCESS_PHASE,
* 請求資源限速等操作

NGX_HTTP_ACCESS_PHASE,
* 訪問控制,如限制ip訪問

NGX_HTTP_POST_ACCESS_PHASE,
* 驗證NGX_HTTP_ACCESS_PHASE(訪問控制)結果,如果通過則繼續

NGX_HTTP_PRECONTENT_PHASE,
* try_files指令時生效

NGX_HTTP_CONTENT_PHASE,
* 處理http請求內容,一般需要與後端應用進行交互

NGX_HTTP_LOG_PHASE
* 日志輸出

        

             

                                

ngx_lua 執行階段

         

              

        

執行順序

init_by_lua_block
init_worker_by_lua_block

set_by_lua_block
rewrite_by_lua_block
access_by_lua_block
content_by_lua_block

header_filter_by_lua_block
body_filter_by_lua_block
log_filter_by_lua_block

            

init_by_lua_block:nginx初始化、重啟時執行,在http模塊中設置

http {
    include       mime.types;
    default_type  application/octet-stream;
 
    init_by_lua_block {
        local uuid = require 'resty.jit-uuid';
        uuid.seed();
    } 

    ...

}

          

init_by_lua_file:作用與init_by_lua_block相同,代碼塊保存在文件中

http {
    include       mime.types;
    default_type  application/octet-stream;
 
    init_worker_by_lua_file /usr/local/nginx/conf/init.lua

    ...

}


# /usr/local/nginx/conf/init.lua 文件內容
local uuid = require 'resty.jit-uuid';
uuid.seed();

           

init_worker_by_lua_block:master進程啟動後,執行相關代碼塊,在http模塊中設置

# 可用來執行定時任務、後端健康檢查
http {
    include       mime.types;
    default_type  application/octet-stream;
 
    init_worker_by_lua_block {
        ...
    } 

    ...

}

              

set_by_lua_block:執行代碼塊,並將結果返回,在server、location語句塊中設置

語法格式:set_by_lua_block $result {...}
* 該命令是阻塞命令,執行的語句塊盡可能短、快,避免耗時過多
* 語句塊中禁用:ngx.say()、ngx.exit()、ngx.sleep()等命令

server {
    listen       80;
    server_name  localhost; 

    set $a "";
    set_by_lua_block $a {
        return "1";
    }


    location / {
        root   /usr/local/openresty/nginx/html;
        index  index.html index.htm;
    }

    ...

 }

              

rewrite_by_lua_block:重寫階段執行,可在http、server、location中設置

語法格式:rewrite_by_lua_block {...}
* 語句塊會在最後執行,可通過rewrite_by_lua_no_postpone(默認off)改變執行順序
* 可調用所有ngx api

# a的值為2
server {
    listen       80;
    server_name  localhost; 

    set $a "1";
    rewrite_by_lua_block {
        ngx_var_a="2";
    }
    set $a "3"


    location / {
        root   /usr/local/openresty/nginx/html;
        index  index.html index.htm;
    }

    ...

 }


# http階段配置:rewrite_by_lua_no_postpone on
# a的值為3
server {
    listen       80;
    server_name  localhost; 

    set $a "1";
    rewrite_by_lua_block {
        ngx_var_a="2";
    }
    set $a "3"


    location / {
        root   /usr/local/openresty/nginx/html;
        index  index.html index.htm;
    }

    ...

 }

             

access_by_lua_block:access階段執行語句塊(權限檢查、黑白名單),在http、server、location中設置

語法格式:access_by_lua_block {...}
* 語句塊會在最後執行,可通過access_by_lua_no_postpone(默認off)改變執行順序
* 可調用所有ngx api
* 應用:將黑白名單存放在redis、共享內存中,動態設置黑白名單

         

content_by_lua_block:內容處理階段執行,設置輸出內容,在location中設置

語法格式:content_by_lua_block {...}
* 可調用所有ngx api
* 若與其他內容執行階段語句同時使用,content_by_lua_block可能不會執行

location / {

    content_by_lua_block {
        ngx.say("hello gtlx");
    }
    
    echo "test"    #只會輸出test,不會輸出hello gtlx
}

               

content_by_lua_file:與content_by_lua_block相同,語句保存在文件中,在location中設置

語法格式:content_by_lua_file file
* 可調用所有ngx api

location / {

    content_by_lua_file /usr/local/openresty/file/test.lua
    
}


# 文件:/usr/local/openresty/file/test.lua
hello gtlx

            

balancer_by_lua_block:內容處理階段執行,在upstream語句中使用

語法格式:balancer_by_lua_block {...}
* upstream使用的ip為balancer_by_lua_block中設置的地址
* 禁用的api:cosockets、light threads


upstream web-server {
    server  172.18.0.2   #會忽略server設置的ip地址

    balancer_by_lua_block {
        ...              #真是使用的ip地址
    }
}

                  

header_filter_by_lua_block:響應階段添加、删除響應頭,在http、server、location中設置

語法格式:header_filter_by_lua_block {...}
* 禁用的api:ngx.say()、ngx.redirect()、ngx.exec()等

# 添加響應頭
location {
    header_filter_by_lua_block {
        ngx.header.test="hello gtlx"
    }

    echo "hello"
}

           

body_filter_by_lua_block:響應階段修改響應體,在http、server、location中設置

語法格式:body_filter_by_lua_block {...}
* 禁用的api:ngx.say()、ngx.redirect()、ngx.exec()等

# 將相應數據轉換為大寫(ngx.arg[1]獲取響應數據,ngx.arg[2]響應結束符)
# 輸出:HELLO
location {
    body_filter_by_lua_block {
        ngx.arg[1] = string.upper(ngx.arg[1]);
    }

    echo "hello"
}

        

log_by_lua_block:輸出日志,可在http、server、location中設置

語法格式:log_filter_by_lua_block {...}
* 在access_log之前執行,不會替換當前的access_log日志
* 禁用的api:ngx.say()、ngx.redirect()、ngx.exec()等

location {
    log_filter_by_lua_block {
        ngx.log("hello gtlx")
    }

    echo "hello"
}

            

                   

原网站

版权声明
本文为[o_瓜田李下_o]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/07/202207050217063466.html