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 相关文章推荐
15 个 JavaScript Web UI 库
May 19 Javascript
javascript中indexOf技术详解
May 07 Javascript
JavaScript生成福利彩票双色球号码
May 15 Javascript
如何使用jquery easyui创建标签组件
Nov 18 Javascript
详解Javascript继承的实现
Mar 25 Javascript
Bootstrap CSS组件之导航(nav)
Dec 17 Javascript
vue mintui-Loadmore结合实现下拉刷新和上拉加载示例
Oct 12 Javascript
vue+express 构建后台管理系统的示例代码
Jul 19 Javascript
React+Webpack快速上手指南(小结)
Aug 15 Javascript
个人小程序接入支付解决方案
May 23 Javascript
微信小程序基于movable-view实现滑动删除效果
Jan 08 Javascript
vue项目创建步骤及路由router
Jan 14 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
Sony CFR 320 修复改造
2020/03/14 无线电
PHP遍历文件夹与文件类及处理类用法实例
2014/09/23 PHP
php基于PDO实现功能强大的MYSQL封装类实例
2017/02/27 PHP
laravel框架添加数据,显示数据,返回成功值的方法
2019/10/11 PHP
jquery 批量上传图片实现代码
2010/01/28 Javascript
JavaScript等比例缩放图片控制超出范围的图片
2013/08/06 Javascript
导航跟随滚动条置顶移动示例代码
2013/09/11 Javascript
jquery插件lazyload.js延迟加载图片的使用方法
2014/02/19 Javascript
jquery.validate.js插件使用经验记录
2014/07/02 Javascript
IE下支持文本框和密码框placeholder效果的JQuery插件分享
2015/01/31 Javascript
Javascript 正则表达式校验数字的简单实例
2016/11/02 Javascript
深入学习nodejs中的async模块的使用方法
2017/07/12 NodeJs
基于React+Redux的SSR实现方法
2018/07/03 Javascript
微信小程序实现运动步数排行功能(可删除)
2018/07/05 Javascript
vue3.0 CLI - 3.2 路由的初级使用教程
2018/09/20 Javascript
koa大型web项目中使用路由装饰器的方法示例
2019/04/02 Javascript
Vue程序调试的方法
2019/06/17 Javascript
在vue中高德地图引入和轨迹的绘制的实现
2019/10/11 Javascript
微信小程序 (地址选择1)--选取搜索地点并显示效果
2019/12/17 Javascript
基于vue.js实现购物车
2020/01/15 Javascript
重命名批处理python脚本
2013/04/05 Python
Python中的super()方法使用简介
2015/08/14 Python
5种Python单例模式的实现方式
2016/01/14 Python
Linux 发邮件磁盘空间监控(python)
2016/04/23 Python
python中dict字典的查询键值对 遍历 排序 创建 访问 更新 删除基础操作方法
2018/09/13 Python
记一次python 内存泄漏问题及解决过程
2018/11/29 Python
在Pandas中DataFrame数据合并,连接(concat,merge,join)的实例
2019/01/29 Python
python实现诗歌游戏(类继承)
2019/02/26 Python
python实现简单聊天室功能 可以私聊
2019/07/12 Python
pytorch数据预处理错误的解决
2020/02/20 Python
ET Mall东森购物网:东森严选
2017/03/06 全球购物
工程测量与监理专业应届生求职信
2013/11/27 职场文书
工程班组长岗位职责
2013/12/30 职场文书
投标担保书范文
2014/04/02 职场文书
2014预防青少年违法犯罪工作总结
2014/12/10 职场文书
2015年财政所工作总结
2015/04/25 职场文书