JAVA parsing, editing nginx.conf

Source: Internet
Author: User
Tags webhost

Recently, engineering development has encountered a need to parse and edit nginx.conf in Java

Find the Nginx-java-parser tool on GitHub, project address: Https://github.com/odiszapc/nginx-java-parser

The parsing nginx.conf process can refer to the project's README.MD

Here's an example to illustrate how to edit nginx.conf.

Define a Pojo

Import Com.alibaba.fastjson.JSONArray; Import com.google.common.base.Strings; Import Lombok. Data; @Datapublicclass  webhost {    privatelong  ID;     Private  String host;     Private String protocol;     Private String name;}

1. Add Nginx Configuration

1      Public BooleanAddwebtonginxconfig (webhost webhost, String fileName) {2         if(webhost = =NULL||Strings.isnullorempty (FileName)) {3             return false;4         }5         Try {6Ngxconfig Ngxconfig =Ngxconfig.read (fileName);7Ngxblock ngxblockhttp = Ngxconfig.findblock ("http");8Ngxblock Ngxblockweb =NewNgxblock ();9Ngxblockweb.addvalue ("Server");Ten ngxblockhttp.addentry (ngxblockweb); One             if("https". Equals (Webhost.getprotocol ())) { ANgxparam Ngxparam =NewNgxparam (); -Ngxparam.addvalue ("Listen 443 SSL"); - ngxblockweb.addentry (ngxparam); theNgxparam =NewNgxparam (); -Ngxparam.addvalue (String.Format ("server_name%s", Webhost.gethost ())); - ngxblockweb.addentry (ngxparam); -  +String VirtualServerName =webhost.formatname (); -Ngxparam =NewNgxparam (); +Ngxparam.addvalue (String.Format ("Ssl_certificate/etc/nginx/cert/%s.cert", VirtualServerName)); A ngxblockweb.addentry (ngxparam); atNgxparam =NewNgxparam (); -Ngxparam.addvalue (String.Format ("Ssl_certificate_key/etc/nginx/key/%s.key", VirtualServerName)); - ngxblockweb.addentry (ngxparam); -}Else { -Ngxparam Ngxparam =NewNgxparam (); -Ngxparam.addvalue ("Listen 80"); in ngxblockweb.addentry (ngxparam); -Ngxparam =NewNgxparam (); toNgxparam.addvalue (String.Format ("server_name%s", Webhost.gethost ())); + ngxblockweb.addentry (ngxparam); -             } the  *Ngxblock ngxblocklocation =NewNgxblock (); $Ngxblocklocation.addvalue ("Location");Panax NotoginsengNgxblocklocation.addvalue ("/"); -Ngxparam Ngxparam =NewNgxparam (); theNgxparam.addvalue ("Proxy_pass http://backend_http"); + ngxblocklocation.addentry (ngxparam); ANgxparam =NewNgxparam (); theNgxparam.addvalue ("Proxy_set_header Host $host"); + ngxblocklocation.addentry (ngxparam); -Ngxparam =NewNgxparam (); $Ngxparam.addvalue ("Proxy_set_header x-real-ip $remote _addr"); $ ngxblocklocation.addentry (ngxparam); -Ngxparam =NewNgxparam (); -Ngxparam.addvalue ("Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for"); the ngxblocklocation.addentry (ngxparam); - ngxblockweb.addentry (ngxblocklocation);Wuyi  theString content =NewNgxdumper (ngxconfig). dump (); -Log.info ("{}", content); Wu             return true; -}Catch(IOException e) { AboutLog.warn ("Write nginx.conf to file catch ioexception!", e); $         } -         return false; -}

Examples of adding results:

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 "" "$htt  P_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. Delete Nginx configuration

1      Public BooleanDeletewebfromnginxconfig (string host, String fileName) {2         if(Strings.isnullorempty (host) | |Strings.isnullorempty (FileName)) {3             return false;4         }5         Try {6Ngxconfig Ngxconfig =Ngxconfig.read (fileName);7Ngxblock ngxblockhttp = Ngxconfig.findblock ("http");8list<ngxentry> serverlist = Ngxblockhttp.findall (ngxconfig.block, "Server");9              for(Ngxentry ngxentry:serverlist) {TenNgxblock Ngxblock =(Ngxblock) ngxentry; OneNgxparam Ngxparam = Ngxblock.findparam ("SERVER_NAME"); A                 if(Host.equals (Ngxparam.getvalue ())) { - Ngxblockhttp.remove (ngxblock); -                 } the             } -String content =NewNgxdumper (ngxconfig). dump (); -Log.info ("{}", content); -             return true; +}Catch(IOException e) { -Log.warn ("Write nginx.conf to file catch ioexception!", e); +         } A         return false; at}

JAVA parsing, editing nginx.conf

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.