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

本文实例讲述了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,通过数组下标的方式访问
 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', '{\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配置上的注释,以数组的方式返回
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程序设计有所帮助。

(0)

相关推荐

  • 使用Node.js配合Nginx实现高负载网络

    在搭建高吞吐量web应用这个议题上,NginX和Node.js可谓是天生一对.他们都是基于事件驱动模型而设计,可以轻易突破Apache等传统web服务器的C10K瓶颈.预设的配置已经可以获得很高的并发,不过,要是大家想在廉价硬件上做到每秒数千以上的请求,还是有一些工作要做的. 这篇文章假定读者们使用NginX的HttpProxyModule来为上游的node.js服务器充当反向代理.我们将介绍Ubuntu 10.04以上系统sysctl的调优,以及node.js应用与NginX的调优.当然,如果

  • 用Nginx反向代理Node.js的方法

    本文介绍了用Nginx反向代理Node.js的方法,分享给大家,具体如下: 安装pm2 npm install pm2 -g ln -s /home/download/node-v8.11.1-linux-x64/lib/node_modules/pm2/bin/pm2 /usr/local/bin/pm2 修改package.json "scripts": { "test": "echo \"Error: no test specified\&

  • Node.JS段点续传:Nginx配置文件分段下载功能的实现方法

    Html5 提供了一个新的 Range 标签来实现文件的分段下载.在Node.JS中可以配置这个标签来实现文件的分段下载. Header标签 请求 Request Header: 下载 3744 以后的文件内容 range: bytes=3744- 返回 Response Header: 文件总长 15522643 个字节 accept-ranges': 'bytes' content-range': 'bytes */15522643' Nginx配置 首先要配置Nginx支持range标签返

  • 为高负载网络优化Nginx和Node.js的方法

    在搭建高吞吐量web应用这个议题上,NginX和Node.js可谓是天生一对.他们都是基于事件驱动模型而设计,可以轻易突破 Apache等传统web服务器的C10K瓶颈.预设的配置已经可以获得很高的并发,不过,要是大家想在廉价硬件上做到每秒数千以上的请求,还是有一些工作要做的. 这篇文章假定读者们使用NginX的HttpProxyModule来为上游的node.js服务器充当反向代理.我们将介绍Ubuntu 10.04以上系统sysctl的调优,以及node.js应用与NginX的调优.当然,如

  • 为Node.js程序配置使用Nginx服务器的简明教程

    Node.js是一个基于Chrome JavaScript运行时建立的平台, 用于方便地搭建响应速度快.易于扩展的网络应用.Node.js 使用事件驱动, 非阻塞I/O 模型而得以轻量和高效,非常适合在分布式设备上运行的数据密集型的实时应用,如实时聊天等等.然而对于gzip编码,静态文件,HTTP缓存,SSL处理,负载平衡和反向代理等,都可以通过nginx来完成,从而减小node.js的负载,并通过nginx强大的缓存来节省网站的流量从而提高网站的加载速度. 流程图 nginx配置如下: htt

  • Node.js站点使用Nginx作反向代理时配置GZip压缩的教程

    node.js 开发的站点,如果你也是用了nginx实现反向代理. 那么在服务端可以轻松实现 gzip 压缩,让站点浏览更顺畅. 前提条件: node.js + nginx 反向代理. node.js 需要做的工作: express 4.0以下版本: app.use(express.compress()); //主要是这句 app.use(express.json()); app.use(express.urlencoded()); app.use(express.bodyParser());

  • 利用Nginx实现反向代理Node.js的方法详解

    前言 公司有项目前端是用node.js进行服务器渲染,然后再返回给浏览器,进而解决单页面的SEO问题.项目部署的时候,使用Nginx反向代理Node.js.具体的步骤如下: (Nginx.Node.js的安装和基本配置直接跳过) 首先我们要在nginx.cnf文件中的http节点打开下面的配置: http { log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_by

  • Nginx+SSL+Node.js运行环境配置教程

    Nginx是一款高性能的HTTP服务器,同时也是一款高效的反向代理服务器.不像传统的服务器,Nginx是基于事件驱动的异步架构,内存占用少但是性能很好.如果你的Web应用是基于Node.js的,那么建议你考虑使用Nginx来做反向代理,因为Nginx可以非常高效地提供静态文件服务.本文的主要内容是在不同的操作系统下配置Nginx和SSL,并且搭建一个Node.js运行环境. 安装Nginx 假设你已经在服务器上安装了Node.js,下面我们来安装Nginx. 在Mac系统上安装Nginx 利用c

  • Nginx做NodeJS应用负载均衡配置实例

    负载均衡可以把用户的请求分摊到多个服务器上进行处理,从而实现了对海量用户的访问支持.负载均衡的架构如图所示: 对于复杂的Web应用来说,用Nginx做前端负载均衡是理所当然的事. 下面,我们用Nginx做NodeJS应用的负载均衡. 1.配置Nginx 修改nginx.conf: upstream sample { server 127.0.0.1:3000; server 127.0.0.1:3001; keepalive 64; } server { listen 80; .... serv

  • 利用nginx + node在阿里云部署https的步骤详解

    缘起 最近在写node+mongodb版本的灵犀微商城,所以免不了要自己去部署自己的https证书到阿里服务器,下面将实现的过程完整的给大家总结下,话不多说了,来一起看看详细的介绍吧. HTTPS和HTTP的区别主要如下: 1.https协议需要到ca申请证书,一般免费证书较少,因而需要一定费用. 2.http是超文本传输协议,信息是明文传输,https则是具有安全性的ssl加密传输协议. 3.http和https使用的是完全不同的连接方式,用的端口也不一样,前者是80,后者是443. 4.ht

随机推荐