node解析修改nginx配置文件操作實(shí)例分析
本文實(shí)例講述了node解析修改nginx配置文件操作。分享給大家供大家參考,具體如下:
主要是通過(guò)nginx-conf這個(gè)工具。
git地址:https://github.com/tmont/nginx-conf
具體用法:
npm install -S nginx-conf 安裝工具
var NginxConfFile = require('nginx-conf').NginxConfFile; // 這個(gè)api提供了node讀寫(xiě)conf文件的功能 NginxConfFile.create('/etc/nginx.conf', function(err, conf) { if (err) { console.log(err); return; } // 通過(guò)_value的方式讀取每一個(gè)配置的值 console.log(conf.nginx.user._value); //www www console.log(conf.nginx.http.server.listen._value); //one.example.com //模塊中有多個(gè)子模塊,比如server中配置了多個(gè)location,通過(guò)數(shù)組下標(biāo)的方式訪問(wèn) console.log(conf.nginx.http.server.location[3].root._value); // /spool/www //修改配置 //create api是同步修改文件的,對(duì)于配置的修改和刪除會(huì)同步反映到磁盤(pán)中 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; // 這個(gè)api的用途是當(dāng)配置改變時(shí)不寫(xiě)到磁盤(pán)中 conf.die('/etc/nginx.conf'); conf.nginx.events.connections._value = 2000; //change remains local, not in /etc/nginx.conf // 將內(nèi)存中的配置寫(xiě)到另一個(gè)文件中 conf.live('/etc/nginx.conf.bak'); // 強(qiáng)行將內(nèi)存中的配置刷到磁盤(pán)中 conf.flush(); // 增加和移除指令 通過(guò) _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] //如果只有一個(gè)帶有名稱的指令,會(huì)被被展開(kāi)成一個(gè)屬性,通過(guò)數(shù)組下表訪問(wèn)的將是undefined console.log(conf.nginx.http.add_header._value); //X-Load-Balancer lb-01 console.log(conf.nginx.http.add_header[0]); //undefined // 增加一個(gè)新的模塊 conf.nginx.http._add('server'); conf.nginx.http.server._add('listen', '80'); //that'll create something like this: /* server { listen 80; } */ // 存在多個(gè)模塊是通過(guò)數(shù)組方式訪問(wèn) 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\ }'); });
此工具同樣支持對(duì)注釋的修改
// 讀取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
希望本文所述對(duì)大家node.js程序設(shè)計(jì)有所幫助。
- Node.JS段點(diǎn)續(xù)傳:Nginx配置文件分段下載功能的實(shí)現(xiàn)方法
- Nginx做NodeJS應(yīng)用負(fù)載均衡配置實(shí)例
- Nginx+SSL+Node.js運(yùn)行環(huán)境配置教程
- 為Node.js程序配置使用Nginx服務(wù)器的簡(jiǎn)明教程
- Node.js站點(diǎn)使用Nginx作反向代理時(shí)配置GZip壓縮的教程
- 使用Node.js配合Nginx實(shí)現(xiàn)高負(fù)載網(wǎng)絡(luò)
- 為高負(fù)載網(wǎng)絡(luò)優(yōu)化Nginx和Node.js的方法
- 用Nginx反向代理Node.js的方法
- 利用nginx + node在阿里云部署https的步驟詳解
- 利用Nginx實(shí)現(xiàn)反向代理Node.js的方法詳解
相關(guān)文章
Node.js對(duì)MongoDB數(shù)據(jù)庫(kù)實(shí)現(xiàn)模糊查詢的方法
模糊查詢是數(shù)據(jù)庫(kù)的基本操作之一,下面這篇文章主要給大家介紹了利用Node.js對(duì)MongoDB數(shù)據(jù)庫(kù)實(shí)現(xiàn)模糊查詢的方法教程,文中給出了詳細(xì)的介紹和示例代碼,對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-05-05node靜態(tài)服務(wù)器實(shí)現(xiàn)靜態(tài)讀取文件或文件夾
這篇文章主要介紹了node靜態(tài)服務(wù)器實(shí)現(xiàn)靜態(tài)讀取文件或文件夾,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12詳解Node.js amqplib 連接 Rabbit MQ最佳實(shí)踐
這篇文章主要介紹了詳解Node.js amqplib 連接 Rabbit MQ最佳實(shí)踐,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-01-01node?NPM庫(kù)glob通配符匹配文件名minimatch模式匹配字符串學(xué)習(xí)
這篇文章主要為大家介紹了node?NPM庫(kù)glob通配符匹配文件名minimatch模式匹配字符串學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07nodejs和npm版本不匹配報(bào)錯(cuò)的解決方法
當(dāng)公司要求使用固定nodejs的版本時(shí),自己不小心更新了npm,就會(huì)導(dǎo)致npm和nodejs不匹配,下面這篇文章主要給大家介紹了關(guān)于nodejs和npm版本不匹配報(bào)錯(cuò)的解決方法,需要的朋友可以參考下2023-04-04Public?Npm?Registry模塊使用方式實(shí)例
這篇文章主要為大家介紹了Public?Npm?Registry的使用方式示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Node.js發(fā)起HTTP請(qǐng)求的6種不同方法小結(jié)
本文主要介紹了Node.js發(fā)起HTTP請(qǐng)求的6種不同方法小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03