logregexnginxconfig
v1.0.0
Published
generate Regex From Nginx Config
Downloads
13
Readme
nginx配置正则验证
安装
npm install logtailregexnginxconfig
配置
var logtailregexnginxconfig = require ('logtailregexnginxconfig');
var logtailregexnginxconfig = function (logFormat) {}
/* @Params: logFormat参数就是标准NGINX配置文件中对应log_format内容 通过logFormat.match() 提取实际配置部分; 优先提取多行NGINX配置 通过单引号提取实际KEY列表 */
if (logFormat == undefined || logFormat == null)
return null;
示例
const logtailregexnginxconfig = require('logtailregexnginxconfig')//引入logtailregexnginxconfig
const log_sample = `42.110.55.127 - - [17/Jan/2019:16:52:17 +0800] "GET /favicon.ico HTTP/1.1" 45.16.35.141 404 404 570 "http://example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" 0.000 -` //配置 日志样例
const log_format = `log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $request_length '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent"';` //配置推荐的log_format日志格式
const result = logtailregexnginxconfig(log_format)
const generateRegex = new RegExp(result.regex) //生成一个正则
console.log(log_sample.match(generateRegex))//对正则进行macth操作,分割枚举
/*
上述代码运行结果如下:
[ '42.110.55.127 - - [17/Jan/2019:16:52:17 +0800] "GET /favicon.ico HTTP/1.1" 45.16.35.141 404 404 570 "http://example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" 0.000 -',
'42.110.55.127',
'-',
'17/Jan/2019:16:52:17',
'GET',
'/favicon.ico',
'45.16.35.141',
'404',
'404',
'570',
'http://example.com/',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
index: 0,
input: '42.110.55.127 - - [17/Jan/2019:16:52:17 +0800] "GET /favicon.ico HTTP/1.1" 45.16.35.141 404 404 570 "http://example.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36" 0.000 -',
groups: undefined ]
*/