最近工程开发遇到一个需求:用Java去解析并编辑nginx.conf
解析nginx.conf过程可以参考该项目的README.md
下面举个列子说明一下该如何编辑nginx.conf。
定义一个pojo
importcom.alibaba.fastjson.JSONArray;importcom.google.common.base.Strings;importlombok.Data;
@Datapublic classWebHost {private longid;privateString host;privateString protocol;privateString name;
}
1、添加nginx配置
1 public booleanaddWebToNginxConfig(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 = newNgxBlock();9 ngxBlockWeb.addValue("server");10 ngxBlockHttp.addEntry(ngxBlockWeb);11 if ("https".equals(webHost.getProtocol())) {12 NgxParam ngxParam = newNgxParam();13 ngxParam.addValue("listen 443 ssl");14 ngxBlockWeb.addEntry(ngxParam);15 ngxParam = newNgxParam();16 ngxParam.addValue(String.format("server_name %s", webHost.getHost()));17 ngxBlockWeb.addEntry(ngxParam);18
19 String virtualServerName =webHost.formatName();20 ngxParam = newNgxParam();21 ngxParam.addValue(String.format("ssl_certificate /etc/nginx/cert/%s.cert", virtualServerName));22 ngxBlockWeb.addEntry(ngxParam);23 ngxParam = newNgxParam();24 ngxParam.addValue(String.format("ssl_certificate_key /etc/nginx/key/%s.key", virtualServerName));25 ngxBlockWeb.addEntry(ngxParam);26 } else{27 NgxParam ngxParam = newNgxParam();28 ngxParam.addValue("listen 80");29 ngxBlockWeb.addEntry(ngxParam);30 ngxParam = newNgxParam();31 ngxParam.addValue(String.format("server_name %s", webHost.getHost()));32 ngxBlockWeb.addEntry(ngxParam);33 }34
35 NgxBlock ngxBlockLocation = newNgxBlock();36 ngxBlockLocation.addValue("location");37 ngxBlockLocation.addValue("/");38 NgxParam ngxParam = newNgxParam();39 ngxParam.addValue("proxy_pass http://backend_http");40 ngxBlockLocation.addEntry(ngxParam);41 ngxParam = newNgxParam();42 ngxParam.addValue("proxy_set_header Host $host");43 ngxBlockLocation.addEntry(ngxParam);44 ngxParam = newNgxParam();45 ngxParam.addValue("proxy_set_header X-Real-IP $remote_addr");46 ngxBlockLocation.addEntry(ngxParam);47 ngxParam = newNgxParam();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 = newNgxDumper(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 booleandeleteWebFromNginxConfig(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 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 = newNgxDumper(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 }