Code Monkey home page Code Monkey logo

nginxconfigparser's Introduction

NGINX configuration file parser and builder

A .net standard library for reading and writing nginx configuration files.

Quick start

Install nuget package NuGet

Create new file

NginxConfig.Create()
    .AddOrUpdate("http:server:listen", "80")
    .AddOrUpdate("http:server:root", "/var/wwwroot")
    // add location
    .AddOrUpdate("http:server:location", "/", true, "default")
    .AddOrUpdate("http:server:location:root", "/app1")
    // add location
    .AddOrUpdate("http:server:location[1]", "~ ^/(images|javascript|js|css|flash|media|static)/", true)
    .AddOrUpdate("http:server:location[1]:root", "/app2")
    .AddOrUpdate("http:server:location[1]:expires", "1d")
    // add location
    .AddOrUpdate("http:server:location[2]", "~/api", true, "api")
    .AddOrUpdate("http:server:location[2]:proxy_pass", "http://server.com")
    // save file
    .Save("temp2.conf");

The temp2.conf file content :

http   {

  server   {
    listen  80;
    root  /var/wwwroot;

    location  / { # default
      root  /app1;
    }

    location  ~ ^/(images|javascript|js|css|flash|media|static)/ {
      root  /app2;
      expires  1d;
    }

    location  ~/api { # api
      proxy_pass  http://server.com;
    }

  }
}

Read exist file

// load from file
var config = NginxConfig.LoadFrom("nginx.conf");

// read single value
Console.WriteLine(config.GetToken("worker_processes")); // read the root key 'worker_processes'
Console.WriteLine(config["http:log_format"])); // read the key 'log_format' from http section
Console.WriteLine(config["http:server[2]:root"])); // read the key 'root' from second http server

// read multi values
// read all values by key 'include' from http section
config.GetTokens("http:include").ToList().ForEach(item =>
{
    Console.WriteLine(item);
});

// read all group section
Console.WriteLine(config.GetGroup("http"));  // get all from group key 'http'

// add or update value
config.AddOrUpdate("http:root", "/var/wwwroot");
config.AddOrUpdate("http:server:root", "/var/wwwroot/server1");
config.AddOrUpdate("http:server[2]:root", "/var/wwwroot/server2");

// add group
config.AddOrUpdate("http:server:location", "/api", true, comment: "new location");
// add value to group
config.AddOrUpdate("http:server:location:root", "/var/wwwroot/api");
Console.WriteLine(config["http:server:location:root"]);

// delete value
config.Remove("http:server:access_log"); // remove by key
config.Remove("http:server:location"); // remove the location section

// save to file
config.Save("nginx.conf");
// or get file content
string configContent = config.ToString();

Buy Me A Coffee

"Buy Me A Coffee"

nginxconfigparser's People

Contributors

jxnkwlp avatar mrdoger avatar imgbotapp avatar

Stargazers

 avatar 9kb avatar  avatar 书越 avatar Denis Ivanov avatar  avatar Dmitry avatar Sammo Gabay avatar Gerard Smit avatar  avatar Flithor avatar Pwnint32 avatar 小◕黑◕贝 avatar yesicoo avatar KnigthtKeepers avatar  avatar  avatar  avatar

Watchers

James Cloos avatar  avatar  avatar

nginxconfigparser's Issues

How to add parameters with conditional expressions

Hello
Thank you very much for writing the Nginx parser, I would like to ask if I can use this tool to write configuration files with conditional expressions?
For example, I would like to write the following.

if($time_iso8061 ~ '(\d{4}-\d{2}-\d{2})'){
set $time $1;
}
access_log log/aceess-$time.log;

How should I write the code please

How to add a group value

How does one add a group value?

For example a location block can specify a route like /api

location /api {

}

I couldn't find a way to do this so I added a bit of code to allow one to create a group and add a value afterwards, I can create a pull request for this if you'd like

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.