共计 3581 个字符,预计需要花费 9 分钟才能阅读完成。

此处记录下openresty中可用的时间方法
ngx时间方法
ngx有以下几个时间方法
- ngx.today():从 nginx 缓存里获取 yyyy-mm-dd 格式的时间,没有额外的系统调用
- ngx.time():从 nginx 缓存里获取时间戳
- ngx.now():从 nginx 缓存里获取时间戳,小数位为毫秒,格式为 1533134658.555
- ngx.update_time():要 nginx 更新缓存时间,会造成系统调用
- ngx.localtime():从 nginx 缓存里获取本地时间格式为 yyyy-mm-dd hh:mm:ss
- ngx.cookie_time():从 nginx 缓存里获取 UTC 时间格式为 yyyy-mm-dd hh:mm:ss
- ngx.http_time():转化时间戳为 cookie 的时间格式
- ngx.parse_http_time():转化时间戳为 http头使用的时间格式,比如 Last-Modified
一个获取时间样例
location = /getTime {
content_by_lua_block {
ngx.say(ngx.today())
ngx.say(ngx.time())
ngx.say(ngx.now())
ngx.say(ngx.localtime())
ngx.say(ngx.utctime())
ngx.say(ngx.cookie_time(ngx.time()))
ngx.say(ngx.http_time(ngx.time()))
ngx.say(ngx.parse_http_time("Thu, 18 Nov 2019 11:27:35 GMT"))
}
}
输出如下
[root@nginx-cluster conf.d]# curl 127.0.0.1:8084/getTime
2021-06-09
1623233087
1623233087.354
2021-06-09 18:04:47
2021-06-09 10:04:47
Wed, 09-Jun-21 10:04:47 GMT
Wed, 09 Jun 2021 10:04:47 GMT
1290079655
接口时段控制
放行工作日内调用接口
时段控制module
博主已将基本逻辑提取为module:comm.time_acl
[root@nginx-cluster lua]# pwd
/usr/local/openresty/nginx/lua
[root@nginx-cluster lua]# cat comm/
param.lua time_acl.lua
[root@nginx-cluster lua]# cat comm/time_acl.lua
local _M = {}
function _M.access_week(acl,week)
local week_tbl = {
Mon = 1,
Tus = 2,
Wen = 3,
Thu = 4,
Fri = 5,
Sta = 6,
Sun = 7
}
for _,v in ipairs(acl) do
if week_tbl[week] == v then
return true
end
end
return false
end
return _M
access阶段处理lua文件
[root@nginx-cluster lua]# pwd
/usr/local/openresty/nginx/lua
[root@nginx-cluster lua]# cat time_acl_check.lua
local time_acl= require("comm.time_acl")
--- 在table中存储星期策略,周一为{1},周一到周三{1,2,3}
local week_acl = {1,3,5,7}
--- 获取上海时间字符串格式:Wed, 09 Jun 2021 10:04:47 GMT
local time_str = ngx.http_time(ngx.time()+28800)
local week,day,month,year,hour,min,sec=string.match(time_str,"(%w+), (%d+) (%w+) (%d+) (%d+):(%d+):(%d+)")
if not(time_acl.access_week(week_acl, week)) then
ngx.log(ngx.INFO, "time_str: ", time_str, " deny by week_acl")
return ngx.exit(403)
end
location部分
location ~ ^/api/admin {
access_by_lua_file lua/time_acl_check.lua;
content_by_lua_block {
ngx.say("Allow Operating")
}
}
测试验证
测试正常时段输出
[root@nginx-cluster lua]# curl '127.0.0.1:8084/api/admin'
Allow operating
测试禁止时段
今日周三,将acl table中3去除验证,读者自行处理
[root@nginx-cluster conf.d]# curl 127.0.0.1:8084/api/admin -I
HTTP/1.1 403 Forbidden
Server: openresty/1.17.8.2
Date: Wen, 09 Jul 2021 08:37:36 GMT
Content-Type: text/html
Content-Length: 159
Connection: keep-alive
# 查看日志
[root@nginx-cluster lua]# tail ../logs/error.log -f
2021/06/09 08:37:36 [info] 11038#0: *1 [lua] time_acl_check.lua:7: time_str: Sun, 09 Jul 2021 16:37:36 GMT, client: 127.0.0.1, server: , request: "GET /api/admin HTTP/1.1", host: "127.0.0.1:8084"
接口增加小时hour限制
此时模块内容
[root@nginx-cluster lua]# cat comm/time_acl.lua
local _M = {}
function _M.access_week(acl,week)
local week_tbl = {
Mon = 1,
Tus = 2,
Wen = 3,
Thu = 4,
Fri = 5,
Sta = 6,
Sun = 7
}
for _,v in ipairs(acl) do
if week_tbl[week] == v then
return true
end
end
return false
end
function _M.access_hour(acl, hour)
local hour_tbl = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24}
hour = 0 == hour and 24 or hour
for _,v in ipairs(acl) do
local ret = 0 == v and 24 or v
if hour_tbl[hour] == ret then
return true
end
end
return false
end
return _M
access阶段处理lua文件
[root@nginx-cluster lua]# cat time_acl_check.lua
local time_acl= require("comm.time_acl")
--- 在table中存储星期策略,周一为{1},周一到周三{1,2,3}
local week_acl = {1,3,5,7}
--- 在table中存储小时策略,0点为{0},0点到5点{0,1,2,3,4,5}
local hour_acl = {9,10,11,14,15,16,17,18,19,20}
--- 获取上海时间字符串格式:Wed, 09 Jun 2021 10:04:47 GMT
local time_str = ngx.http_time(ngx.time()+28800)
local week,day,month,year,hour,min,sec=string.match(time_str,"(%w+), (%d+) (%w+) (%d+) (%d+):(%d+):(%d+)")
if not(time_acl.access_week(week_acl, week)) then
ngx.log(ngx.INFO, "time_str: ", time_str, " deny by week_acl")
return ngx.exit(403)
end
if not(time_acl.access_hour(hour_acl, tonumber(hour))) then
ngx.log(ngx.INFO, "time_str: ", time_str, " deny by hour_acl")
return ngx.exit(403)
end
正文完
隐私政策
留言板
金色传说
kubernetes
terraform
云生原
helm
代码编程
Java
Python
Shell
DevOps
Ansible
Gitlab
Jenkins
运维
老司机
Linux 杂锦
Nginx
数据库
elasticsearch
监控
上帝视角
DJI FPV
DJI mini 3 pro
关于本站