openresty开发系列32--openresty执行流程之初始化阶段

一)初始化阶段

1)init_by_lua   init_by_lua_block     init_by_lua_file
语法:init_by_lua <lua-script-str>
语境:http
阶段:loading-config
当nginx master进程在加载nginx配置文件时运行指定的lua脚本,
通常用来注册lua的全局变量或在服务器启动时预加载lua模块:

[root@node5 conf]# cat nginx.conf
worker_processes  4;

error_log logs/error.log;
error_log logs/debug.log debug;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    #default_type  application/octet-stream;
    default_type  text/html;
    charset utf-8;

sendfile        on;
    # 关闭lua缓存,不需要每次重启才生效,会牺牲一定性能
    lua_code_cache on;

lua_shared_dict shared_data 10m;
    keepalive_timeout  65;

init_by_lua_block {
    cjson = require "cjson"
    }

server {
        listen       80;
        server_name  www.server1.com;
        resolver 8.8.8.8;
        lua_ssl_verify_depth 2;
    lua_ssl_trusted_certificate "/etc/ssl/certs/ca-bundle.crt";

location = /api {
        content_by_lua_block {
            ngx.say(cjson.encode({dog = 5, cat = 6}))
        }
    }

location / {
        root html;
        index index.html;
    }

error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

}

}

从这段配置代码,我们可以看出,其实这个指令就是初始化一些lua的全局变量,以便后续的代码使用。

初始化lua_shared_dict共享数据:

lua_shared_dict dogs 1m;

init_by_lua_block {
    local dogs = ngx.shared.dogs;

dogs:set("Tom", 50)
    dogs:set("flag",1)
}

server {
    location = /api {
        content_by_lua_block {
            local dogs = ngx.shared.dogs;
            ngx.say(dogs:get("Tom"))
        }
    }
}

lua_shared_dict的内容不会在nginx reload时被清除。所以如果你不想在init_by_lua中重复初始化共享数据,
那么你需要在你的共享内存中设置一个标志位并在init_by_lua中进行检查。

因为这个阶段的lua代码是在nginx forks出任何worker进程之前运行,
数据和代码的加载将享受由操作系统提供的copy-on-write的特性,从而节约了大量的内存。
不要在这个阶段初始化你的私有lua全局变量,因为使用lua全局变量会照成性能损失,
并且可能导致全局命名空间被污染。
这个阶段只支持一些小的LUA Nginx API设置:ngx.log和print、ngx.shared.DICT;

2)init_worker_by_lua
语法:init_worker_by_lua <lua-script-str>
语境:http
阶段:starting-worker
在每个nginx worker进程启动时调用指定的lua代码。

用于启动一些定时任务,比如心跳检查,定时拉取服务器配置等等;此处的任务是跟Worker进程数量有关系的,
比如有2个Worker进程那么就会启动两个完全一样的定时任务。

a、nginx.conf配置文件中的http部分添加如下代码

init_worker_by_lua_file /usr/local/lua/init_worker.lua;

------------------------------------

b、/usr/local/lua/init_worker.lua;

local count = 0
local delayInSeconds = 3
local heartbeatCheck = nil

heartbeatCheck = function(args)
   count = count + 1
   ngx.log(ngx.ERR, "do check ", count)

local ok, err = ngx.timer.at(delayInSeconds, heartbeatCheck)

if not ok then
      ngx.log(ngx.ERR, "failed to startup heartbeart worker...", err)
   end
end

heartbeatCheck()

# 观察日志:
# tail logs/debug.log
...
2019/08/23 19:09:18 [error] 77617#0: *431 [lua] init_worker.lua:7: heartbeatcheck(): do check 1, context: init_worker_by_lua*
2019/08/23 19:09:18 [error] 77618#0: *432 [lua] init_worker.lua:7: heartbeatcheck(): do check 1, context: init_worker_by_lua*
2019/08/23 19:09:18 [error] 77619#0: *433 [lua] init_worker.lua:7: heartbeatcheck(): do check 1, context: init_worker_by_lua*
2019/08/23 19:09:18 [error] 77620#0: *434 [lua] init_worker.lua:7: heartbeatcheck(): do check 1, context: init_worker_by_lua*
2019/08/23 19:09:21 [error] 77620#0: *436 [lua] init_worker.lua:7: do check 2, context: ngx.timer
2019/08/23 19:09:21 [error] 77617#0: *438 [lua] init_worker.lua:7: do check 2, context: ngx.timer
2019/08/23 19:09:21 [error] 77618#0: *437 [lua] init_worker.lua:7: do check 2, context: ngx.timer
2019/08/23 19:09:21 [error] 77619#0: *435 [lua] init_worker.lua:7: do check 2, context: ngx.timer
2019/08/23 19:09:24 [error] 77619#0: *439 [lua] init_worker.lua:7: do check 3, context: ngx.timer
2019/08/23 19:09:24 [error] 77617#0: *442 [lua] init_worker.lua:7: do check 3, context: ngx.timer
2019/08/23 19:09:24 [error] 77620#0: *441 [lua] init_worker.lua:7: do check 3, context: ngx.timer
...

