# nginx-discovery
[](https://crates.io/crates/nginx-discovery)
[](https://docs.rs/nginx-discovery)
[](https://github.com/urwithajit9/nginx-discovery#license)
[](https://github.com/urwithajit9/nginx-discovery/actions)
**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`:
```toml
[dependencies]
nginx-discovery = "0.1"
```
### Parse a Configuration
```rust
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
```rust
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
```rust
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/`](examples/) directory:
- [`lex_config.rs`](examples/lex_config.rs) - Tokenize NGINX configs
- [`extract_logs.rs`](examples/extract_logs.rs) - Extract log configurations
Run an example:
```bash
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
```rust
use nginx_discovery::parser::Lexer;
let mut lexer = Lexer::new("server { listen 80; }");
let tokens = lexer.tokenize()?;
```
### Mid-Level: Parser
```rust
use nginx_discovery::parse;
let config = parse("server { listen 80; }")?;
for directive in &config.directives {
println!("{}", directive.name());
}
```
### High-Level: Extractors
```rust
use nginx_discovery::{parse, extract};
let config = parse(config_text)?;
let logs = extract::access_logs(&config)?;
```
### Extract Servers
```rust
use nginx_discovery::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 = NginxDiscovery::from_config_text(config)?;
// Get all servers
let servers = discovery.servers();
println!("Found {} servers", servers.len());
// Get SSL servers only
let ssl_servers = discovery.ssl_servers();
println!("SSL servers: {}", ssl_servers.len());
// Get all listening ports
let ports = discovery.listening_ports();
println!("Listening on ports: {:?}", ports);
// Get proxy locations
let proxies = discovery.proxy_locations();
for location in proxies {
println!("Proxy: {} -> {:?}", location.path, location.proxy_pass);
}
```
## π Documentation
- [API Documentation](https://docs.rs/nginx-discovery)
- [Examples](examples/)
- [Contributing Guide](CONTRIBUTING.md)
## π§ͺ Testing
The library has comprehensive test coverage:
```bash
# Run all tests
cargo test --all-features
# Run with output
cargo test -- --nocapture
# Run specific test
cargo test test_name
```
## π§ Feature Flags
```toml
[dependencies]
nginx-discovery = { version = "0.1", features = ["serde"] }
```
Available features:
- `serde` - Serialize/deserialize AST types
- `system` (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
### v0.2.0 (Released)
- β
Server block extractor
- β
Location block extractor
- β
SSL/TLS server detection
- β
Proxy location detection
### v0.3.0 (Planned)
- [ ] Upstream extractor
- [ ] Map directive support
- [ ] Geo/GeoIP support
- [ ] Include directive resolution
- [ ] 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](CONTRIBUTING.md) for details.
### Development
```bash
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:
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license ([LICENSE-MIT](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](https://github.com/urwithajit9)
- **Issues**: [GitHub Issues](https://github.com/urwithajit9/nginx-discovery/issues)
---
**Star β this repo if you find it useful!**