node解析修正nginxプロファイル操作例分析


本明細書の例は、nodeの解析的変更nginxプロファイルの動作を説明する。皆さんに参考にしてあげます。具体的には以下の通りです。
主にnginx-confというツールを通じて。
git住所:https://github.com/tmont/nginx-conf
具体的な使い方:
npm install-S inx-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,           
 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
 //                
 conf.live('/etc/nginx.conf.bak');
 //               
 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]
 //             ,          ,           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;
  }
 */
 //                
 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', '{
\ ngx.say("this is a lua block!")
\ res = ngx.location.capture("/memc",
\ { args = { cmd = "incr", key = ngx.var.uri } }
\ )
\ }'); });
このツールはコメントの変更にも対応しています。

//   use      ,        
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プログラムの設計に役に立ちます。