pub struct NginxDiscovery { /* private fields */ }Expand description
High-level NGINX configuration discovery
Provides convenient methods to discover and analyze NGINX configurations.
Implementations§
Source§impl NginxDiscovery
impl NginxDiscovery
Sourcepub fn from_config_text(text: &str) -> Result<Self>
pub fn from_config_text(text: &str) -> Result<Self>
Sourcepub fn from_config_file(path: impl AsRef<Path>) -> Result<Self>
pub fn from_config_file(path: impl AsRef<Path>) -> Result<Self>
Create a discovery instance from a configuration file
§Arguments
path- Path to the NGINX configuration file
§Errors
Returns an error if:
- The file cannot be read
- The configuration cannot be parsed
§Examples
use nginx_discovery::NginxDiscovery;
let discovery = NginxDiscovery::from_config_file("/etc/nginx/nginx.conf")?;Sourcepub fn from_running_instance() -> Result<Self>
Available on crate feature system only.
pub fn from_running_instance() -> Result<Self>
system only.Create a discovery instance from a running NGINX instance
This attempts to:
- Find the nginx binary
- Run
nginx -Tto dump the configuration - Parse the dumped configuration
§Errors
Returns an error if:
- nginx binary cannot be found
- nginx -T fails to execute
- The configuration cannot be parsed
- Insufficient permissions to run nginx -T
§Examples
use nginx_discovery::NginxDiscovery;
let discovery = NginxDiscovery::from_running_instance()?;
let logs = discovery.access_logs();Sourcepub fn access_logs(&self) -> Vec<AccessLog>
pub fn access_logs(&self) -> Vec<AccessLog>
Get all access log configurations
Returns all access_log directives found in the configuration,
including those in http, server, and location contexts.
§Examples
use nginx_discovery::NginxDiscovery;
let config = r"
http {
access_log /var/log/nginx/access.log;
server {
access_log /var/log/nginx/server.log;
}
}
";
let discovery = NginxDiscovery::from_config_text(config)?;
let logs = discovery.access_logs();
assert_eq!(logs.len(), 2);Sourcepub fn log_formats(&self) -> Vec<LogFormat>
pub fn log_formats(&self) -> Vec<LogFormat>
Get all log format definitions
Returns all log_format directives found in the configuration.
§Examples
use nginx_discovery::NginxDiscovery;
let config = r"
log_format combined '$remote_addr - $remote_user [$time_local]';
log_format main '$request $status';
";
let discovery = NginxDiscovery::from_config_text(config)?;
let formats = discovery.log_formats();
assert_eq!(formats.len(), 2);Sourcepub fn all_log_files(&self) -> Vec<PathBuf>
pub fn all_log_files(&self) -> Vec<PathBuf>
Get all log file paths (access logs only)
Returns a deduplicated list of all access log file paths.
§Examples
use nginx_discovery::NginxDiscovery;
let config = r"
access_log /var/log/nginx/access.log;
access_log /var/log/nginx/access.log; # duplicate
access_log /var/log/nginx/other.log;
";
let discovery = NginxDiscovery::from_config_text(config)?;
let files = discovery.all_log_files();
assert_eq!(files.len(), 2); // deduplicatedSourcepub fn server_names(&self) -> Vec<String>
pub fn server_names(&self) -> Vec<String>
Get all server names from server blocks
Returns a list of all server names defined in server blocks.
§Examples
use nginx_discovery::NginxDiscovery;
let config = r"
server {
server_name example.com www.example.com;
}
server {
server_name test.com;
}
";
let discovery = NginxDiscovery::from_config_text(config)?;
let names = discovery.server_names();
assert_eq!(names.len(), 3);Sourcepub fn config(&self) -> &Config
pub fn config(&self) -> &Config
Get the parsed configuration AST
Provides direct access to the parsed configuration for custom processing.
§Examples
use nginx_discovery::NginxDiscovery;
let config = "user nginx;";
let discovery = NginxDiscovery::from_config_text(config)?;
let ast = discovery.config();
assert_eq!(ast.directives.len(), 1);Sourcepub fn config_path(&self) -> Option<&Path>
pub fn config_path(&self) -> Option<&Path>
Get the configuration file path (if loaded from file)
§Examples
use nginx_discovery::NginxDiscovery;
let discovery = NginxDiscovery::from_config_file("/etc/nginx/nginx.conf")?;
assert!(discovery.config_path().is_some());Sourcepub fn summary(&self) -> String
pub fn summary(&self) -> String
Generate a summary of the configuration
Returns a human-readable summary including:
- Number of directives
- Number of server blocks
- Number of access logs
- Number of log formats
§Examples
use nginx_discovery::NginxDiscovery;
let config = r"
user nginx;
access_log /var/log/nginx/access.log;
server { listen 80; }
";
let discovery = NginxDiscovery::from_config_text(config)?;
let summary = discovery.summary();
assert!(summary.contains("directives"));Sourcepub fn servers(&self) -> Vec<Server>
pub fn servers(&self) -> Vec<Server>
Get all server blocks
Returns all server blocks found in the configuration.
§Examples
use nginx_discovery::NginxDiscovery;
let config = r"
server {
listen 80;
server_name example.com;
}
";
let discovery = NginxDiscovery::from_config_text(config)?;
let servers = discovery.servers();
assert_eq!(servers.len(), 1);Sourcepub fn listening_ports(&self) -> Vec<u16>
pub fn listening_ports(&self) -> Vec<u16>
Get all listening ports
Returns a deduplicated list of all ports that servers are listening on.
§Examples
use nginx_discovery::NginxDiscovery;
let config = r"
server {
listen 80;
listen 443 ssl;
}
";
let discovery = NginxDiscovery::from_config_text(config)?;
let ports = discovery.listening_ports();
assert!(ports.contains(&80));
assert!(ports.contains(&443));Sourcepub fn ssl_servers(&self) -> Vec<Server>
pub fn ssl_servers(&self) -> Vec<Server>
Get all SSL-enabled servers
Returns servers that have SSL configured.
§Examples
use nginx_discovery::NginxDiscovery;
let config = r"
server {
listen 80;
server_name example.com;
}
server {
listen 443 ssl;
server_name secure.example.com;
}
";
let discovery = NginxDiscovery::from_config_text(config)?;
let ssl_servers = discovery.ssl_servers();
assert_eq!(ssl_servers.len(), 1);Sourcepub fn proxy_locations(&self) -> Vec<Location>
pub fn proxy_locations(&self) -> Vec<Location>
Get all proxy locations
Returns all location blocks that have proxy_pass configured.
§Examples
use nginx_discovery::NginxDiscovery;
let config = r"
server {
location / {
root /var/www;
}
location /api {
proxy_pass http://backend;
}
}
";
let discovery = NginxDiscovery::from_config_text(config)?;
let proxies = discovery.proxy_locations();
assert_eq!(proxies.len(), 1);Sourcepub fn location_count(&self) -> usize
pub fn location_count(&self) -> usize
Count total number of location blocks
Trait Implementations§
Source§impl Clone for NginxDiscovery
impl Clone for NginxDiscovery
Source§fn clone(&self) -> NginxDiscovery
fn clone(&self) -> NginxDiscovery
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for NginxDiscovery
impl RefUnwindSafe for NginxDiscovery
impl Send for NginxDiscovery
impl Sync for NginxDiscovery
impl Unpin for NginxDiscovery
impl UnwindSafe for NginxDiscovery
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more