Skip to main content

NginxDiscovery

Struct NginxDiscovery 

Source
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

Source

pub fn from_config_text(text: &str) -> Result<Self>

Create a discovery instance from configuration text

§Arguments
  • text - NGINX configuration as a string
§Errors

Returns an error if the configuration cannot be parsed.

§Examples
use nginx_discovery::NginxDiscovery;

let config = "user nginx;";
let discovery = NginxDiscovery::from_config_text(config)?;
Source

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")?;
Source

pub fn from_running_instance() -> Result<Self>

Available on crate feature system only.

Create a discovery instance from a running NGINX instance

This attempts to:

  1. Find the nginx binary
  2. Run nginx -T to dump the configuration
  3. 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();
Source

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);
Source

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);
Source

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); // deduplicated
Source

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);
Source

pub fn to_json(&self) -> Result<String>

Available on crate feature serde only.

Export configuration to JSON

§Errors

Returns an error if serialization fails.

§Examples
use nginx_discovery::NginxDiscovery;

let config = "user nginx;";
let discovery = NginxDiscovery::from_config_text(config)?;
let json = discovery.to_json()?;
assert!(json.contains("user"));
Source

pub fn to_yaml(&self) -> Result<String>

Available on crate feature serde only.

Export configuration to YAML

§Errors

Returns an error if serialization fails.

§Examples
use nginx_discovery::NginxDiscovery;

let config = "user nginx;";
let discovery = NginxDiscovery::from_config_text(config)?;
let yaml = discovery.to_yaml()?;
assert!(yaml.contains("user"));
Source

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);
Source

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());
Source

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"));
Source

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);
Source

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));
Source

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);
Source

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);
Source

pub fn location_count(&self) -> usize

Count total number of location blocks

Trait Implementations§

Source§

impl Clone for NginxDiscovery

Source§

fn clone(&self) -> NginxDiscovery

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for NginxDiscovery

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.