node解析修改ngix配置文件

主要是經過nginx-conf這個工具。node

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

具體用法:git

npm install -S nginx-conf 安裝工具github

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\ }'); });

 

此工具一樣支持對註釋的修改npm

// 讀取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

注意特殊狀況api

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
相關文章
相關標籤/搜索