{"id":15401,"date":"2024-11-17T20:53:53","date_gmt":"2024-11-17T12:53:53","guid":{"rendered":"https:\/\/fwq.ai\/blog\/?p=15401"},"modified":"2024-11-17T20:53:53","modified_gmt":"2024-11-17T12:53:53","slug":"openresty%e4%b8%ad%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/15401\/","title":{"rendered":"openResty\u4e2d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4f7f\u7528"},"content":{"rendered":"<p>\u5728\u00a0<em>OpenResty<\/em>\u00a0\u4e2d\uff0c\u540c\u65f6\u5b58\u5728\u4e24\u5957\u6b63\u5219\u8868\u8fbe\u5f0f\u89c4\u8303\uff1a<em>Lua<\/em>\u00a0\u8bed\u8a00\u7684\u89c4\u8303\u548c\u00a0<em>Nginx<\/em>\u00a0\u7684\u89c4\u8303\uff1b\u5373\u4f7f\u60a8\u5bf9\u00a0<em>Lua<\/em>\u00a0\u8bed\u8a00\u4e2d\u7684\u89c4\u8303\u975e\u5e38\u719f\u6089\uff0c\u6211\u4eec\u4ecd\u4e0d\u5efa\u8bae\u4f7f\u7528\u00a0<em>Lua<\/em>\u00a0\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/p>\n<ol>\n<li>\u56e0\u4e3a\u00a0<em>Lua<\/em>\u00a0\u4e2d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6027\u80fd\u5e76\u4e0d\u5982\u00a0<em>Nginx<\/em>\u00a0\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u4f18\u79c0\uff1b<\/li>\n<li><em>Lua<\/em>\u00a0\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5e76\u4e0d\u7b26\u5408\u00a0<em>POSIX<\/em>\u00a0\u89c4\u8303\uff0c\u800c\u00a0<em>Nginx<\/em>\u00a0\u4e2d\u5b9e\u73b0\u7684\u662f\u6807\u51c6\u7684\u00a0<em>POSIX<\/em>\u00a0\u89c4\u8303\uff0c\u540e\u8005\u660e\u663e\u66f4\u5177\u5907\u901a\u7528\u6027\u3002<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<p><em>Lua<\/em>\u00a0\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0e Nginx \u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u76f8\u6bd4\uff0c\u6709 5%-15%\u7684\u6027\u80fd\u635f\u5931\uff0c\u800c\u4e14 Lua \u5c06\u8868\u8fbe\u5f0f\u7f16\u8bd1\u6210 Pattern \u4e4b\u540e\uff0c\u5e76\u4e0d\u4f1a\u5c06 Pattern \u7f13\u5b58\uff0c\u800c\u662f\u6bcf\u6b64\u4f7f\u7528\u90fd\u91cd\u65b0\u7f16\u8bd1\u4e00\u904d\uff0c\u6f5c\u5728\u5730\u964d\u4f4e\u4e86\u6027\u80fd\u3002<em>Nginx<\/em>\u00a0\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570\u7f13\u5b58\u7f16\u8bd1\u8fc7\u540e\u7684 Pattern \uff0c\u4e0d\u4f1a\u6709\u7c7b\u4f3c\u7684\u6027\u80fd\u635f\u5931\u3002<\/p>\n<p>o \u9009\u9879\u53c2\u6570\u7528\u4e8e\u63d0\u9ad8\u6027\u80fd\uff0c\u6307\u660e\u8be5\u53c2\u6570\u4e4b\u540e\uff0c\u88ab\u7f16\u8bd1\u7684 Pattern \u5c06\u4f1a\u5728 worker \u8fdb\u7a0b\u4e2d\u7f13\u5b58\uff0c\u5e76\u4e14\u88ab\u5f53\u524d worker \u8fdb\u7a0b\u7684\u6bcf\u6b21\u8bf7\u6c42\u6240\u5171\u4eab\u3002 Pattern \u7f13\u5b58\u7684\u4e0a\u9650\u503c\u901a\u8fc7 lua_regex_cache_max_entries \u6765\u4fee\u6539\u3002<\/p>\n<p>Nginx \u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5305\u62ecngx.re.match\u3001ngx.re.find\u3001ngx.re.gmatch\u3001ngx.re.sub\u3001ngx.re.gsub\uff0c\u8fd95\u4e2a\u51fd\u6570\uff0c\u51fd\u6570\u7684\u4f5c\u7528\u57df\u90fd\u662f\uff1a<\/p>\n<p><strong>\u4f5c\u7528\u57df:<\/strong>\u00a0<em>init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua*<\/em><\/p>\n<h1><strong>1.\u00a0ngx.re.match\u00a0<\/strong><\/h1>\n<p><strong>\u8bed\u6cd5:<\/strong>\u00a0<em>captures, err = ngx.re.match(subject, regex, options?, ctx?, res_table?)<\/em><\/p>\n<p>\u53ea\u6709\u7b2c\u4e00\u6b21\u5339\u914d\u7684\u7ed3\u679c\u88ab\u8fd4\u56de\uff0c\u5982\u679c\u6ca1\u6709\u5339\u914d\uff0c\u5219\u8fd4\u56denil\uff1b\u6216\u8005\u5339\u914d\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u9519\u8bef\u65f6\uff0c\u4e5f\u4f1a\u8fd4\u56denil\uff0c\u6b64\u65f6\u9519\u8bef\u4fe1\u606f\u4f1a\u88ab\u4fdd\u5b58\u5728err\u4e2d\u3002<\/p>\n<p>\u5f53\u5339\u914d\u7684\u5b57\u7b26\u4e32\u627e\u5230\u65f6\uff0c\u4e00\u4e2aLua table captures\u4f1a\u88ab\u8fd4\u56de\uff0ccaptures[0]\u4e2d\u4fdd\u5b58\u7684\u5c31\u662f\u5339\u914d\u5230\u7684\u5b57\u4e32\uff0ccaptures[1]\u4fdd\u5b58\u7684\u662f\u7528\u62ec\u53f7\u62ec\u8d77\u6765\u7684\u7b2c\u4e00\u4e2a\u5b50\u6a21\u5f0f\u7684\u7ed3\u679c\uff0ccaptures[2]\u4fdd\u5b58\u7684\u662f\u7b2c\u4e8c\u4e2a\u5b50\u6a21\u5f0f\u7684\u7ed3\u679c\uff0c\u4f9d\u6b21\u7c7b\u4f3c\u3002<\/p>\n<h3><strong>\u53c2\u6570\uff1aoptions<\/strong><\/h3>\n<table border=\"1\" cellspacing=\"5\" cellpadding=\"5\">\n<tbody>\n<tr>\n<td align=\"center\" valign=\"top\" width=\"10%\"><strong>a<\/strong><\/td>\n<td valign=\"top\">\u951a\u5b9a\u6a21\u5f0f\uff0c\u53ea\u4ece\u5934\u5f00\u59cb\u5339\u914d.<\/td>\n<\/tr>\n<tr>\n<td align=\"center\" valign=\"top\"><strong>d<\/strong><\/td>\n<td valign=\"top\">DFA\u6a21\u5f0f\uff0c\u6216\u8005\u79f0\u6700\u957f\u5b57\u7b26\u4e32\u5339\u914d\u8bed\u4e49\uff0c\u9700\u8981PCRE 6.0+\u652f\u6301.<\/td>\n<\/tr>\n<tr>\n<td align=\"center\" valign=\"top\"><strong>D<\/strong><\/td>\n<td valign=\"top\">\u5141\u8bb8\u91cd\u590d\u7684\u547d\u540d\u7684\u5b50\u6a21\u5f0f\uff0c\u8be5\u9009\u9879\u9700\u8981PCRE 8.12+\u652f\u6301\uff0c\u4f8b\u5982<br \/>\nlocal m = ngx.re.match(\u201chello, world\u201d,<br \/>\n\u201c(?&lt;named&gt;\\w+), (?&lt;named&gt;\\w+)\u201d,<br \/>\n\u201cD\u201d)<br \/>\n\u2014 m[\u201cnamed\u201d] == {\u201chello\u201d, \u201cworld\u201d}<\/td>\n<\/tr>\n<tr>\n<td align=\"center\" valign=\"top\"><strong>i<\/strong><\/td>\n<td valign=\"top\">\u5927\u5c0f\u5199\u4e0d\u654f\u611f\u6a21\u5f0f.<\/td>\n<\/tr>\n<tr>\n<td align=\"center\" valign=\"top\"><strong>j<\/strong><\/td>\n<td valign=\"top\">\u542f\u7528PCRE JIT\u7f16\u8bd1, \u9700\u8981PCRE 8.21+ \u652f\u6301\uff0c\u5e76\u4e14\u5fc5\u987b\u5728\u7f16\u8bd1\u65f6\u52a0\u4e0a\u9009\u9879\u2013enable-jit\uff0c\u4e3a\u4e86\u8fbe\u5230\u6700\u4f73\u6027\u80fd\uff0c\u8be5\u9009\u9879\u603b\u662f\u5e94\u8be5\u548c\u2019o\u2019\u9009\u9879\u642d\u914d\u4f7f\u7528.<\/td>\n<\/tr>\n<tr>\n<td align=\"center\" valign=\"top\"><strong>J<\/strong><\/td>\n<td valign=\"top\">\u542f\u7528PCRE Javascript\u7684\u517c\u5bb9\u6a21\u5f0f\uff0c\u9700\u8981PCRE 8.12+ \u652f\u6301.<\/td>\n<\/tr>\n<tr>\n<td align=\"center\" valign=\"top\"><strong>m<\/strong><\/td>\n<td valign=\"top\">\u591a\u884c\u6a21\u5f0f.<\/td>\n<\/tr>\n<tr>\n<td align=\"center\" valign=\"top\"><strong>o<\/strong><\/td>\n<td valign=\"top\">\u4e00\u6b21\u7f16\u8bd1\u6a21\u5f0f\uff0c\u542f\u7528worker-process\u7ea7\u522b\u7684\u7f16\u8bd1\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u7f13\u5b58.<\/td>\n<\/tr>\n<tr>\n<td align=\"center\" valign=\"top\"><strong>s<\/strong><\/td>\n<td valign=\"top\">\u5355\u884c\u6a21\u5f0f.<\/td>\n<\/tr>\n<tr>\n<td align=\"center\" valign=\"top\"><strong>u<\/strong><\/td>\n<td valign=\"top\">UTF-8\u6a21\u5f0f. \u8be5\u9009\u9879\u9700\u8981\u5728\u7f16\u8bd1PCRE\u5e93\u65f6\u52a0\u4e0a\u2013enable-utf8 \u9009\u9879.<\/td>\n<\/tr>\n<tr>\n<td align=\"center\" valign=\"top\"><strong>U<\/strong><\/td>\n<td valign=\"top\">\u4e0e\u201du\u201d \u9009\u9879\u7c7b\u4f3c\uff0c\u4f46\u662f\u8be5\u9879\u9009\u7981\u6b62PCRE\u5bf9subject\u5b57\u7b26\u4e32UTF-8\u6709\u6548\u6027\u7684\u68c0\u67e5.<\/td>\n<\/tr>\n<tr>\n<td align=\"center\" valign=\"top\"><strong>x<\/strong><\/td>\n<td valign=\"top\">\u6269\u5c55\u6a21\u5f0f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><strong>\u53ef\u9009\u53c2\u6570\uff1actx<\/strong><\/h3>\n<p>\u53ef\u9009\u53c2\u6570<em>ctx<\/em>\u53ef\u4ee5\u4f20\u5165\u4e00\u4e2aLua Table\uff0c\u4f20\u5165\u7684Lua Table\u53ef\u4ee5\u662f\u4e00\u4e2a\u7a7a\u8868\uff0c\u4e5f\u53ef\u4ee5\u662f\u5305\u542bpos\u5b57\u6bb5\u7684Lua Table\u3002\u5982\u679c\u4f20\u5165\u7684\u662f\u4e00\u4e2a\u7a7a\u7684Lua Table\uff0c\u90a3\u4e48\uff0cngx.re.match\u5c06\u4f1a\u4ecesubject\u5b57\u7b26\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\u5f00\u59cb\u5339\u914d\u67e5\u627e\uff0c\u67e5\u627e\u5230\u5339\u914d\u4e32\u540e\uff0c\u4fee\u6539pos\u7684\u503c\u4e3a\u5339\u914d\u5b57\u7b26\u4e32\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e\u7684\u503c\uff0c\u5e76\u5c06pos\u7684\u503c\u4fdd\u5b58\u5230ctx\u4e2d\uff0c\u5982\u679c\u5339\u914d\u5931\u8d25\uff0c\u90a3\u4e48pos\u7684\u503c\u4fdd\u6301\u4e0d\u53d8\uff1b\u5982\u679c\u4f20\u5165\u7684\u662f\u4e00\u4e2a\u975e\u7a7a\u7684Lua Table\uff0c\u5373\u6307\u5b9a\u4e86pos\u7684\u521d\u503c\uff0c\u90a3\u4e48ngx.re.match\u5c06\u4f1a\u4ece\u6307\u5b9a\u7684pos\u7684\u4f4d\u7f6e\u5f00\u59cb\u8fdb\u884c\u5339\u914d\uff0c\u5982\u679c\u5339\u914d\u6210\u529f\u4e86\uff0c\u4fee\u6539pos\u7684\u503c\u4e3a\u5339\u914d\u5b57\u7b26\u4e32\u7684\u4e0b\u4e00\u4e2a\u4f4d\u7f6e\u7684\u503c\uff0c\u5e76\u5c06pos\u7684\u503c\u4fdd\u5b58\u5230ctx\u4e2d\uff0c\u5982\u679c\u5339\u914d\u5931\u8d25\uff0c\u90a3\u4e48pos\u7684\u503c\u4fdd\u6301\u4e0d\u53d8\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>local ctx = {pos = 1}\r\nlocal url = \"https:\/\/www.baidu.com\/s?wd=site%3Ahuangxiaobai.com&amp;iq=site%3Abaidu.com&amp;ie=utf-8\"\r\nlocal capture, err = ngx.re.match(url, \"site([^&amp;]+)\", \"jo\", ctx)\r\nprint(capture)\r\nprint(ctx)<\/pre>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>{\"0\":\"site%3Ahuangxiaobai.com\",\"1\":\"%3Ahuangxiaobai.com\"}\r\n{\"pos\":51}<\/pre>\n<h1><strong>\u7ed3\u8bba \uff1a<\/strong><\/h1>\n<pre><strong>1\u3001ngx.re.match\u6709\u70b9\u7c7b\u4f3cPHP\u4e2d\u7684preg_match\uff0c\u7b2c\u4e00\u6b21\u5339\u914d\u540e \u5c06\u4f1a\u505c\u6b62\u641c\u7d22\u3002\r\n2\u3001\u4efb\u4f55\u65f6\u523b\u5e26\u4e0a\u53c2\u6570\"jo\"\r\n3\u3001\u5982\u65e0\u5339\u914d\u6216\u5339\u914d\u9519\u8bef\uff0ccapture\u8fd4\u56denil\uff0c\u4f7f\u7528 if not capture \u5224\u65ad\u662f\u5426\u5339\u914d\u5373\u53ef\u3002<\/strong><\/pre>\n<h1><strong>2.\u00a0ngx.re.find<\/strong><\/h1>\n<p><strong>\u8bed\u6cd5:<\/strong>\u00a0<em>from, to, err = ngx.re.find(subject, regex, options?, ctx?, nth?)<\/em><\/p>\n<p>\u8be5\u65b9\u6cd5\u4e0engx.re.match\u65b9\u6cd5\u57fa\u672c\u7c7b\u4f3c\uff0c\u4e0d\u540c\u7684\u5730\u65b9\u5728\u4e8engx.re.find\u8fd4\u56de\u7684\u662f\u5339\u914d\u7684\u5b57\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\u7d22\u5f15\u548c\u7ed3\u675f\u4f4d\u7f6e\u7d22\u5f15\uff0c\u5982\u679c\u6ca1\u6709\u5339\u914d\u6210\u529f\uff0c\u90a3\u4e48\u5c06\u4f1a\u8fd4\u56de\u4e24\u4e2anil\uff0c\u5982\u679c\u5339\u914d\u51fa\u9519\uff0c\u8fd8\u4f1a\u8fd4\u56de\u9519\u8bef\u4fe1\u606f\u5230err\u4e2d\u3002<\/p>\n<p>\u8be5\u65b9\u6cd5\u76f8\u6bd4ngx.re.match\uff0c\u4e0d\u4f1a\u521b\u5efa\u65b0\u7684Lua\u5b57\u7b26\u4e32\uff0c\u4e5f\u4e0d\u4f1a\u521b\u5efa\u65b0\u7684Lua Table\uff0c\u56e0\u6b64\uff0c\u8be5\u65b9\u6cd5\u6bd4ngx.re.match\u66f4\u52a0\u9ad8\u6548\uff0c\u56e0\u6b64\uff0c\u5728\u53ef\u4ee5\u4f7f\u7528ngx.re.find\u7684\u5730\u65b9\u5e94\u8be5\u5c3d\u91cf\u4f7f\u7528\u3002<\/p>\n<h3><strong>\u53c2\u6570<em>options<\/em>\u00a0\u548c \u53c2\u6570 ctx \uff0c\u53c2\u8003ngx.re.match<\/strong><\/h3>\n<h3><strong>\u53c2\u6570\uff1anth<\/strong><\/h3>\n<p>\u53ef\u4ee5\u6307\u5b9a\u8fd4\u56de\u7b2c\u51e0\u4e2a\u5b50\u6a21\u5f0f\u4e32\u7684\u8d77\u59cb\u4f4d\u7f6e\u548c\u7ed3\u675f\u4f4d\u7f6e\u7684\u7d22\u5f15\u503c\uff0c\u9ed8\u8ba4\u503c\u662f0\uff0c\u6b64\u65f6\u5c06\u4f1a\u8fd4\u56de\u5339\u914d\u7684\u6574\u4e2a\u5b57\u4e32\uff1b\u5982\u679c<em>nth<\/em>\u7b49\u4e8e1\uff0c\u90a3\u4e48\u5c06\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5b50\u6a21\u5f0f\u4e32\u7684\u59cb\u672b\u4f4d\u7f6e\u7684\u7d22\u5f15\u503c\uff1b\u5982\u679c<em>nth<\/em>\u7b49\u4e8e2\uff0c\u90a3\u4e48\u5c06\u8fd4\u56de\u7b2c\u4e8c\u4e2a\u5b50\u6a21\u5f0f\u4e32\u7684\u59cb\u672b\u4f4d\u7f6e\u7684\u7d22\u5f15\u503c\uff0c\u4f9d\u6b21\u7c7b\u63a8\u3002\u5982\u679c<em>nth<\/em>\u6307\u5b9a\u7684\u5b50\u6a21\u5f0f\u6ca1\u6709\u5339\u914d\u6210\u529f\uff0c\u90a3\u4e48\u5c06\u4f1a\u8fd4\u56de\u4e24\u4e2anil<\/p>\n<h1>\u7ed3\u8bba\uff1a<\/h1>\n<pre><strong>1\u3001ngx.re.match\u6709\u70b9\u7c7b\u4f3cPHP\u4e2d\u7684preg_match\uff0c\u7b2c\u4e00\u6b21\u5339\u914d\u540e \u5c06\u4f1a\u505c\u6b62\u641c\u7d22\u3002\r\n2\u3001\u4efb\u4f55\u65f6\u523b\u5e26\u4e0a\u53c2\u6570\"jo\"\r\n3\u3001\u5982\u65e0\u5339\u914d\u6216\u5339\u914d\u9519\u8bef\uff0cfrom\u3001to\u8fd4\u56denil\uff0c\u4f7f\u7528 if not from \u5224\u65ad\u662f\u5426\u5339\u914d\u5373\u53ef\u3002\r\n4\u3001\u6027\u80fd\u4f18\u8d8a\u4e8e ngx.re.match<\/strong><\/pre>\n<h1><strong>3.\u00a0ngx.re.gmatch<\/strong><\/h1>\n<p><strong>\u8bed\u6cd5:<\/strong>\u00a0<em>iterator, err = ngx.re.gmatch(subject, regex, options?)<\/em><\/p>\n<p>\u4e0engx.re.match\u76f8\u4f3c\uff0c\u533a\u522b\u5728\u4e8e\u8be5\u65b9\u6cd5\u8fd4\u56de\u7684\u662f\u4e00\u4e2aLua\u7684\u8fed\u4ee3\u5668\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u901a\u8fc7\u8fed\u4ee3\u5668\u904d\u5386\u6240\u6709\u5339\u914d\u7684\u7ed3\u679c\u3002<\/p>\n<p>\u5982\u679c\u5339\u914d\u5931\u8d25\uff0c\u5c06\u4f1a\u8fd4\u56denil\uff0c\u5982\u679c\u5339\u914d\u51fa\u73b0\u9519\u8bef\uff0c\u90a3\u4e48\u8fd8\u4f1a\u8fd4\u56de\u9519\u8bef\u4fe1\u606f\u5230err\u4e2d\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>local url = \"https:\/\/www.baidu.com\/s?wd=sit1e%3Ahuangxiaobai.com&amp;iq=sit1e%3Abaidu.com&amp;ie=utf-8\"\r\nlocal iterator, err = ngx.re.gmatch(url, \"site([^&amp;]+)\", \"jo\")\r\nif iterator then\r\n    while true do\r\n        local it, err = iterator()\r\n        if not it then break end\r\n        print(it)\r\n    end\r\nend<\/pre>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>{\"0\":\"site%3Ahuangxiaobai.com\",\"1\":\"%3Ahuangxiaobai.com\"}\r\n{\"0\":\"site%3Abaidu.com\",\"1\":\"%3Abaidu.com\"}<\/pre>\n<p><span id=\"ngx.re.gmatch\" class=\"mw-headline\"><strong>\u6ce8\u610f\uff1angx.re.gmatch<\/strong>\u8fd4\u56de\u7684<\/span>\u8fed\u4ee3\u5668<strong>\u53ea\u80fd\u5728\u4e00\u4e2a\u8bf7\u6c42\u6240\u5728\u7684\u73af\u5883\u4e2d\u4f7f\u7528<\/strong>\uff0c\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u4e0d\u80fd\u628a\u8fd4\u56de\u7684\u8fed\u4ee3\u5668\u8d4b\u503c\u7ed9\u6301\u4e45\u5b58\u5728\u7684\u547d\u540d\u7a7a\u95f4\uff08\u6bd4\u5982\u4e00\u4e2aLua Packet\uff09\u4e2d\u7684\u67d0\u4e00\u4e2a\u53d8\u91cf\u3002<\/p>\n<h1>\u7ed3\u8bba\uff1a<\/h1>\n<pre><strong>1\u3001ngx.re.match\u6709\u70b9\u7c7b\u4f3cPHP\u4e2d\u7684preg_match_all\r\n2\u3001\u4efb\u4f55\u65f6\u523b\u5e26\u4e0a\u53c2\u6570\"jo\"\r\n3\u3001\u5982\u65e0\u5339\u914d\u6216\u5339\u914d\u9519\u8bef\uff0citerator \u8fd4\u56denil\uff0c\u4f7f\u7528 if not iterator \u5224\u65ad\u662f\u5426\u5339\u914d\u5373\u53ef\u3002\r\n4\u3001ngx.re.gmatch\u8fd4\u56de\u7684\u8fed\u4ee3\u5668\u53ea\u80fd\u5728\u4e00\u4e2a\u8bf7\u6c42\u6240\u5728\u7684\u73af\u5883\u4e2d\u4f7f\u7528\uff0c\u5c31\u662f\u8bf4\uff0c\u6211\u4eec\u4e0d\u80fd\u628a\u8fd4\u56de\u7684\u8fed\u4ee3\u5668\u8d4b\u503c\u7ed9<\/strong>\r\n<strong>  \u6301\u4e45\u5b58\u5728\u7684\u547d\u540d\u7a7a\u95f4<\/strong><\/pre>\n<h1><strong>4.\u00a0ngx.re.sub<\/strong><\/h1>\n<p><strong>\u8bed\u6cd5:<\/strong>\u00a0<em>newstr, n, err = ngx.re.sub(subject, regex, replace, options?)<\/em><\/p>\n<p>\u8be5\u65b9\u6cd5\u4e3b\u8981\u5b9e\u73b0\u5339\u914d\u5b57\u7b26\u4e32\u7684\u66ff\u6362\uff0c\u4f1a\u7528replace\u66ff\u6362\u5339\u914d\u7684\u5b57\u4e32\uff0creplace\u53ef\u4ee5\u662f\u7eaf\u5b57\u7b26\u4e32\uff0c\u4e5f\u53ef\u4ee5\u662f\u4f7f\u7528$0, $1\u7b49\u5b50\u6a21\u5f0f\u4e32\u7684\u5f62\u5f0f\uff0cngx.re.sub\u8fd4\u56de\u8fdb\u884c\u66ff\u6362\u540e\u7684\u5b8c\u6574\u7684\u5b57\u7b26\u4e32\uff0c\u540c\u65f6\u8fd4\u56de\u66ff\u6362\u7684\u603b\u4e2a\u6570\uff1boptions\u9009\u9879\uff0c\u4e0engx.re.match\u4e2d\u7684options\u9009\u9879\u662f\u4e00\u6837\u7684\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>local url = \"https:\/\/www.baidu.com\/s?wd=site%3Ahuangxiaobai.com&amp;iq=site%3Abaidu.com&amp;ie=utf-8\"\r\nlocal newStr, n, err = ngx.re.sub(url, \"site([^&amp;]+)\", \"[$0][$1]\", \"jo\")\r\nprint(newStr)<\/pre>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>https:\/\/www.baidu.com\/s?wd=[site%3Ahuangxiaobai.com][%3Ahuangxiaobai.com]&amp;\r\niq=site%3Abaidu.com&amp;ie=utf-8<\/pre>\n<h1><strong>\u7ed3\u8bba\uff1a<\/strong><\/h1>\n<pre>1\u3001\u4e5f\u53ea\u4ec5\u5339\u914d\u4e00\u6b21\uff0c<strong>\u7b2c\u4e00\u6b21\u5339\u914d\u540e \u5c06\u4f1a\u505c\u6b62\u641c\u7d22\u3002\r\n<\/strong>2\u3001\u5982\u65e0\u5339\u914d\uff0c\u5219newstr\u8fd4\u56de\uff0c\u548csubject\u76f8\u540c\u7684\u5b57\u7b26\u4e32\u3002\r\n3\u3001$0\u8868\u793a\u6574\u4e2a\u5339\u914d\u7684\u5b50\u4e32\uff0c$1\u8868\u793a\u7b2c\u4e00\u4e2a\u5b50\u6a21\u5f0f\u5339\u914d\u7684\u5b57\u4e32\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002\r\n4\u3001\u53ef\u4ee5\u7528\u5927\u62ec\u53f7{}\u5c06\u76f8\u5e94\u76840\uff0c1\uff0c2...\u62ec\u8d77\u6765\uff0c\u4ee5\u533a\u5206\u4e00\u822c\u7684\u6570\u5b57\u3002\r\n5\u3001\u5982\u679c\u60f3\u5728replace\u5b57\u7b26\u4e32\u4e2d\u663e\u793a$\u7b26\u53f7\uff0c\u53ef\u4ee5\u7528$\u8fdb\u884c\u8f6c\u4e49\uff08\u4e0d\u8981\u7528\u53cd\u659c\u6760\\$\u5bf9\u7f8e\u5143\u7b26\u53f7\u8fdb\u884c\u8f6c\u4e49\uff0c\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4f1a\r\n   \u5f97\u5230\u671f\u671b\u7684\u7ed3\u679c\uff09\r\n6\u3001\u5982\u679creplace\u662f\u4e00\u4e2a\u51fd\u6570\uff0c\u90a3\u4e48\u51fd\u6570\u7684\u53c2\u6570\u662f\u4e00\u4e2a\"match table\"\uff0c \u800c\u8fd9\u4e2a\"match table\"\u4e0engx.re.match\r\n   \u4e2d\u7684\u8fd4\u56de\u503ccaptures\u662f\u4e00\u6837\u7684\uff0creplace\u8fd9\u4e2a\u51fd\u6570\u6839\u636e\"match table\"\u4ea7\u751f\u7528\u4e8e\u66ff\u6362\u7684\u5b57\u7b26\u4e32\u3002<\/pre>\n<p><strong>\u4f8b\u5982\uff1a<\/strong><\/p>\n<pre class=\"de1\"><span class=\"kw1\"> local<\/span> newstr, n, err <span class=\"sy0\">=<\/span> ngx.re.sub<span class=\"br0\">(<\/span><span class=\"st0\">\"hello, 1234\"<\/span>, <span class=\"st0\">\"[0-9]\"<\/span>, <span class=\"st0\">\"${0}00\"<\/span><span class=\"br0\">)<\/span><\/pre>\n<p><strong>\u4f8b\u5982\uff1a<\/strong><\/p>\n<pre class=\"de1\"><span class=\"kw1\">local<\/span> newstr, n, err <span class=\"sy0\">=<\/span> ngx.re.sub<span class=\"br0\">(<\/span><span class=\"st0\">\"hello, 1234\"<\/span>, <span class=\"st0\">\"[0-9]\"<\/span>, <span class=\"st0\">\"$$\"<\/span><span class=\"br0\">)<\/span><\/pre>\n<p><strong>\u4f8b\u5982\uff1a<\/strong><\/p>\n<pre>local replace = function(m)\r\n    return \"[\"..m[0]..\"][\"..m[1]..\"]\"\r\nend\r\nlocal url = \"https:\/\/www.baidu.com\/s?wd=site%3Ahuangxiaobai.com&amp;iq=site%3Abaidu.com&amp;ie=utf-8\"\r\nlocal newStr, n, err = ngx.re.sub(url, \"site([^&amp;]+)\", replace, \"jo\")\r\nprint(newStr)<\/pre>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>https:\/\/www.baidu.com\/s?wd=[site%3Ahuangxiaobai.com][%3Ahuangxiaobai.com]&amp;\r\niq=site%3Abaidu.com&amp;ie=utf-8<\/pre>\n<h1><strong>5.\u00a0ngx.re.gsub<\/strong><\/h1>\n<p><strong>\u8bed\u6cd5:<\/strong>\u00a0<em>newstr, n, err = ngx.re.gsub(subject, regex, replace, options?)<\/em><\/p>\n<p>\u8be5\u65b9\u6cd5\u4e0engx.re.sub\u662f\u7c7b\u4f3c\u7684\uff0c\u4f46\u662f\u8be5\u65b9\u6cd5\u8fdb\u884c\u7684\u662f\u5168\u5c40\u66ff\u6362\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>local replace = function(m)\r\n    return \"[\"..m[0]..\"][\"..m[1]..\"]\"\r\nend\r\nlocal url = \"https:\/\/www.baidu.com\/s?wd=site%3Ahuangxiaobai.com&amp;iq=site%3Abaidu.com&amp;ie=utf-8\"\r\nlocal newStr, n, err = ngx.re.gsub(url, \"site([^&amp;]+)\", replace, \"jo\")\r\nprint(newStr)<\/pre>\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n<pre>https:\/\/www.baidu.com\/s?wd=[site%3Ahuangxiaobai.com][%3Ahuangxiaobai.com]\r\n&amp;iq=[site%3Abaidu.com][%3Abaidu.com]&amp;ie=utf-8<\/pre>\n<h1><strong>\u00a0\u7ed3\u8bba\uff1a<\/strong><\/h1>\n<pre>1\u3001\u7528\u6cd5\u540cngx.re.sub\uff0c\u53ea\u4e0d\u8fc7\u8be5\u65b9\u6cd5\u8fdb\u884c\u7684\u662f\u5168\u5c40\u66ff\u6362<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u00a0OpenResty\u00a0\u4e2d\uff0c\u540c\u65f6\u5b58\u5728\u4e24\u5957\u6b63\u5219\u8868\u8fbe\u5f0f\u89c4\u8303\uff1aLua\u00a0\u8bed\u8a00\u7684\u89c4\u8303\u548c\u00a0Nginx\u00a0\u7684\u89c4\u8303\uff1b\u5373\u4f7f\u60a8\u5bf9\u00a0Lua\u00a0\u8bed\u8a00\u4e2d\u7684\u89c4\u8303\u975e\u5e38\u719f\u6089\uff0c\u6211\u4eec\u4ecd\u4e0d\u5efa\u8bae\u4f7f\u7528\u00a0Lua\u00a0\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u3002 \u56e0\u4e3a\u00a0Lua\u00a0\u4e2d\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u6027\u80fd\u5e76\u4e0d\u5982\u00a0Nginx\u00a0\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u4f18\u79c0\uff1b Lua\u00a0\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5e76\u4e0d\u7b26\u5408\u00a0POSIX\u00a0\u89c4\u8303\uff0c\u800c\u00a0Nginx\u00a0\u4e2d\u5b9e\u73b0\u7684\u662f\u6807\u51c6\u7684\u00a0POSIX\u00a0\u89c4\u8303\uff0c\u540e\u8005\u660e\u663e\u66f4\u5177\u5907\u901a\u7528\u6027\u3002 &nbsp; Lua\u00a0\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0e Nginx \u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u76f8\u6bd4\uff0c\u6709 5%-15%\u7684\u6027\u80fd\u635f\u5931\uff0c\u800c\u4e14 Lua \u5c06\u8868\u8fbe\u5f0f\u7f16\u8bd1\u6210 Pattern \u4e4b\u540e\uff0c\u5e76\u4e0d\u4f1a\u5c06 Pattern \u7f13\u5b58\uff0c\u800c\u662f\u6bcf\u6b64\u4f7f\u7528\u90fd\u91cd\u65b0\u7f16\u8bd1\u4e00\u904d\uff0c\u6f5c\u5728\u5730\u964d\u4f4e\u4e86\u6027\u80fd\u3002Nginx\u00a0\u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u53ef\u4ee5\u901a\u8fc7\u53c2\u6570\u7f13\u5b58\u7f16\u8bd1\u8fc7\u540e\u7684 Pattern \uff0c\u4e0d\u4f1a\u6709\u7c7b\u4f3c\u7684\u6027\u80fd\u635f\u5931\u3002 o \u9009\u9879\u53c2\u6570\u7528\u4e8e\u63d0\u9ad8\u6027\u80fd\uff0c\u6307\u660e\u8be5\u53c2\u6570\u4e4b\u540e\uff0c\u88ab\u7f16\u8bd1\u7684 Pattern \u5c06\u4f1a\u5728 worker \u8fdb\u7a0b\u4e2d\u7f13\u5b58\uff0c\u5e76\u4e14\u88ab\u5f53\u524d worker \u8fdb\u7a0b\u7684\u6bcf\u6b21\u8bf7\u6c42\u6240\u5171\u4eab\u3002 Pattern \u7f13\u5b58\u7684\u4e0a\u9650\u503c\u901a\u8fc7 lua_regex_cache_max_entries \u6765\u4fee\u6539\u3002 Nginx \u4e2d\u7684\u6b63\u5219\u8868\u8fbe\u5f0f\u5305\u62ecngx.re.match\u3001ngx.re.find\u3001ngx.re.gmatch\u3001ngx.re.sub\u3001ngx.re.gsub\uff0c\u8fd95\u4e2a\u51fd\u6570\uff0c\u51fd\u6570\u7684\u4f5c\u7528\u57df\u90fd\u662f\uff1a \u4f5c\u7528\u57df:\u00a0init_worker_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*, ngx.timer.*, balancer_by_lua*, ssl_certificate_by_lua*, ssl_session_fetch_by_lua*, ssl_session_store_by_lua* 1.\u00a0ngx.re.match\u00a0 \u8bed\u6cd5:\u00a0captures, err = ngx.re.match(subject, regex, options?, ctx?, res_table?) \u53ea\u6709\u7b2c\u4e00\u6b21\u5339\u914d\u7684\u7ed3\u679c\u88ab\u8fd4\u56de\uff0c\u5982\u679c\u6ca1\u6709\u5339\u914d\uff0c\u5219\u8fd4\u56denil\uff1b\u6216\u8005\u5339\u914d\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u9519\u8bef\u65f6\uff0c\u4e5f\u4f1a\u8fd4\u56denil\uff0c\u6b64\u65f6\u9519\u8bef\u4fe1\u606f\u4f1a\u88ab\u4fdd\u5b58\u5728err\u4e2d\u3002 \u5f53\u5339\u914d\u7684\u5b57\u7b26\u4e32\u627e\u5230\u65f6\uff0c\u4e00\u4e2aLua table [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-15401","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15401","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=15401"}],"version-history":[{"count":1,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15401\/revisions"}],"predecessor-version":[{"id":15402,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/15401\/revisions\/15402"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=15401"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=15401"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=15401"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}