Regex pattern in openresty
2019獨角獸企業重金招聘Python工程師標準>>>
Special Escaping Sequences
NOTE Following the v0.9.17 release, this pitfall can be avoided by using the *_by_lua_block {} configuration directives.
- Openresty Version 1.9.7.1 upgraded Lua Nginx Module to 0.9.20.
- Openresty Version 1.9.3.2 upgraded Lua Nginx Module to 0.9.19.
- Openresty Version 1.9.3.1 upgraded Lua Nginx Module to 0.9.16.
- Openresty Version 1.7.10.2 upgraded Lua Nginx Module to 0.9.16.
- Openresty Version 1.7.10.1 upgraded Lua Nginx Module to 0.9.15.
- Openresty Version 1.7.7.2 upgraded Lua Nginx Module to 0.9.14.
- Openresty Version 1.7.7.1 upgraded Lua Nginx Module to 0.9.13.
So Openresty Version >=1.9.3.2 is different from <1.9.3.2 with the lua regex.
How to check nginx version and Lua version
Compile args
[root@slave2 conf]# /usr/local/openresty/bin/openresty -V nginx version: openresty/1.11.2.1 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-17) (GCC) built with OpenSSL 1.0.2h 3 May 2016 (running with OpenSSL 1.0.2j 26 Sep 2016) TLS SNI support enabled configure arguments: --prefix=/usr/local/openresty/nginx --with-cc-opt='-O2 -I/usr/local/openresty/zlib/include -I/usr/local/openresty/pcre/include -I/usr/local/openresty/openssl/include' --add-module=../ngx_devel_kit-0.3.0 --add-module=../echo-nginx-module-0.60 --add-module=../xss-nginx-module-0.05 --add-module=../ngx_coolkit-0.2rc3 --add-module=../set-misc-nginx-module-0.31 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.06 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.6 --add-module=../ngx_lua_upstream-0.06 --add-module=../headers-more-nginx-module-0.31 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.17 --add-module=../redis2-nginx-module-0.13 --add-module=../redis-nginx-module-0.3.7 --with-ld-opt='-Wl,-rpath,/usr/local/openresty/luajit/lib -L/usr/local/openresty/zlib/lib -L/usr/local/openresty/pcre/lib -L/usr/local/openresty/openssl/lib -Wl,-rpath,/usr/local/openresty/zlib/lib:/usr/local/openresty/pcre/lib:/usr/local/openresty/openssl/lib' --with-pcre-jit --with-ipv6 --with-stream --with-stream_ssl_module --with-http_v2_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_auth_request_module --with-http_secure_link_module --with-http_random_index_module --with-http_gzip_static_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-threads --with-file-aio --with-dtrace-probes --with-http_ssl_moduleNOTE the nginx version: openresty/1.11.2.1 and config args ngx_lua-0.10.6
Source code module dir name
if you use the source code install openresty, the bundle dir show you the add module version
[root@slave2 bundle]# pwd /root/ngx_openresty-1.9.3.2/bundle [root@slave2 bundle]# ll|grep ngx_lua drwxrwxr-x 9 1000 1000 4096 Nov 11 2015 ngx_lua-0.9.19 drwxrwxr-x 5 1000 1000 4096 Oct 28 2015 ngx_lua_upstream-0.04Lua function
location / {default_type 'text/plain';content_by_lua_block {ngx.log(ngx.ERR, "Nginx version is :", ngx.config.nginx_version)ngx.log(ngx.ERR, "Lua-nginx-module version is :", ngx.config.ngx_lua_version)} }Note: *_by_lua_block implemented since Openresty Version 1.9.3.2.If Openresty tip with you nginx: [emerg] unknown directive "content_by_lua_block",it's older. Early version use *_by_lua
location / {content_by_lua 'ngx.log(ngx.ERR, "Nginx version is :", ngx.config.nginx_version)ngx.log(ngx.ERR, "Lua-nginx-module version is :", ngx.config.ngx_lua_version)'; }log:
2017/12/12 15:29:50 [error] 27781#0: *38 [lua] content_by_lua(nginx.conf:44):2: Nginx version is :1009003, client: 172.19.23.59, server: 172.19.23.208, request: "GET / HTTP/1.1", host: "172.19.23.208:808" 2017/12/12 15:29:50 [error] 27781#0: *38 [lua] content_by_lua(nginx.conf:44):3: Lua-nginx-module version is :9016, client: 172.19.23.59, server: 172.19.23.208, request: "GET / HTTP/1.1", host: "172.19.23.208:808"openresty is 1.9.3 /lua-nginx-module is 0.9.16
Example
What's the different of openresty version with *_by_lua directives
openresty-1.9.3.1:
content_by_lua '-- local regex = "\\\\d+" -- because lua code in the nginx config file ,so nginx first -->\\d+ ,then lua -->\d+.-- equal-- local regex = [[\\d+]] -- [[]] just like python """ """-- equallocal regex = [=[[0-9]+]=] -- if [] in regex ,use [=[...]=]local m = ngx.re.match("hello, 1234", regex)ngx.say(string.format("Nginx_version_is:%s Lua_nginx_module_version_is:%s Matched_result:%s", ngx.config.nginx_version,ngx.config.ngx_lua_version,m[0]))';OUT_PUT:
Nginx_version_is:1009003 Lua_nginx_module_version_is:9016 Matched_result:1234openresty-1.9.3.2:
content_by_lua '-- local regex = "\\\\d+" -- because lua code in the nginx config file ,so nginx first -->\\d+ ,then lua -->\d+.-- equal-- local regex = [[\\d+]] -- [[]] just like python """ """-- equallocal regex = [=[[0-9]+]=] -- if [] in regex ,use [=[...]=]local m = ngx.re.match("hello, 1234", regex)if m then result=m[0] else result="not matched!" endngx.say(string.format("Nginx_version_is:%s Lua_nginx_module_version_is:%s Matched_result:%s", ngx.config.nginx_version,ngx.config.ngx_lua_version,result)) ';OUT_PUT:
Nginx_version_is:1009003 Lua_nginx_module_version_is:9019 Matched_result:1234What's the difference between *_by_lua and *_by_lua_block with same openresty version
Note early version not has *_by_lua_block
openresty-1.9.3.2:
content_by_lua_block {-- local regex = "\\\\d+" -- output: not matched!local regex = "\\d+" -- output: 1234-- local regex = [[\\d+]] -- [[]] just like python """ """. output: not matched!-- local regex = [[\d+]] -- [[]] just like python """ """. output: 1234-- local regex = [=[[0-9]+]=] -- if [] in regex ,use [=[...]=] output: 1234local m = ngx.re.match("hello, 1234", regex)if m then result=m[0] else result="not matched!" endngx.say(string.format("Nginx_version_is:%s Lua_nginx_module_version_is:%s Matched_result:%s", ngx.config.nginx_version,ngx.config.ngx_lua_version,result))}
What's the difference in lua file
openresty-1.9.3.2:
[root@slave2 conf]# more test.lua local regex = "\\d+" -- output: 123 -- local regex = [[\d+]] -- [[]] just like python """ """. output: 1234 -- local regex = [=[[0-9]+]=] -- if [] in regex ,use [=[...]=] output: 1234local m = ngx.re.match("hello, 1234", regex) if m then result=m[0] else result="not matched!" end ngx.say(string.format("Nginx_version_is:%s Lua_nginx_module_version_is:%s Matched_result:%s", ngx.config.nginx_version,ngx.config.ngx_lua_version,result))openresty-1.9.3.1:
[root@slave2 conf]# more test.lua local regex = "\\d+" -- output: 123 -- local regex = [[\d+]] -- [[]] just like python """ """. output: 1234 -- local regex = [=[[0-9]+]=] -- if [] in regex ,use [=[...]=] output: 1234local m = ngx.re.match("hello, 1234", regex) if m then result=m[0] else result="not matched!" end ngx.say(string.format("Nginx_version_is:%s Lua_nginx_module_version_is:%s Matched_result:%s", ngx.config.nginx_version,ngx.config.ngx_lua_version,result))##Thinking lua string define ,python string
local regex="\\d+" --> regex="\\d+" local regex=[[\d+]] ==> regex="""\d+"""##Conclusion
- If you can always use lua code inside lua file ,it's the best practice.
- If your Openresty worked for later version (>=1.9.3.2,or lua-nginx-module>=.0.9.17) Try to use *_by_lua_block as much as possible, because it is same to the lua's file.
- When you have trouble ,you don't want to check then version's influence ,you can use no blackslash.for example: \d --> [0-9] ;\w --> [a-zA-z] and use [=[]=].But is not everthing.
轉載于:https://my.oschina.net/monkeyzhu/blog/1603837
總結
以上是生活随笔為你收集整理的Regex pattern in openresty的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IIS服务中五种身份验证的灵活运用-转
- 下一篇: 1月8日学习内容整理:JS的作用域和作用