最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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ù)組下標的方式訪問
 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
 // 將內(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)文章

  • 詳解如何用typescript開發(fā)koa2的二三事

    詳解如何用typescript開發(fā)koa2的二三事

    這篇文章主要介紹了詳解如何用typescript開發(fā)koa2的二三事,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • 用Node.JS打造一個惡劣天氣實時預(yù)警系統(tǒng)

    用Node.JS打造一個惡劣天氣實時預(yù)警系統(tǒng)

    本文將從實戰(zhàn)的角度出發(fā),利用NodeJS以及聚合數(shù)據(jù)的第三方免費接口打造一個完整的天氣實時預(yù)警項目系統(tǒng),具有一定的參考價值,感興趣的可以了解一下
    2021-12-12
  • Node.js一行代碼實現(xiàn)靜態(tài)文件服務(wù)器的方法步驟

    Node.js一行代碼實現(xiàn)靜態(tài)文件服務(wù)器的方法步驟

    這篇文章主要介紹了Node.js一行代碼實現(xiàn)靜態(tài)文件服務(wù)器的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Node中的streams流的具體使用

    Node中的streams流的具體使用

    本文主要介紹了Node中的streams流的具體使用,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • 淺談node中的exports與module.exports的關(guān)系

    淺談node中的exports與module.exports的關(guān)系

    本篇文章主要介紹了淺談node中的exports與module.exports的關(guān)系,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • nodejs入門教程六:express模塊用法示例

    nodejs入門教程六:express模塊用法示例

    這篇文章主要介紹了nodejs入門教程之express模塊用法,結(jié)合實例形式分析了express模塊的功能、創(chuàng)建、路由相關(guān)使用技巧,需要的朋友可以參考下
    2017-04-04
  • windows實現(xiàn)npm和cnpm安裝步驟

    windows實現(xiàn)npm和cnpm安裝步驟

    這篇文章主要介紹了windows實現(xiàn)npm和cnpm安裝步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-10-10
  • 利用pm2部署多個node.js項目的配置教程

    利用pm2部署多個node.js項目的配置教程

    目前似乎最常見的線上部署nodejs項目的有forever,pm2這兩種,而下面這篇文章主要給大家介紹了關(guān)于利用pm2部署多個node.js項目的配置教程,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-10-10
  • npm安裝yarn后找不到y(tǒng)arn報錯的解決過程

    npm安裝yarn后找不到y(tǒng)arn報錯的解決過程

    這篇文章主要給大家介紹了關(guān)于npm安裝yarn后找不到y(tǒng)arn報錯的解決過程,文中通過圖文介紹的非常詳細,對遇到同樣問題的同學(xué)具有一定的參考性,需要的朋友可以參考下
    2023-04-04
  • autojs的nodejs打包成品app經(jīng)驗分享

    autojs的nodejs打包成品app經(jīng)驗分享

    這篇文章主要為大家介紹了autojs的nodejs打包成品app經(jīng)驗分享,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01

最新評論

禄劝| 错那县| 道真| 临沂市| 乌苏市| 哈密市| 清苑县| 四子王旗| 固原市| 景谷| 宁蒗| 中宁县| 搜索| 清水河县| 兖州市| 报价| 玛多县| 石楼县| 漾濞| 会宁县| 莫力| 五华县| 洪泽县| 高尔夫| 昭通市| 达州市| 长泰县| 吴桥县| 尼玛县| 张家界市| 巴南区| 广州市| 遵化市| 肥城市| 息烽县| 化隆| 义乌市| 普安县| 赞皇县| 吉安县| 松桃|