JAVA 解析编辑nginx.conf
Posted snowater
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA 解析编辑nginx.conf相关的知识,希望对你有一定的参考价值。
最近工程开发遇到一个需求:用Java去解析并编辑nginx.conf
在github上找到nginx-java-parser工具,项目地址:https://github.com/odiszapc/nginx-java-parser
解析nginx.conf过程可以参考该项目的README.md
下面举个列子说明一下该如何编辑nginx.conf。
定义一个pojo
import com.alibaba.fastjson.JSONArray; import com.google.common.base.Strings; import lombok.Data; @Data public class WebHost { private long id; private String host; private String protocol; private String name; }
1、添加nginx配置
1 public boolean addWebToNginxConfig(WebHost webHost, String fileName) { 2 if (webHost == null || Strings.isNullOrEmpty(fileName)) { 3 return false; 4 } 5 try { 6 NgxConfig ngxConfig = NgxConfig.read(fileName); 7 NgxBlock ngxBlockHttp = ngxConfig.findBlock("http"); 8 NgxBlock ngxBlockWeb = new NgxBlock(); 9 ngxBlockWeb.addValue("server"); 10 ngxBlockHttp.addEntry(ngxBlockWeb); 11 if ("https".equals(webHost.getProtocol())) { 12 NgxParam ngxParam = new NgxParam(); 13 ngxParam.addValue("listen 443 ssl"); 14 ngxBlockWeb.addEntry(ngxParam); 15 ngxParam = new NgxParam(); 16 ngxParam.addValue(String.format("server_name %s", webHost.getHost())); 17 ngxBlockWeb.addEntry(ngxParam); 18 19 String virtualServerName = webHost.formatName(); 20 ngxParam = new NgxParam(); 21 ngxParam.addValue(String.format("ssl_certificate /etc/nginx/cert/%s.cert", virtualServerName)); 22 ngxBlockWeb.addEntry(ngxParam); 23 ngxParam = new NgxParam(); 24 ngxParam.addValue(String.format("ssl_certificate_key /etc/nginx/key/%s.key", virtualServerName)); 25 ngxBlockWeb.addEntry(ngxParam); 26 } else { 27 NgxParam ngxParam = new NgxParam(); 28 ngxParam.addValue("listen 80"); 29 ngxBlockWeb.addEntry(ngxParam); 30 ngxParam = new NgxParam(); 31 ngxParam.addValue(String.format("server_name %s", webHost.getHost())); 32 ngxBlockWeb.addEntry(ngxParam); 33 } 34 35 NgxBlock ngxBlockLocation = new NgxBlock(); 36 ngxBlockLocation.addValue("location"); 37 ngxBlockLocation.addValue("/"); 38 NgxParam ngxParam = new NgxParam(); 39 ngxParam.addValue("proxy_pass http://backend_http"); 40 ngxBlockLocation.addEntry(ngxParam); 41 ngxParam = new NgxParam(); 42 ngxParam.addValue("proxy_set_header Host $host"); 43 ngxBlockLocation.addEntry(ngxParam); 44 ngxParam = new NgxParam(); 45 ngxParam.addValue("proxy_set_header X-Real-IP $remote_addr"); 46 ngxBlockLocation.addEntry(ngxParam); 47 ngxParam = new NgxParam(); 48 ngxParam.addValue("proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for"); 49 ngxBlockLocation.addEntry(ngxParam); 50 ngxBlockWeb.addEntry(ngxBlockLocation); 51 52 String content = new NgxDumper(ngxConfig).dump(); 53 log.info("{}", content); 54 return true; 55 } catch (IOException e) { 56 log.warn("write nginx.conf to file catch IOException!", e); 57 } 58 return false; 59 }
添加结果示例:
user nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1025; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main ‘$remote_addr - $remote_user [$time_local] "$request" ‘ ‘$status $body_bytes_sent "$http_referer" ‘ ‘"$http_user_agent" "$http_x_forwarded_for"‘; access_log /var/log/nginx/access.log main; sendfile on; keepalive_timeout 65; #gzip on; upstream backend_http { server 1.1.1.1:80 weight=2; server 2.2.2.2:80 weight=2; } upstream backend_https { server 1.1.1.1:443 weight=2; server 2.2.2.2:443 weight=2; } server { listen 80; server_name aaaa.com; location / { proxy_pass http://backend_http; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } server { listen 443 ssl; server_name eeee.com; ssl_certificate /etc/nginx/cert/eeee.com.https.cert; ssl_certificate_key /etc/nginx/key/eeee.com.https.key; location / { proxy_pass http://backend_http; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } } }
2、删除nginx配置
1 public boolean deleteWebFromNginxConfig(String host, String fileName) { 2 if (Strings.isNullOrEmpty(host) || Strings.isNullOrEmpty(fileName)) { 3 return false; 4 } 5 try { 6 NgxConfig ngxConfig = NgxConfig.read(fileName); 7 NgxBlock ngxBlockHttp = ngxConfig.findBlock("http"); 8 List<NgxEntry> serverList = ngxBlockHttp.findAll(NgxConfig.BLOCK,"server"); 9 for (NgxEntry ngxEntry : serverList) { 10 NgxBlock ngxBlock = (NgxBlock) ngxEntry; 11 NgxParam ngxParam = ngxBlock.findParam("server_name"); 12 if (host.equals(ngxParam.getValue())) { 13 ngxBlockHttp.remove(ngxBlock); 14 } 15 } 16 String content = new NgxDumper(ngxConfig).dump(); 17 log.info("{}", content); 18 return true; 19 } catch (IOException e) { 20 log.warn("write nginx.conf to file catch IOException!", e); 21 } 22 return false; 23 }
以上是关于JAVA 解析编辑nginx.conf的主要内容,如果未能解决你的问题,请参考以下文章
Nginx防盗链Nginx访问控制Nginx解析php相关配置Nginx代理