经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » Nginx » 查看文章
node解析修改nginx配置文件操作实例分析
来源:jb51  时间:2019/11/6 11:42:37  对本文有异议

本文实例讲述了node解析修改nginx配置文件操作。分享给大家供大家参考,具体如下:

主要是通过nginx-conf这个工具。

git地址:https://github.com/tmont/nginx-conf

具体用法:

npm install -S nginx-conf 安装工具

  1. var NginxConfFile = require('nginx-conf').NginxConfFile;
  2. // 这个api提供了node读写conf文件的功能
  3. NginxConfFile.create('/etc/nginx.conf', function(err, conf) {
  4. if (err) {
  5. console.log(err);
  6. return;
  7. }
  8. // 通过_value的方式读取每一个配置的值
  9. console.log(conf.nginx.user._value); //www www
  10. console.log(conf.nginx.http.server.listen._value); //one.example.com
  11. //模块中有多个子模块,比如server中配置了多个location,通过数组下标的方式访问
  12. console.log(conf.nginx.http.server.location[3].root._value); // /spool/www
  13. //修改配置
  14. //create api是同步修改文件的,对于配置的修改和删除会同步反映到磁盘中
  15. conf.on('flushed', function() {
  16. console.log('finished writing to disk');
  17. });
  18. //listen to the flushed event to determine when the new file has been flushed to disk
  19. conf.nginx.events.connections._value = 1000;
  20. // 这个api的用途是当配置改变时不写到磁盘中
  21. conf.die('/etc/nginx.conf');
  22. conf.nginx.events.connections._value = 2000; //change remains local, not in /etc/nginx.conf
  23. // 将内存中的配置写到另一个文件中
  24. conf.live('/etc/nginx.conf.bak');
  25. // 强行将内存中的配置刷到磁盘中
  26. conf.flush();
  27. // 增加和移除指令 通过 _add 和 _remove
  28. conf.nginx.http._add('add_header', 'Cache-Control max-age=315360000, public');
  29. console.log(conf.nginx.http.add_header._value); //Cache-Control max-age=315360000, public
  30. conf.nginx.http._add('add_header', 'X-Load-Balancer lb-01');
  31. conf.nginx.http._add('add_header', 'X-Secure true');
  32. console.log(conf.nginx.http.add_header[0]._value); //Cache-Control max-age=315360000, public
  33. console.log(conf.nginx.http.add_header[1]._value); //X-Load-Balancer lb-01
  34. console.log(conf.nginx.http.add_header[2]._value); //X-Secure true
  35. conf.nginx.http._remove('add_header'); //removes add_header[0]
  36. conf.nginx.http._remove('add_header', 1); //removes add_header[1]
  37. //如果只有一个带有名称的指令,会被被展开成一个属性,通过数组下表访问的将是undefined
  38. console.log(conf.nginx.http.add_header._value); //X-Load-Balancer lb-01
  39. console.log(conf.nginx.http.add_header[0]); //undefined
  40. // 增加一个新的模块
  41. conf.nginx.http._add('server');
  42. conf.nginx.http.server._add('listen', '80');
  43. //that'll create something like this:
  44. /*
  45. server {
  46. listen 80;
  47. }
  48. */
  49. // 存在多个模块是通过数组方式访问
  50. conf.nginx.http._add('server');
  51. conf.nginx.http.server[1]._add('listen', '443');
  52. /*
  53. server {
  54. listen 80;
  55. }
  56. server {
  57. listen 443;
  58. }
  59. */
  60. // blocks with values:
  61. conf.nginx.http.server[1]._add('location', '/');
  62. conf.nginx.http.server[1].location._add('root', '/var/www/example.com');
  63. /*
  64. server {
  65. location / {
  66. root /var/www/example.com;
  67. }
  68. }
  69. */
  70. // lua blocks also work, but you can't put a mismatched "{" or "}" in a comment!
  71. 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}');
  72. });
  73.  

此工具同样支持对注释的修改

  1. // 读取use配置上的注释,以数组的方式返回
  2. console.log(conf.nginx.events.use._comments.length); // 1
  3. console.log(conf.nginx.events.use._comments[0]); // use [ kqueue | rtsig | epoll | /dev/poll | select | poll ];
  4. // 删除注释
  5. conf.nginx.events.use._comments.splice(0, 1);
  6. // 添加注释
  7. conf.nginx.event.use._comments.push('my new comment');
  8. console.log(conf.nginx.events.use._comments.length); // 1
  9. console.log(conf.nginx.events.use._comments[0]); //my new comment
  10. // 修改注释
  11. conf.nginx.event.use._comments[0] = 'updated';
  12. console.log(conf.nginx.events.use._comments[0]); //updated
  13.  

注意特殊情况

  1. foo #comment
  2. bar;
  3. console.log(conf.nginx.foo._value); //bar
  4. console.log(conf.nginx.foo._comments[0]); //comment
  5. But if the comment comes after:
  6. foo bar;
  7. #comment
  8. console.log(conf.nginx.foo._value); //bar
  9. console.log(conf.nginx.foo._comments.length); //0
  10.  

希望本文所述对大家node.js程序设计有所帮助。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号