node解析修改nginx配置文件操作实例分析


Posted in Javascript onNovember 06, 2019

本文实例讲述了node解析修改nginx配置文件操作。分享给大家供大家参考,具体如下:

主要是通过nginx-conf这个工具。

git地址:https://github.com/tmont/nginx-conf

具体用法:

npm install -S nginx-conf 安装工具

var NginxConfFile = require('nginx-conf').NginxConfFile;
// 这个api提供了node读写conf文件的功能
NginxConfFile.create('/etc/nginx.conf', function(err, conf) {
 if (err) {
  console.log(err);
  return;
 }
// 通过_value的方式读取每一个配置的值
 console.log(conf.nginx.user._value); //www www
 console.log(conf.nginx.http.server.listen._value); //one.example.com
 //模块中有多个子模块,比如server中配置了多个location,通过数组下标的方式访问
 console.log(conf.nginx.http.server.location[3].root._value); // /spool/www
 //修改配置
 //create api是同步修改文件的,对于配置的修改和删除会同步反映到磁盘中
 conf.on('flushed', function() {
  console.log('finished writing to disk');
 });
 //listen to the flushed event to determine when the new file has been flushed to disk
 conf.nginx.events.connections._value = 1000;
 // 这个api的用途是当配置改变时不写到磁盘中
 conf.die('/etc/nginx.conf');
 conf.nginx.events.connections._value = 2000; //change remains local, not in /etc/nginx.conf
 // 将内存中的配置写到另一个文件中
 conf.live('/etc/nginx.conf.bak');
 // 强行将内存中的配置刷到磁盘中
 conf.flush();
 // 增加和移除指令 通过 _add 和 _remove
 conf.nginx.http._add('add_header', 'Cache-Control max-age=315360000, public');
 console.log(conf.nginx.http.add_header._value); //Cache-Control max-age=315360000, public
 conf.nginx.http._add('add_header', 'X-Load-Balancer lb-01');
 conf.nginx.http._add('add_header', 'X-Secure true');
 console.log(conf.nginx.http.add_header[0]._value); //Cache-Control max-age=315360000, public
 console.log(conf.nginx.http.add_header[1]._value); //X-Load-Balancer lb-01
 console.log(conf.nginx.http.add_header[2]._value); //X-Secure true
 conf.nginx.http._remove('add_header'); //removes add_header[0]
 conf.nginx.http._remove('add_header', 1); //removes add_header[1]
 //如果只有一个带有名称的指令,会被被展开成一个属性,通过数组下表访问的将是undefined
 console.log(conf.nginx.http.add_header._value); //X-Load-Balancer lb-01
 console.log(conf.nginx.http.add_header[0]); //undefined
 // 增加一个新的模块
 conf.nginx.http._add('server');
 conf.nginx.http.server._add('listen', '80');
 //that'll create something like this:
 /*
  server {
   listen 80;
  }
 */
 // 存在多个模块是通过数组方式访问
 conf.nginx.http._add('server');
 conf.nginx.http.server[1]._add('listen', '443');
 /*
  server {
   listen 80;
  }
  server {
   listen 443;
  }
 */
 // blocks with values:
 conf.nginx.http.server[1]._add('location', '/');
 conf.nginx.http.server[1].location._add('root', '/var/www/example.com');
 /*
  server {
   location / {
    root /var/www/example.com;
   }
  }
 */
 // lua blocks also work, but you can't put a mismatched "{" or "}" in a comment!
 conf.nginx.http.location._addVerbatimBlock('rewrite_by_lua_block', '{\n\
 ngx.say("this is a lua block!")\n\
 res = ngx.location.capture("/memc",\n\
  { args = { cmd = "incr", key = ngx.var.uri } }\n\
 )\n\
}');
});

此工具同样支持对注释的修改

// 读取use配置上的注释,以数组的方式返回
console.log(conf.nginx.events.use._comments.length); // 1
console.log(conf.nginx.events.use._comments[0]); // use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];
// 删除注释
conf.nginx.events.use._comments.splice(0, 1);
// 添加注释
conf.nginx.event.use._comments.push('my new comment');
console.log(conf.nginx.events.use._comments.length); // 1
console.log(conf.nginx.events.use._comments[0]); //my new comment
// 修改注释
conf.nginx.event.use._comments[0] = 'updated';
console.log(conf.nginx.events.use._comments[0]); //updated

注意特殊情况

