@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 配置的模式
集成到我们的系统中是一个不错的选择
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2021-04-09 cube.js mssql driver 问题
2019-04-09 使用fpm 方便快速生成postgresql extension分发包
2014-04-09 web性能优化