Lua 自带的正则表达式确实有点另类,不支持 “|” 而且正则语法不好适应,想用Nginx原生的正则表达式就需要在Lua中借助Nginx内置对象 ngx.re
正则在 nginx.conf 文件中错误的使用方法:
# nginx.conf ? location /test { ? content_by_lua ' ? local regex = "\d+" -- THIS IS WRONG!! ? local m = ngx.re.match("hello, 1234", regex) ? if m then ngx.say(m[0]) else ngx.say("not matched!") end ? '; ? } # evaluates to "not matched!"
nginx.conf 文件中正确使用方法 记得是\\\\才能转义
# nginx.conf location /test { content_by_lua ' local regex = "\\\\d+" local m = ngx.re.match("hello, 1234", regex) if m then ngx.say(m[0]) else ngx.say("not matched!") end '; } # evaluates to "1234"
可以使用Lua中特有的 [[ 正则 ]] 定义符号
# nginx.conf location /test { content_by_lua ' local regex = [[\\d+]] local m = ngx.re.match("hello, 1234", regex) if m then ngx.say(m[0]) else ngx.say("not matched!") end '; } # evaluates to "1234"
这个自己翻译了
Here, [[\\d+]]
is stripped down to [[\d+]]
by the Nginx config file parser and this is processed correctly.
Note that a longer from of the long bracket, [=[...]=]
, may be required if the regex pattern contains [...]
sequences. The [=[...]=]
form may be used as the default form if desired.
# nginx.conf location /test { content_by_lua ' local regex = [=[[0-9]+]=] local m = ngx.re.match("hello, 1234", regex) if m then ngx.say(m[0]) else ngx.say("not matched!") end '; } # evaluates to "1234"
如果采用 content_by_lua_file 'test.lua' \\\\d+ 简化为 \\d+
-- test.lua local regex = "\\d+" local m = ngx.re.match("hello, 1234", regex) if m then ngx.say(m[0]) else ngx.say("not matched!") end -- evaluates to "1234"
如果采用 content_by_lua_file 'test.lua' [[\\d+]] 简化为 [[\d+]]
-- test.lua local regex = [[\d+]] local m = ngx.re.match("hello, 1234", regex) if m then ngx.say(m[0]) else ngx.say("not matched!") end -- evaluates to "1234"
来源:http://wiki.nginx.org/HttpLuaModule
相关推荐
idea+openresty+lua运行nginx服务器, 代码和软件都有, 参考资料: https://blog.csdn.net/dongyuxu342719/article/details/90413904
OpenResty 是一个强大的 Web 应用服务器,Web 开发人员可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,更主要的是在性能方面,OpenResty可以 快速构造出足以胜任 10K 以上并发连接响应的超高性能 Web ...
OpenResty lua redis 实现
test-nginx, 面向 Nginx C 模块和 OpenResty Lua库开发的数据驱动测试 电子邮件名称Test::Nginx - Nginx MODULE 和 Nginx/openresty库和应用程序的数据驱动测试脚手架 table-内容NAME描述用户指南使用 Test::Nginx ...
nginx openresty lua luaxml
此模块是基于openresty开发(或者nginx安装了ngx_lua_module模块, 建议使用openresty)
主要介绍了openresty中使用lua-nginx创建socket实例,本文直接给出代码实例和运行效果,需要的朋友可以参考下
但由于项目需要在Windows上使用nginx,无奈只好自己去找资料,在Windows上编译nginx-openresty同时加入rtmp模块。本资源是Windows上生成好的openresty-1.19.3.1+nginx-rtmp-module的包,同时包含win32、win64版本。
Nginx模块开发OpenResty简单使用笔记整理 ### Nginx简介 Nginx是当前最流行的HTTP Server之一,根据W3Techs的统计,目前世界排名(根据Alexa)前100万的网站中。与Apache相比。 同时,大量的第三方扩展模块也令...
nginx+lua+redis实现token验证。实现基本的token验证、反向代理转发内部服务,lua连接redis封装、lua域名解析封装、lua域名脚本等
这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。 OpenResty的目标是让你的Web服务直接...
nginx-tcp-lua服务器如何建造 1. download the special version from openresty....
缩略图OpenResty(Nginx)+Lua+GraphicsMagick
每个人想到的最好的地方就是嵌入在nginx中的Lua吗? 这个项目适合你。用法您可以通过运行openresty来测试示例: openresty -c `pwd`/config/nginx.conf然后通过curl发出请求: # valid JSONcurl -X POST -d '["bob...
使用openresty实现nginx+lua安装部署,并且使用dyups模块实现根据不同客户端请求跳转到不同服务集群。
lua-resty-limit-traffic, 在 openresty/ngx_lua中,用于限制和控制流量的Lua库 电子邮件名称lua-resty-limit-traffic - 用于限制和控制 openresty/ngx_lua中流量的Lua库目录名称状态概要说明描述安装工具社区服务...
resty-redis-cluster:Redis集群的Openresty lua客户端
通过概要,指令,api接口三方面阐述lua-nginx模块的使用。 使用Lua编写Nginx脚本的基本构建块是指令。指令用于指定何时运行用户Lua代码以及如何使用结果。 在nginx.conf文件中各种*_by_lua,*_by_lua_block和*_by_...
nginx+lua+redis 集群 连接插件和脚本,原来插件是没有密码功能 故 修改了一下 k可以自己下载使用
ngx_stream_lua_module-将Lua的功能嵌入到Nginx流/ TCP服务器中。 该模块是OpenResty的核心组件。 如果您使用的是此模块,则实际上是在使用OpenResty。 该模块不随Nginx源一起分发。 请参阅。 目录 代码库 错误和...