foo #comment
bar;
console.log(conf.nginx.foo._value); //bar
console.log(conf.nginx.foo._comments[0]); //comment
But if the comment comes after:
foo bar;
#comment
console.log(conf.nginx.foo._value); //bar
console.log(conf.nginx.foo._comments.length); //0

希望本文所述对大家node.js程序设计有所帮助。

Javascript 相关文章推荐
用javascript获得地址栏参数的两种方法
Nov 08 Javascript
JavaScript 闭包深入理解(closure)
May 27 Javascript
基于jquery的分页控件(C#)
Jan 06 Javascript
javaScript(JS)替换节点实现思路介绍
Apr 17 Javascript
浅谈页面装载js及性能分析方法
Dec 09 Javascript
jQuery使用hide方法隐藏元素自身用法实例
Mar 30 Javascript
javascript为按钮注册回车事件(设置默认按钮)的方法
May 09 Javascript
c#程序员对TypeScript的认识过程
Jun 19 Javascript
JQuery form表单提交前验证单选框是否选中、删除记录时验证经验总结(整理)
Jun 09 jQuery
js中this的指向问题归纳总结
Nov 28 Javascript
node.js命令行教程图文详解
May 27 Javascript
JavaScript前后端JSON使用方法教程
Nov 23 Javascript
vuex实现像调用模板方法一样调用Mutations方法
Nov 06 #Javascript
vuex actions异步修改状态的实例详解
Nov 06 #Javascript
Windows上node.js的多版本管理工具用法实例分析
Nov 06 #Javascript
vue限制输入框只能输入8位整数和2位小数的代码
Nov 06 #Javascript
vuex存值与取值的实例
Nov 06 #Javascript
node省市区三级数据性能测评实例分析
Nov 06 #Javascript
vue计算属性无法监听到数组内部变化的解决方案
Nov 06 #Javascript
You might like
将数组写入txt文件 var_export
2009/04/21 PHP
PHP中的正则表达式函数介绍
2012/02/27 PHP
利用curl 多线程 模拟 并发的详解
2013/06/14 PHP
php使用curl并发减少后端访问时间的方法分析
2016/05/12 PHP
php Session无效分析资料整理
2016/11/29 PHP
PHP filesize函数用法浅析
2019/02/15 PHP
解决tp5在nginx下修改配置访问的问题
2019/10/16 PHP
JavaScript 指导方针
2007/04/05 Javascript
js循环改变div颜色具体方法
2013/06/25 Javascript
jQuery取得select选择的文本与值的示例
2013/12/09 Javascript
js取模(求余数)隔行变色
2014/05/15 Javascript
node.js中的定时器nextTick()和setImmediate()区别分析
2014/11/26 Javascript
自己封装的常用javascript函数分享
2015/01/07 Javascript
Java中Timer的用法详解
2015/10/21 Javascript
详解获取jq ul第一个li定位的四种解决方案
2016/11/23 Javascript
laravel5.4+vue+element简单搭建的示例代码
2017/08/29 Javascript
JS库中的Particles.js在vue上的运用案例分析
2017/09/13 Javascript
jQuery动态添加li标签并添加属性和绑定事件方法
2018/02/24 jQuery
Vue 实现手动刷新组件的方法
2019/02/19 Javascript
小程序云开发如何实现图片上传及发表文字
2019/05/17 Javascript
vue实现页面内容禁止选中功能,仅输入框和文本域可选
2019/11/09 Javascript
微信小程序按顺序同步执行的两种方式
2019/12/20 Javascript
vue全局使用axios的操作
2020/09/08 Javascript
[01:08:30]DOTA2-DPC中国联赛 正赛 Ehome vs Elephant BO3 第一场 2月28日
2021/03/11 DOTA
Python爬虫DNS解析缓存方法实例分析
2017/06/02 Python
Python通过属性手段实现只允许调用一次的示例讲解
2018/04/21 Python
python中in在list和dict中查找效率的对比分析
2018/05/04 Python
Django之Mode的外键自关联和引用未定义的Model方法
2018/12/15 Python
Anaconda之conda常用命令介绍(安装、更新、删除)
2019/10/06 Python
详解Python 中的 defaultdict 数据类型
2021/02/22 Python
Mio Skincare中文官网:肌肤和身体护理
2016/10/26 全球购物
美国知名保健品网站:LuckyVitamin(支持中文)
2017/08/09 全球购物
2014年会计个人工作总结
2014/11/24 职场文书
老干部局2015年度工作总结
2015/10/22 职场文书
小学班级标语口号大全
2015/12/26 职场文书
《槐乡的孩子》教学反思
2016/02/20 职场文书