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
- π₯οΈ Server Block Extraction - Extract and analyze server blocks with listen directives and locations
- π Extract Information - High-level extractors for logs, servers, locations, 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.2"
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 Servers (New in v0.2.0)
use NginxDiscovery;
let config = r#"
server {
listen 80;
listen 443 ssl;
server_name example.com www.example.com;
location / {
root /var/www/html;
}
location /api {
proxy_pass http://backend:3000;
}
}
"#;
let discovery = from_config_text?;
// Get all servers
let servers = discovery.servers;
println!;
// Get SSL servers only
let ssl_servers = discovery.ssl_servers;
println!;
// Get all listening ports
let ports = discovery.listening_ports;
println!;
// Get proxy locations
let proxies = discovery.proxy_locations;
for location in proxies
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:
extract_servers.rs- Extract server blocks and analyze configurationsextract_logs.rs- Extract log configurationsparse_config.rs- Parse NGINX configurationlex_config.rs- Tokenize NGINX configstest_nginx_detection.rs- Test NGINX system detection (requiressystemfeature)
Run an example:
Run the system detection 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
- Service Discovery - Extract server names, ports, and proxy configurations
ποΈ Architecture
The library is organized in layers:
βββββββββββββββββββββββββββββββββββββββ
β High-Level API (NginxDiscovery) β β Convenient discovery methods
βββββββββββββββββββββββββββββββββββββββ€
β Extractors (extract::*) β β Extract servers, logs, 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 servers = servers?;
let logs = access_logs?;
Highest-Level: Discovery API
use NginxDiscovery;
let discovery = from_config_text?;
let ssl_servers = discovery.ssl_servers;
let ports = discovery.listening_ports;
π Documentation
π§ͺ Testing
The library has comprehensive test coverage:
# Run all tests
# Run with output
# Run specific test
π§ Feature Flags
[]
= { = "0.2", = ["serde"] }
Available features:
serde- Serialize/deserialize AST typessystem(default) - System interaction utilities
π Supported NGINX Directives
Currently supports:
- β
Simple directives:
user nginx; - β
Block directives:
server { ... },location { ... } - β
Nested blocks:
http { server { location { } } } - β
Quoted strings:
"value"and'value' - β
Variables:
$host,${variable} - β
Numbers:
80,443,1024 - β
Comments:
# comment - β Log formats and access logs
- β Server blocks with listen directives and locations (v0.2.0)
- β Listen directives with SSL, HTTP/2, HTTP/3 options (v0.2.0)
- β Location blocks with all modifiers (=, ^~, ~, ~*) (v0.2.0)
- β Proxy detection and static file serving (v0.2.0)
πΊοΈ Roadmap
πΊοΈ Roadmap
v0.3.0 (Released)
- β CLI Phase 1 implementation
- β Parse, extract, export, doctor commands
- β SSL/TLS analysis command
- β Security analysis command
- β Interactive mode
- β Enhanced extract commands
v0.4.0 (Planned)
- Export markdown - Documentation generation
- Export fluentd - Fluentd config generation
- Network checks in doctor
- Shell completions
- Enhanced security analysis
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
π₯οΈ Command-Line Interface
nginx-discovery includes a powerful CLI tool for analyzing NGINX configurations.
Installation
Quick Start
# Run health check
# Parse configuration
# Extract servers
# Export to JSON
Documentation
- Complete CLI Guide - Comprehensive documentation
- Quick Reference - Command cheat sheet
Example Usage
# Generate infrastructure inventory
# Security audit
# Service discovery
| \
π 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!