主要是通过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); //
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); //
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); //

node解析修改ngix配置文件的更多相关文章

  1. Golang读取并修改非主流配置文件

    今天工作中碰到的问题,要求修改此配置文件,没看出来是什么格式,用了下面的思路: mysql { # If any of the files below are set, TLS encryption ...

  2. 并发之AQS原理(二) CLH队列与Node解析

    并发之AQS原理(二) CLH队列与Node解析 1.CLH队列与Node节点 就像通常医院看病排队一样,医生一次能看的病人数量有限,那么超出医生看病速度之外的病人就要排队. 一条队列是队列中每一个人 ...

  3. asp.net中为什么修改了配置文件后我们不需要重启IIS

    本文转载:http://blog.itpub.net/12639172/viewspace-659819/ 大家知道,asp.net中,如果我们修改了配置文件只要把它保存之后,就会立刻反应到程序中, ...

  4. Spark添加/更改集群节点需要修改的配置文件

    笔记:在配置好了spark后,如果需要添加/删除一个结点需要修改如下配置文件 cd $HADOOP/etc/hadoop 进入hadoop配置文件夹下 修改 slaves,将对应的节点添加/删除 修改 ...

  5. sublime text3修改默认配置文件是失败的解决方法

    如果你修改sublime text3的默认配置文件Preferences.sublime-settings失败,现实的错误信息如下图: 其实根据提示信息就好找问题出在哪里了:权限 要想成功的修改默认配 ...

  6. Linux中使用sed命令或awk命令修改常规配置文件

    一.方案: Linux中使用sed命令或awk命令修改常规配置文件 二.步骤: 1.假设有一个a.txt,内容如下: #!/bin/bash aa= bbb= ccc= #ddd= 2.如果想要把里面 ...

  7. AWS中国EC2 公网IP登录免pemKEY修改shh 配置文件

    个人使用记录 1:KEY 授权 chmod 400 VPN.pem 2:连接 ssh -i "VPN.pem" ubuntu@ec2-54-183-119-93.us-west-1 ...

  8. docker下修改mysql配置文件

    原文:docker下修改mysql配置文件 版权声明:本文为博主原创文章,转载注明地址:http://blog.csdn.net/wang704987562 https://blog.csdn.net ...

  9. 修改gitlab配置文件指定服务器ip和自定义端口:

    修改gitlab配置文件指定服务器ip和自定义端口: vim /etc/gitlab/gitlab.rb gitlab-ctl reconfiguregitlab-ctl restart 查看与rpm ...

随机推荐

  1. Java自学-数字与字符串 装箱和拆箱

    Java中基本类型的装箱和拆箱 步骤 1 : 封装类 所有的基本类型,都有对应的类类型 比如int对应的类是Integer 这种类就叫做封装类 package digit; public class ...

  2. Vue3.0报错error: Unexpected console statement (no-console) 解决办法

    写项目过程中用ESLint遵守代码规范很有必要,但是对于一些规范也很是无语,比如:‘Unexpected console statement (no-console)’,连console都不能用,这就 ...

  3. vim替换字符串

    1. s 命令来替换字符串 :s/vivian/sky/ #替换当前行第一个 vivian 为 sky :s/vivian/sky/g #替换当前行所有 vivian 为 sky :n,$s/vivi ...

  4. Linux chage命令详解

    原文 chage命令用于密码实效管理,该是用来修改帐号和密码的有效期限,接下来通过本文给大家介绍Linux chage命令相关知识,本文介绍的非常详细,具有参考借鉴价值,感兴趣的朋友一起学习吧 lin ...

  5. rsync异常处理

    实验环境: centos7.6 实验目的:    错误的思考,在目标端执行rsync拉取源端文件,源端也必须存在rsync命令,目的用于差异比对实现增量传输! 01.执行rsync命令不存在 02.执 ...

  6. docker指定固定IP创建容器

    问题描述: docker默认使用的docker0,子网172.17.0.0/16,不支持指定IP启动容器 问题解决: 新增网段bridge指定别的网段即可 01.docker新增network 新建n ...

  7. Odoo销售模块

    转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10825988.html 一:销售模块 销售模块的用途: 1)管理销售团队.销售人员:维护销售产品: 2)管理 ...

  8. Windows & Ubuntu 双系统完美卸载Ubuntu(不残留,无污染)

    双系统卸载Ubuntu时,如若直接从Windows磁盘管理里格式化Ubuntu分区,由于Ubuntu的引导盘的原因,会导致电脑启动时出现问题,所以不建议这样的操作. 卸载Ubuntu前需要区分BIOS ...

  9. 黄金矿工(LeetCode Medium难度)1129题 题解(DFS)

    题目描述: 给定一个二维网络,给定任意起点与终点.每一步可以往4个方向走.要找出黄金最多的一条线路. 很明显的是要“一条路走到黑,一直下去直到某个条件停止”. 运用dfs(深度优先搜索)求解. 因为起 ...

  10. 一文弄懂Pytorch的DataLoader, DataSet, Sampler之间的关系

    以下内容都是针对Pytorch 1.0-1.1介绍. 很多文章都是从Dataset等对象自下往上进行介绍,但是对于初学者而言,其实这并不好理解,因为有的时候会不自觉地陷入到一些细枝末节中去,而不能把握 ...