Nginx配置文件操作神器(依赖nginxparser)

近期项目nginx负载均衡,需要对nginx的配置文件nginx.conf中的Listener和Member进行增删改的操作。找到了一个神器完美的解决了对配置文件的操作,并且以后写其他文件时也可以使用。
1,可以在github上找到nginx-java-parser工具,项目地址:https://github.com/odiszapc/nginx-java-parser, 解析nginx.conf过程可以参考该项目的README.md
2,更简便的方法是此工具在maven仓库中有依赖,可以直接把依赖引用的项目中

<!-- https://mvnrepository.com/artifact/com.github.odiszapc/nginxparser -->
<dependency>
    <groupId>com.github.odiszapc</groupId>
    <artifactId>nginxparser</artifactId>
    <version>0.9.3</version>
</dependency>

以下详细说明此依赖如何使用

http {
  server {
    listen 80;
    server_name www.nginx.cn;
    location / {
      proxy_pass http://www.nginx.cn;
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
  }
}

这是一个最简单的nginx.conf配置文件,先来分析它的结构。
首先,我们想向一个文件写入数据,需要知道配置文件的地址和名称,读这个配置文件
NgxConfig ngxConfig = NgxConfig.read(配置文件地址+配置文件名称);
其次,我们看一下这个配置文件的结构 最外层是http{} 里边又分两层server{} upstream{}
创建时如果创建的数据里边还包括其他内容,也就是以数据+{}的形式
NgxBlock ngxBlockWeb = new NgxBlock();
如果创建的数据不再包含其他数据,只是一个简单的数据如: listen 80;
NgxParam ngxParam = new NgxParam();
最后我们把整个操作都操作完了,需要把数据写到配置文件中
NgxDumper ngxDumper = new NgxDumper(ngxConfig);
ngxDumper.dump();

 public boolean addDateToNginxConfig() {
        try {
 				//根据路径读取整个配置文件
            NgxConfig ngxConfig = NgxConfig.read("/etc/nginx/nginx.conf");
 				//找到http{}   因为不是单独的一条数据所以使用findBlock
            NgxBlock ngxBlockHttp = ngxConfig.findBlock("http");
 				//因为server也是一个复杂结构{}  用Block
            NgxBlock ngxBlockWeb = new NgxBlock();
            ngxBlockWeb.addValue("server");
 				//server属于http所以要加入到http里
            ngxBlockHttp.addEntry(ngxBlockWeb);
            
            NgxParam ngxParam = new NgxParam();
            ngxParam.addValue(String.format("Listener %s", ”80“));
            ngxBlockWeb.addEntry(ngxParam);

            ngxParam = new NgxParam();
            ngxParam.addValue(String.format("server_name %s", “www.nginx.cn”));
            ngxBlockWeb.addEntry(ngxParam);
 			
 			//添加location
             NgxBlock ngxBlockLocation = new NgxBlock();
             ngxBlockLocation.addValue("location");
             ngxBlockLocation.addValue("/");

             NgxParam ngxParam = new NgxParam();
             ngxParam.addValue("proxy_pass http://www.nginx.cn");
             ngxBlockLocation.addEntry(ngxParam);

             ngxParam = new NgxParam();
             ngxParam.addValue("proxy_set_header Host $host");
             ngxBlockLocation.addEntry(ngxParam);

             ngxParam = new NgxParam();
             ngxParam.addValue("proxy_set_header X-Real-IP $remote_addr");
             ngxBlockLocation.addEntry(ngxParam);

             ngxParam = new NgxParam();
             ngxParam.addValue("proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for");
             ngxBlockLocation.addEntry(ngxParam);
             ngxBlockWeb.addEntry(ngxBlockLocation);

             String content = new NgxDumper(ngxConfig).dump();
             log.info("{}", content);
            return true;
         } catch (IOException e) {
             log.warn("write nginx.conf to file catch IOException!", e);
         }
         return false;
     }

以上是写入文件,日常使用中还会涉及到查找指定配置项
NgxBlock members = block.findBlock(“server”);
NgxParam bHealthyCheck = block.findParam(“Listener”);
Collection entries = members.getEntries();
ListserverList=ngxHttpBlock.findAll(NgxConfig.BLOCK,“server”);
如何从查找的配置中取值
String state = ngxParam.getValue()
String state = ngxParam.getName()
例如:server_name www.nginx.cn; 这条配置项 空格前边的是name,后边的是value

基本方法原理就这些至于读配置文件的增删改查,自己看一下就明白了。

猜你喜欢

转载自blog.csdn.net/liucy007/article/details/88579986