node configuration file parsing modify ngix

Mainly through nginx-conf this tool.

git Address: https://github.com/tmont/nginx-conf

Specific Usage:

npm install -S nginx-conf Installation Tools

var NginxConfFile the require = ( 'Nginx-conf' ) .NginxConfFile;
 
// this api node provides the functionality to read and write files conf NginxConfFile.create (
'/etc/nginx.conf', function (ERR, conf) { IF (ERR ) { the console.log (ERR); return ; } // read the value of each configuration by way _value
the console.log (conf.nginx.user._value); // WWW WWW the console.log (conf.nginx .http.server.listen._value); // one.example.com // multiple submodules, modules, arranged in a plurality of such LOCATION server, accessible via the target array mode console.log (conf.nginx.http .server.location [3] .root._value); // / spool / the WWW // modify the configuration create api // is synchronized to modify files for configuration changes and deletions be synced to disk Conf. on ( 'flushed', function () { the console.log ( 'Writing to Disk Finished' ); }); // the listen to The flushed to the Determine When Event The new new Disk File has been flushed to conf.nginx.events = 1000 .connections._value ; // api this purpose is when the configuration changes are not written to disk conf.die ( '/ etc / nginx.conf' ); conf.nginx.events.connections._value = 2000; / / Change Remains local, Not in /etc/nginx.conf // memory configuration file written to another conf.live ( '/ etc / nginx.conf.bak' ); // force the memory configuration brush disk conf.flush (); // add and remove instructions by _add and _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('the add_header'); // Removes the add_header [0] conf.nginx.http._remove ( 'the add_header',. 1); // Removes the add_header [. 1] // If only one command with a name, is to be expanded an attribute, table access by the array will be undefined the console.log (conf.nginx.http.add_header._value); // X--the Load Balancer LB-01- the console.log (conf.nginx.http.add_header [0 ]); // undefined // Add a new module conf.nginx.http._add ( 'Server' ); conf.nginx.http.server._add ( 'the listen', '80' ); // that'll something like the this Create: / * Server { the listen 80; } * / // multiple modules are accessed by way of an array 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\ }'); });

 

This tool also supports changes to the comments

// read the annotations on the use configuration, so as to return the array 
the console.log (conf.nginx.events.use._comments.length); // . 1 the console.log (conf.nginx.events.use._comments [0 ]); // use [kqueue | rtsig | epoll | / dev / poll | the SELECT | poll]; // Remove the comment conf.nginx.events.use._comments.splice (0, 1 ); // add comments conf. nginx.event.use._comments.push ( 'My new new Comment' ); the console.log (conf.nginx.events.use._comments.length); // . 1 the console.log (conf.nginx.events.use._comments [0]); // My new new comment // Review Notes conf.nginx.event.use._comments [0] = 'Updated' ; the console.log (conf.nginx.events.use._comments [ 0]);//updated

Note the special circumstances

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

 

Guess you like

Origin www.cnblogs.com/cangqinglang/p/11731721.html