@webantic/nginx-config-parser nodejs nginx conf 解析以及生成处理包试用

@webantic/nginx-config-parser 是一个基于nodejs 开发nginx 配置解析工具

参考使用

  • demo.conf
upstream mydemoapp {
        # simple round-robin
        server app1:80;
        server app2:80;
        check interval=1000 rise=2 fall=5 timeout=1000 type=http;
        check_http_send "HEAD / HTTP/1.0\r\n\r\n";
        check_http_expect_alive http_2xx http_3xx;
}
server {
    listen       80;
    server_name  mydemoapp.com;
    location / {
        root   html;
        index  index.html index.htm;
        proxy_pass http://mydemoapp;
    }
    location /status {
        healthcheck_status;
    }
}
 
 
server {
    listen       443 http2;
    server_name  mydemoapp.com;
    ssl app/cer;
    location / {
        root   html;
        index  index.html index.htm;
        proxy_pass http://mydemoapp;
    }
    location /status {
        healthcheck_status;
    }
}
  • app.js
var ConfigParser = require('@webantic/nginx-config-parser')
var parser = new ConfigParser()
 
// parse straight from file. by default, will try to resolve includes
var config = parser.readConfigFile('demo.conf')
 
// to keep deterministic behaviour, set parseIncludes = false in the options
var configWithoutIncludes = parser.readConfigFile('demo.conf', { parseIncludes: false })
 
// write direct to file (overwriting existing one)
// 读取写到其他地方,比如基于json的解析,然后写到一个s3 的挂载中
parser.writeConfigFile('mynginx.conf', config, true)
console.log(configWithoutIncludes.server)
 
// 基于json 的nginx 配置生成
var sampleConfig = {
    "upstream mydemoapp":{
        "server":[
            "app1:80",
            "app2:80"
        ],
        "check":"interval=1000 rise=2 fall=5 timeout=1000 type=http",
        "check_http_send":"\"HEAD / HTTP/1.0\\\\r\\n\\r\\n\"",
        "check_http_expect_alive":"http_2xx http_3xx"
    },
    "server":[
        {
            "listen":"80",
            "server_name":"mydemoapp.com",
            "location /":{
                "root":"html",
                "index":"index.html index.htm",
                "proxy_pass":"http://mydemoapp"
            },
            "location /status":{
                "healthcheck_status":""
            }
        },
        {
            "listen":"443 http2",
            "server_name":"mydemoapp.com",
            "ssl":"app/cer",
            "location /":{
                "root":"html",
                "index":"index.html index.htm",
                "proxy_pass":"http://mydemoapp"
            },
            "location /status":{
                "healthcheck_status":""
            }
        }
    ]
}
 
// to multi-line config string
var configString = parser.toConf(sampleConfig)
var configString2 = parser.parse(sampleConfig)
console.log("app:",configString)
console.log("app2:",configString2)
// and back again
var configJson = parser.toJSON(configString)
 
// shorthand (will change object --> string and string --> object)
parser.parse(configString)
 
console.log(JSON.stringify(config))

说明

目前关于nginx 解析方便的工具并不是很多(主要是nginx 配置并不是很复杂,我们基于模版引擎也可以解决)
@webantic/nginx-config-parser 是一个不错的选择,可以解决我们管理nginx 配置的一些问题,而且支持json 以及nginx 配置的模式
集成到我们的系统中是一个不错的选择

参考资料

https://github.com/webantic/nginx-config-parser

posted on 2022-04-09 21:33  荣锋亮  阅读(183)  评论(0编辑  收藏  举报

导航