openresty开发系列32--openresty执行流程之1初始化阶段
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初始化阶段的更多相关文章
- openresty开发系列35--openresty执行流程之5内容content阶段
openresty开发系列35--openresty执行流程之5内容content阶段 content 阶段 ---init阶段---重写赋值---重写rewrite---access content ...
- openresty开发系列33--openresty执行流程之2重写赋值阶段
openresty开发系列33--openresty执行流程之2重写赋值阶段 一)重写赋值阶段 1)set_by_lua 语法:set_by_lua $res <lua-script-str&g ...
- openresty开发系列36--openresty执行流程之6日志模块处理阶段
openresty开发系列36--openresty执行流程之6日志模块处理阶段 一)header_filter_by_lua 语法:header_filter_by_lua <lua-scri ...
- openresty开发系列34--openresty执行流程之4访问阶段
openresty开发系列34--openresty执行流程之4访问阶段 访问阶段 用途:访问权限限制 返回403 nginx:allow 允许,deny 禁止 allow ip:deny ip: 涉 ...
- openresty开发系列33--openresty执行流程之3重写rewrite和重定向
openresty开发系列33--openresty执行流程之3重写rewrite和重定向 重写rewrite阶段 1)重定向2)内部,伪静态 先介绍一下if,rewrite指令 一)if指令语法:i ...
- openresty开发系列31--openresty执行流程
openresty开发系列31--openresty执行流程 我们先看个例子 location /test { set $a 32; echo $a; set $a 56; e ...
- openresty开发系列28--openresty中操作mysql
openresty开发系列28--openresty中操作mysql Mysql客户端 应用中最常使用的就是数据库了,尤其mysql数据库,那openresty lua如何操作mysql呢? ...
- openresty开发系列24--openresty中lua的引入及使用
openresty开发系列24--openresty中lua的引入及使用 openresty 引入 lua 一)openresty中nginx引入lua方式 1)xxx_by_lua ---> ...
- openresty开发系列40--nginx+lua实现获取客户端ip所在的国家信息
openresty开发系列40--nginx+lua实现获取客户端ip所在的国家信息 为了实现业务系统针对不同地区IP访问,展示包含不同地区信息的业务交互界面.很多情况下系统需要根据用户访问的IP信息 ...
随机推荐
- 有趣for循环
String fileValue = "2;3;4;5;6;"; String[] arry = fileValue.split(";"); for (int ...
- 面试中的nginx高可用高并发!
本文转自:91博客:原文地址:http://www.9191boke.com/439923471.html 面试题: nginx高可用?nginx 是如何实现并发的?为什么nginx不使用多线程?ng ...
- 数据库开发-pymysql详解
数据库开发-pymysql详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Python支持的MySQL驱动 1>.什么是驱动 与MySQL通信就是典型的CS模式.Se ...
- 使用kubeadm搭建高可用k8s v1.16.3集群
目录 1.部署环境说明 2.集群架构及部署准备工作 2.1.集群架构说明 2.2.修改hosts及hostname 2.3.其他准备 3.部署keepalived 3.1.安装 3.2.配置 3.3. ...
- HTML常用全部代码--第一部分--HTML/CSS( 小伙伴要牢记😁😁😁😁 )
<一>html代码大全:结构性定义 (1) 文件类型<HTML></HTML> (放在档案的开头与结尾) (2) 文件主题<TITLE></TIT ...
- 由Catalan数所引出的
百度一番: 历史 ·1758年,Johann Segner 给出了欧拉问题的递推关系: ·1838年,研究热潮: –GabrielLame给出完整证明和简洁表达式: –EugèneCharlesCat ...
- KVM-virsh常用命令
virsh list #在线VM virsh list --all #所有VM virsh start #开机 virsh shutdown #软关机 virsh destroy #强制关机 virs ...
- WIFI信道频率对应
802.11b/g还是802.11b/g/n 一般都支持13个信道 信道也称作通道(Channel).频段,是以无线信号(电磁波)作为传输载体的数据信号传送通道.无线网络(路由器.AP热点.电脑无线网 ...
- Echo团队Alpha冲刺 - 测试随笔
目录 测试工作的安排 测试工具选择和运用 测试用例文档 测试体会 项目测试评述 测试工作的安排 模块 测试人 测试内容 单元测试 李东权,黄少勇 测试类或者函数是否能正确处理用户请求 接口测试 林弘杰 ...
- width: calc(100% - 80px); 屏幕自适应方法
width: calc(100% - 80px); 屏幕自适应方法