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 相关文章推荐
jquery实现动态菜单的实例代码
Nov 28 Javascript
获取鼠标在div中的相对位置的实现代码
Dec 30 Javascript
jquery实现select选中行、列合计示例
Apr 25 Javascript
jquery实现多行文字图片滚动效果示例代码
Oct 10 Javascript
javascript实现tab切换的两个实例
Nov 05 Javascript
JavaScript简单实现弹出拖拽窗口(一)
Jun 17 Javascript
微信小程序 监听手势滑动切换页面实例详解
Jun 15 Javascript
Angular 2 利用Router事件和Title实现动态页面标题的方法
Aug 23 Javascript
vue element-ui table表格滚动加载方法
Mar 02 Javascript
微信小程序日历/日期选择插件使用方法详解
Dec 28 Javascript
mock.js实现模拟生成假数据功能示例
Jan 15 Javascript
React Native 混合开发多入口加载方式详解
Sep 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
php用数组返回无限分类的列表数据的代码
2010/08/08 PHP
第4章 数据处理-php字符串的处理-郑阿奇(续)
2011/07/04 PHP
PHP中的替代语法简介
2014/08/22 PHP
php版阿里大于(阿里大鱼)短信发送实例详解
2016/11/30 PHP
用JavaScript玩转游戏物理(一)运动学模拟与粒子系统
2010/06/19 Javascript
javascript倒计时功能实现代码
2012/06/07 Javascript
ExtJS自定义主题(theme)样式详解
2013/11/18 Javascript
分享js粘帖屏幕截图到web页面插件screenshot-paste
2020/08/21 Javascript
JS 终止执行的实现方法
2016/11/24 Javascript
JavaScript数据结构之二叉树的删除算法示例
2017/04/13 Javascript
通过V8源码看一个关于JS数组排序的诡异问题
2017/08/14 Javascript
vue实现点击图片放大效果
2017/08/15 Javascript
vue 添加vux的代码讲解
2017/11/30 Javascript
Vux+Axios拦截器增加loading的问题及实现方法
2018/11/08 Javascript
JavaScript查看代码运行效率console.time()与console.timeEnd()用法
2019/01/18 Javascript
JS实现的冒泡排序,快速排序,插入排序算法示例
2019/03/02 Javascript
javascript中的offsetWidth、clientWidth、innerWidth及相关属性方法
2020/05/14 Javascript
Javascript如何实现扩充基本类型
2020/08/26 Javascript
Python线程的两种编程方式
2015/04/14 Python
Python实现合并两个列表的方法分析
2018/05/28 Python
pytorch逐元素比较tensor大小实例
2020/01/03 Python
Python尾递归优化实现代码及原理详解
2020/10/09 Python
用python制作个音乐下载器
2021/01/30 Python
Python3+Appium安装及Appium模拟微信登录方法详解
2021/02/16 Python
Python爬虫制作翻译程序的示例代码
2021/02/22 Python
完美解决IE8下不兼容rgba()的问题
2017/03/31 HTML / CSS
产假请假条
2014/04/10 职场文书
比赛口号大全
2014/06/10 职场文书
交通运输局四风问题对照检查材料思想汇报
2014/10/09 职场文书
2014年财务工作总结与计划
2014/12/08 职场文书
党课主持词大全
2015/06/30 职场文书
二十年同学聚会致辞
2015/07/28 职场文书
学术会议开幕词
2016/03/03 职场文书
2019年让高校“心动”的自荐信
2019/03/25 职场文书
python批量更改目录名/文件名的方法
2021/04/18 Python
python 实现德洛内三角剖分的操作
2021/04/22 Python