node解析修改nginx配置文件操作實例分析
本文實例講述了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,通過數(shù)組下標(biāo)的方式訪問 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的用途是當(dāng)配置改變時不寫到磁盤中 conf.die('/etc/nginx.conf'); conf.nginx.events.connections._value = 2000; //change remains local, not in /etc/nginx.conf // 將內(nèi)存中的配置寫到另一個文件中 conf.live('/etc/nginx.conf.bak'); // 強行將內(nèi)存中的配置刷到磁盤中 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] //如果只有一個帶有名稱的指令,會被被展開成一個屬性,通過數(shù)組下表訪問的將是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; } */ // 存在多個模塊是通過數(shù)組方式訪問 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配置上的注釋,以數(shù)組的方式返回 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程序設(shè)計有所幫助。
- Node.JS段點續(xù)傳:Nginx配置文件分段下載功能的實現(xiàn)方法
- Nginx做NodeJS應(yīng)用負(fù)載均衡配置實例
- Nginx+SSL+Node.js運行環(huán)境配置教程
- 為Node.js程序配置使用Nginx服務(wù)器的簡明教程
- Node.js站點使用Nginx作反向代理時配置GZip壓縮的教程
- 使用Node.js配合Nginx實現(xiàn)高負(fù)載網(wǎng)絡(luò)
- 為高負(fù)載網(wǎng)絡(luò)優(yōu)化Nginx和Node.js的方法
- 用Nginx反向代理Node.js的方法
- 利用nginx + node在阿里云部署https的步驟詳解
- 利用Nginx實現(xiàn)反向代理Node.js的方法詳解
相關(guān)文章
Node.js對MongoDB數(shù)據(jù)庫實現(xiàn)模糊查詢的方法
模糊查詢是數(shù)據(jù)庫的基本操作之一,下面這篇文章主要給大家介紹了利用Node.js對MongoDB數(shù)據(jù)庫實現(xiàn)模糊查詢的方法教程,文中給出了詳細(xì)的介紹和示例代碼,對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。2017-05-05node靜態(tài)服務(wù)器實現(xiàn)靜態(tài)讀取文件或文件夾
這篇文章主要介紹了node靜態(tài)服務(wù)器實現(xiàn)靜態(tài)讀取文件或文件夾,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12詳解Node.js amqplib 連接 Rabbit MQ最佳實踐
這篇文章主要介紹了詳解Node.js amqplib 連接 Rabbit MQ最佳實踐,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01node?NPM庫glob通配符匹配文件名minimatch模式匹配字符串學(xué)習(xí)
這篇文章主要為大家介紹了node?NPM庫glob通配符匹配文件名minimatch模式匹配字符串學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07Node.js發(fā)起HTTP請求的6種不同方法小結(jié)
本文主要介紹了Node.js發(fā)起HTTP請求的6種不同方法小結(jié),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03