nginx-discovery 0.1.0

Discover and introspect NGINX configurations with ease
Documentation

nginx-discovery

Crates.io Documentation License Build Status

Discover and parse NGINX configurations with ease.

A Rust library for parsing, analyzing, and extracting information from NGINX configuration files. Perfect for building tools that need to understand NGINX configs programmatically.

✨ Features

  • πŸ” Parse NGINX Configs - Full support for directives, blocks, and nested structures
  • πŸ“Š Extract Information - High-level extractors for logs, servers, and more
  • 🎯 Type-Safe - Strongly-typed AST and configuration objects
  • ⚑ Fast - Efficient lexer and parser with zero-copy where possible
  • πŸ› οΈ Great Errors - Detailed error messages with source locations and suggestions
  • πŸ“š Well Documented - Comprehensive docs and examples

πŸš€ Quick Start

Add to your Cargo.toml:

[dependencies]
nginx-discovery = "0.1"

Parse a Configuration

use nginx_discovery::parse;

let config = r#"
server {
    listen 80;
    server_name example.com;

    location / {
        root /var/www/html;
    }
}
"#;

let parsed = parse(config)?;
println!("Found {} directives", parsed.directives.len());

Extract Access Logs

use nginx_discovery::{parse, extract};

let config = parse(config_text)?;
let logs = extract::access_logs(&config)?;

for log in logs {
    println!("Log: {}", log.path.display());
    println!("Format: {:?}", log.format_name);
    println!("Context: {:?}", log.context);
}

Extract Log Formats

use nginx_discovery::{parse, extract};

let config = parse(config_text)?;
let formats = extract::log_formats(&config)?;

for format in formats {
    println!("Format: {}", format.name());
    println!("Variables: {:?}", format.variables());
}

πŸ“– Examples

Check out the examples/ directory:

Run an example:

cargo run --example extract_logs

🎯 Use Cases

  • Log Analysis Tools - Extract log paths and formats for Fluentd, Vector, Logstash
  • Configuration Management - Validate and analyze NGINX configs
  • Monitoring Setup - Discover upstreams and servers for monitoring
  • Migration Tools - Parse existing configs for migration planning
  • Documentation - Auto-generate documentation from configs
  • Security Auditing - Analyze SSL/TLS and security settings

πŸ—οΈ Architecture

The library is organized in layers:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   High-Level API (extract::*)      β”‚  ← Extract logs, servers, etc.
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚   Parser (parse)                    β”‚  ← Convert tokens to AST
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚   Lexer (tokenize)                  β”‚  ← Convert text to tokens
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚   AST Types                         β”‚  ← Type-safe representation
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Low-Level: Lexer

use nginx_discovery::parser::Lexer;

let mut lexer = Lexer::new("server { listen 80; }");
let tokens = lexer.tokenize()?;

Mid-Level: Parser

use nginx_discovery::parse;

let config = parse("server { listen 80; }")?;
for directive in &config.directives {
    println!("{}", directive.name());
}

High-Level: Extractors

use nginx_discovery::{parse, extract};

let config = parse(config_text)?;
let logs = extract::access_logs(&config)?;

πŸ“š Documentation

πŸ§ͺ Testing

The library has comprehensive test coverage:

# Run all tests
cargo test --all-features

# Run with output
cargo test -- --nocapture

# Run specific test
cargo test test_name

πŸ”§ Feature Flags

[dependencies]
nginx-discovery = { version = "0.1", features = ["serde"] }

Available features:

  • serde - Serialize/deserialize AST types
  • system (default) - System interaction utilities

πŸ“Š Supported NGINX Directives (v0.1.0)

Currently supports:

  • βœ… Simple directives: user nginx;
  • βœ… Block directives: server { ... }
  • βœ… Nested blocks: http { server { location { } } }
  • βœ… Quoted strings: "value" and 'value'
  • βœ… Variables: $host, ${variable}
  • βœ… Numbers: 80, 443, 1024
  • βœ… Comments: # comment
  • βœ… Log formats and access logs

πŸ—ΊοΈ Roadmap

v0.2.0 (Planned)

  • Server block extractor
  • Upstream extractor
  • SSL/TLS configuration extractor
  • Include directive resolution

v0.3.0 (Planned)

  • Map directive support
  • Geo/GeoIP support
  • Rate limiting configuration
  • Auth configuration extractor

v1.0.0 (Future)

  • Complete NGINX directive support
  • Configuration validation
  • Config transformation tools

🀝 Contributing

Contributions are welcome! Please read CONTRIBUTING.md for details.

Development

git clone https://github.com/urwithajit9/nginx-discovery.git
cd nginx-discovery

# Run tests
cargo test --all-features

# Run examples
cargo run --example extract_logs

# Format code
cargo fmt

# Run linter
cargo clippy --all-features -- -D warnings

πŸ“ License

Licensed under either of:

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

πŸ™ Acknowledgments

Built with ❀️ in Rust.

Special thanks to the Rust community for excellent parser libraries and documentation.

πŸ“¬ Contact


Star ⭐ this repo if you find it useful!