ngx.timer.at:延时调用相应的回调方法;ngx.timer.at(秒单位延时,回调函数,回调函数的参数列表);
可以将延时设置为0即得到一个立即执行的任务,任务不会在当前请求中执行不会阻塞当前请求,
而是在一个轻量级线程中执行。
 
另外根据实际情况设置如下指令
lua_max_pending_timers 1024;  #最大等待任务数
lua_max_running_timers 256;    #最大同时运行任务数

3)lua_package_path

语法:lua_package_path <lua-style-path-str>
默认:由lua的环境变量决定
适用上下文:http
设置lua代码的寻找目录。
例如:lua_package_path "/opt/nginx/conf/www/?.lua;;";

openresty开发系列32--openresty执行流程之1初始化阶段的更多相关文章

  1. openresty开发系列35--openresty执行流程之5内容content阶段

    openresty开发系列35--openresty执行流程之5内容content阶段 content 阶段 ---init阶段---重写赋值---重写rewrite---access content ...

  2. openresty开发系列33--openresty执行流程之2重写赋值阶段

    openresty开发系列33--openresty执行流程之2重写赋值阶段 一)重写赋值阶段 1)set_by_lua 语法:set_by_lua $res <lua-script-str&g ...

  3. openresty开发系列36--openresty执行流程之6日志模块处理阶段

    openresty开发系列36--openresty执行流程之6日志模块处理阶段 一)header_filter_by_lua 语法:header_filter_by_lua <lua-scri ...

  4. openresty开发系列34--openresty执行流程之4访问阶段

    openresty开发系列34--openresty执行流程之4访问阶段 访问阶段 用途:访问权限限制 返回403 nginx:allow 允许,deny 禁止 allow ip:deny ip: 涉 ...

  5. openresty开发系列33--openresty执行流程之3重写rewrite和重定向

    openresty开发系列33--openresty执行流程之3重写rewrite和重定向 重写rewrite阶段 1)重定向2)内部,伪静态 先介绍一下if,rewrite指令 一)if指令语法:i ...

  6. openresty开发系列31--openresty执行流程

    openresty开发系列31--openresty执行流程 我们先看个例子 location /test {    set $a 32;    echo $a;    set $a 56;    e ...

  7. openresty开发系列28--openresty中操作mysql

    openresty开发系列28--openresty中操作mysql Mysql客户端   应用中最常使用的就是数据库了,尤其mysql数据库,那openresty lua如何操作mysql呢?   ...

  8. openresty开发系列24--openresty中lua的引入及使用

    openresty开发系列24--openresty中lua的引入及使用 openresty 引入 lua 一)openresty中nginx引入lua方式 1)xxx_by_lua   ---> ...

  9. openresty开发系列40--nginx+lua实现获取客户端ip所在的国家信息

    openresty开发系列40--nginx+lua实现获取客户端ip所在的国家信息 为了实现业务系统针对不同地区IP访问,展示包含不同地区信息的业务交互界面.很多情况下系统需要根据用户访问的IP信息 ...

随机推荐

  1. mysql应用

    1.  简述 MySQL是开源的关系型数据库.官网:https://dev.mysql.com/. 2.  安装及应用 可通过https://dev.mysql.com/downloads/下载MyS ...

  2. 《linux就该这么学》课堂笔记02 虚拟机安装使用

    这节学习了虚拟机安装RHEL系统,了解了shell.以及命令的格式            

  3. FreeBSD安装后使用su命令显示sorry的解决办法

    FreeBSD中,可以使用su命令成为root用户,但FreeBSD对执行su命令的用户进行了更严格的限制,能使用su命令的用户必须属于wheel组(root的基本属组,组ID为0),否则就不能通过 ...

  4. Android init介绍(上)

    1. 介绍 init进程是Linux系统第一个用户进程,是Android系统应用程序的根进程,即1号进程(PID为1):Android中的init文件位于/init,代码位于system/core/i ...

  5. js Date对象和数字对象

    <script type="text/javascript"> alert(new Date.toLocaleString()); </script> 以本 ...

  6. 同步关键词synchronized

    概述 synchronized是java中的一个关键字,也就是说是Java语言内置的特性. synchronized( 一个任意的对象(锁) ){代码块中放操作共享数据的代码. } public sy ...

  7. php过滤敏感词

    <?php /**  * 敏感词过滤工具类  * 使用方法  * echo FilterTools::filterContent("你妈的我操一色狼杂种二山食物"," ...

  8. Comet OJ - Contest #14 转转的数据结构题 珂朵莉树+树状数组

    题目链接: 题意:有两个操作 操作1:给出n个操作,将区间为l到r的数字改为x 操作2:给出q个操作,输出进行了操作1中的第x到x+y-1操作后的结果 解法: 把询问离线,按照r从小到大排序 每次询问 ...

  9. [PWA] Storage information for PWA application

    Be careful with the storage use cases, free the storage when it is necessary.

  10. Bagging and Random Forest

    Bagging和随机森林RF. 随机森林是最受欢迎和最强大的机器学习算法之一.它是一种称为Bootstrap Aggregation或bagging的集成机器学习算法. bootstrap是一种强大的 ...