亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

node解析修改nginx配置文件操作實例分析

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

本文實例講述了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è)計有所幫助。

相關(guān)文章

  • Node.js對MongoDB數(shù)據(jù)庫實現(xiàn)模糊查詢的方法

    Node.js對MongoDB數(shù)據(jù)庫實現(xiàn)模糊查詢的方法

    模糊查詢是數(shù)據(jù)庫的基本操作之一,下面這篇文章主要給大家介紹了利用Node.js對MongoDB數(shù)據(jù)庫實現(xiàn)模糊查詢的方法教程,文中給出了詳細(xì)的介紹和示例代碼,對大家具有一定的參考價值,需要的朋友們下面來一起看看吧。
    2017-05-05
  • node靜態(tài)服務(wù)器實現(xiàn)靜態(tài)讀取文件或文件夾

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

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

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

    詳解Node.js amqplib 連接 Rabbit MQ最佳實踐

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

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

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

    Node.js插件安裝圖文教程

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

    nodejs和npm版本不匹配報錯的解決方法

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

    Public?Npm?Registry模塊使用方式實例

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

    node解析修改nginx配置文件操作實例分析

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

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

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

最新評論