JAVA 解析、编辑nginx.conf

时间:2023-03-08 17:54:48
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配置

     public boolean addWebToNginxConfig(WebHost webHost, String fileName) {
if (webHost == null || Strings.isNullOrEmpty(fileName)) {
return false;
}
try {
NgxConfig ngxConfig = NgxConfig.read(fileName);
NgxBlock ngxBlockHttp = ngxConfig.findBlock("http");
NgxBlock ngxBlockWeb = new NgxBlock();
ngxBlockWeb.addValue("server");
ngxBlockHttp.addEntry(ngxBlockWeb);
if ("https".equals(webHost.getProtocol())) {
NgxParam ngxParam = new NgxParam();
ngxParam.addValue("listen 443 ssl");
ngxBlockWeb.addEntry(ngxParam);
ngxParam = new NgxParam();
ngxParam.addValue(String.format("server_name %s", webHost.getHost()));
ngxBlockWeb.addEntry(ngxParam); String virtualServerName = webHost.formatName();
ngxParam = new NgxParam();
ngxParam.addValue(String.format("ssl_certificate /etc/nginx/cert/%s.cert", virtualServerName));
ngxBlockWeb.addEntry(ngxParam);
ngxParam = new NgxParam();
ngxParam.addValue(String.format("ssl_certificate_key /etc/nginx/key/%s.key", virtualServerName));
ngxBlockWeb.addEntry(ngxParam);
} else {
NgxParam ngxParam = new NgxParam();
ngxParam.addValue("listen 80");
ngxBlockWeb.addEntry(ngxParam);
ngxParam = new NgxParam();
ngxParam.addValue(String.format("server_name %s", webHost.getHost()));
ngxBlockWeb.addEntry(ngxParam);
} NgxBlock ngxBlockLocation = new NgxBlock();
ngxBlockLocation.addValue("location");
ngxBlockLocation.addValue("/");
NgxParam ngxParam = new NgxParam();
ngxParam.addValue("proxy_pass http://backend_http");
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;
}

添加结果示例:

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配置

     public boolean deleteWebFromNginxConfig(String host, String fileName) {
if (Strings.isNullOrEmpty(host) || Strings.isNullOrEmpty(fileName)) {
return false;
}
try {
NgxConfig ngxConfig = NgxConfig.read(fileName);
NgxBlock ngxBlockHttp = ngxConfig.findBlock("http");
List<NgxEntry> serverList = ngxBlockHttp.findAll(NgxConfig.BLOCK,"server");
for (NgxEntry ngxEntry : serverList) {
NgxBlock ngxBlock = (NgxBlock) ngxEntry;
NgxParam ngxParam = ngxBlock.findParam("server_name");
if (host.equals(ngxParam.getValue())) {
ngxBlockHttp.remove(ngxBlock);
}
}
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;
}