欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

node解析修改nginx配置文件操作實(shí)例分析

 更新時(shí)間:2019年11月06日 10:29:55   作者:蒼青浪  
這篇文章主要介紹了node解析修改nginx配置文件操作,結(jié)合實(shí)例形式分析了node.js使用nginx-conf解析修改nginx配置文件的相關(guān)操作技巧,需要的朋友可以參考下

本文實(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ì)有所幫助。

相關(guān)文章

  • Node.js對(duì)MongoDB數(shù)據(jù)庫(kù)實(shí)現(xià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-05
  • node靜態(tài)服務(wù)器實(shí)現(xiàn)靜態(tài)讀取文件或文件夾

    node靜態(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
  • nvm使用和nodejs切換版本詳細(xì)圖文教程

    nvm使用和nodejs切換版本詳細(xì)圖文教程

    nvm 是node version manager(node 版本管理工具)的縮寫(xiě),是一個(gè)命令行工具,用于管理和切換到不同版本的 node.js,下面這篇文章主要給大家介紹了關(guān)于nvm使用和nodejs切換版本的相關(guān)資料,需要的朋友可以參考下
    2024-04-04
  • 詳解Node.js amqplib 連接 Rabbit MQ最佳實(shí)踐

    詳解Node.js amqplib 連接 Rabbit MQ最佳實(shí)踐

    這篇文章主要介紹了詳解Node.js amqplib 連接 Rabbit MQ最佳實(shí)踐,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • node?NPM庫(kù)glob通配符匹配文件名minimatch模式匹配字符串學(xué)習(xí)

    node?NPM庫(kù)glob通配符匹配文件名minimatch模式匹配字符串學(xué)習(xí)

    這篇文章主要為大家介紹了node?NPM庫(kù)glob通配符匹配文件名minimatch模式匹配字符串學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-07-07
  • Node.js插件安裝圖文教程

    Node.js插件安裝圖文教程

    Node.js是一個(gè)基于Chrome JavaScript運(yùn)行時(shí)建立的平臺(tái), 用于方便地搭建響應(yīng)速度快、易于擴(kuò)展的網(wǎng)絡(luò)應(yīng)用。本文給大家介紹Node.js插件安裝的教程,非常實(shí)用,特此分享給大家,需要的朋友一起學(xué)習(xí)吧
    2016-05-05
  • nodejs和npm版本不匹配報(bào)錯(cuò)的解決方法

    nodejs和npm版本不匹配報(bào)錯(cuò)的解決方法

    當(dāng)公司要求使用固定nodejs的版本時(shí),自己不小心更新了npm,就會(huì)導(dǎo)致npm和nodejs不匹配,下面這篇文章主要給大家介紹了關(guān)于nodejs和npm版本不匹配報(bào)錯(cuò)的解決方法,需要的朋友可以參考下
    2023-04-04
  • Public?Npm?Registry模塊使用方式實(shí)例

    Public?Npm?Registry模塊使用方式實(shí)例

    這篇文章主要為大家介紹了Public?Npm?Registry的使用方式示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • node解析修改nginx配置文件操作實(shí)例分析

    node解析修改nginx配置文件操作實(shí)例分析

    這篇文章主要介紹了node解析修改nginx配置文件操作,結(jié)合實(shí)例形式分析了node.js使用nginx-conf解析修改nginx配置文件的相關(guān)操作技巧,需要的朋友可以參考下
    2019-11-11
  • Node.js發(fā)起HTTP請(qǐng)求的6種不同方法小結(jié)

    Node.js發(fā)起HTTP請(qǐng)求的6種不同方法小結(jié)

    本文主要介紹了Node.js發(fā)起HTTP請(qǐng)求的6種不同方法小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03

最新評(píng)論