nginx-discovery
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:
[]
= "0.1"
Parse a Configuration
use parse;
let config = r#"
server {
listen 80;
server_name example.com;
location / {
root /var/www/html;
}
}
"#;
let parsed = parse?;
println!;
Extract Access Logs
use ;
let config = parse?;
let logs = access_logs?;
for log in logs
Extract Log Formats
use ;
let config = parse?;
let formats = log_formats?;
for format in formats
π Examples
Check out the examples/ directory:
lex_config.rs- Tokenize NGINX configsextract_logs.rs- Extract log configurations
Run an example:
π― 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 Lexer;
let mut lexer = new;
let tokens = lexer.tokenize?;
Mid-Level: Parser
use parse;
let config = parse?;
for directive in &config.directives
High-Level: Extractors
use ;
let config = parse?;
let logs = access_logs?;
π Documentation
π§ͺ Testing
The library has comprehensive test coverage:
# Run all tests
# Run with output
# Run specific test
π§ Feature Flags
[]
= { = "0.1", = ["serde"] }
Available features:
serde- Serialize/deserialize AST typessystem(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
# Run tests
# Run examples
# Format code
# Run linter
π License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
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
- Author: Ajit Kumar
- GitHub: @urwithajit9
- Issues: GitHub Issues
Star β this repo if you find it useful!