Nginx configuration parser helps you to parse, edit, regenerate your nginx config in your go applications
Find a file
Tufan Barış Yıldırım 8e17ce617d
Implement server helper API for lookup/listen/location management (#78)
Add server helper APIs for name, listen, and locations
2026-02-20 11:15:09 +03:00
.github/workflows enhance: lua code parse and format dump (#68) 2025-04-29 21:02:29 +03:00
config Implement server helper API for lookup/listen/location management (#78) 2026-02-20 11:15:09 +03:00
dumper Add optional and pluggable Lua formatting for DumpLuaBlock (#77) 2026-02-20 10:44:00 +03:00
examples #54 Keep parameters in their relative lines when in multi-line configuration. (#61) 2024-12-20 13:03:56 +03:00
full-example rework variable vs keyword 2023-05-08 14:25:08 +03:00
parser Skip Hidden Files in include Glob (#67) 2026-02-20 11:08:50 +03:00
testdata enhance: lua code parse and format dump (#68) 2025-04-29 21:02:29 +03:00
todo Improve parser/dumper safety, determinism, and API hardening (#74) 2026-02-20 10:20:38 +03:00
.gitignore enhance: lua code parse and format dump (#68) 2025-04-29 21:02:29 +03:00
AGENTS.md Add AGENTS guide (#69) 2025-06-20 11:44:59 +03:00
CODE_OF_CONDUCT.md readme,licence and coc 2020-04-11 19:39:42 +03:00
CONTRIBUTING.md Improve parser/dumper safety, determinism, and API hardening (#74) 2026-02-20 10:20:38 +03:00
go.mod enhance: lua code parse and format dump (#68) 2025-04-29 21:02:29 +03:00
go.sum enhance: lua code parse and format dump (#68) 2025-04-29 21:02:29 +03:00
gopher.png gopher 2020-04-17 23:31:26 +03:00
GUIDE.md Implement server helper API for lookup/listen/location management (#78) 2026-02-20 11:15:09 +03:00
LICENSE Initial commit 2020-04-05 14:42:20 +03:00
Makefile Code has been restructured and task/todo has been completed (#46) 2024-04-19 15:33:06 +03:00
README.md Improve parser/dumper safety, determinism, and API hardening (#74) 2026-02-20 10:20:38 +03:00

Report Card Actions Status

Gonginx

TBH, I would like to rewrite the parser next time I need it again :)) but it still does its job.

Gonginx is an Nginx configuration parser helps you to parse, edit, regenerate your nginx config files in your go applications. It makes managing your balancer configurations easier.

Basic grammar of an nginx config file


%token Keyword Variable BlockStart BlockEnd Semicolon Regex

%%

config      :  /* empty */ 
            | config directives
            ;
block       : BlockStart directives BlockEnd
            ;
directives  : directives directive
            ;
directive   : Keyword [parameters] (semicolon|block)
            ;
parameters  : parameters keyword
            ;
keyword     : Keyword 
            | Variable 
            | Regex
            ;

API

Core Components

  • Parser

    Parser is the main package that analyzes and turns nginx structred files into objects. It basically has 3 libraries, lexer explodes it into tokens and parser turns tokens into config objects which are in their own package,
  • Config

    Config package is representation of any context, directive or their parameters in golang. So basically they are models and also AST
  • Dumper

    Dumper is the package that holds styling configuration only.

Examples

Examples and Library Reference

TODO

  • associate comments with config objects to print them on config generation and make it configurable with dumper.Style
  • move any context wrapper into their own file (remove from parser)
  • Parse included files recusively, keep relative path on load, save all in a related structure and make that optional in dumper.Style
  • Implement specific searches, like finding servers by server_name (domain) or any upstream by target etc.
  • add more examples
  • link the parent directive to any directive for easier manipulation

Limitations

There is no known limitations yet. PRs are more than welcome if you want to implement a specific directive / block, please read Contributing before your first PR.

Behavior Notes

  • Parser APIs return errors for malformed input; malformed configs should not crash your process.
  • Include parsing (parser.WithIncludeParsing()) deduplicates includes by canonical path and skips cyclic include branches by default.
  • Use parser.WithIncludeCycleErr() to fail fast on include-cycle detection.
  • dumper.NoIndentSortedStyle / sorted dump options no longer mutate directive order in the in-memory AST.
  • Parent semantics:
    • root-level leaf directives have nil parent,
    • nested directives point to their enclosing directive wrapper (http, server, location, etc.).
  • Upstream lookup:
    • FindUpstreams() is permissive and skips unexpected upstream directive types.
    • FindUpstreamsStrict() returns a typed error when a matched upstream is not *config.Upstream.
  • Lua comment style policy:
    • existing -- comments stay --,
    • nginx-style # comments are preserved where originally used and safe,
    • formatter failures fall back to original Lua code.

License

MIT License