|
| 1 | +# nginx-sys |
| 2 | + |
| 3 | +The `nginx-sys` crate provides low-level bindings for the nginx C API, allowing Rust applications |
| 4 | +to interact with nginx servers and modules. |
| 5 | + |
| 6 | +## Usage |
| 7 | + |
| 8 | +Add `nginx-sys` as a dependency in your `Cargo.toml`: |
| 9 | + |
| 10 | +```toml |
| 11 | +[dependencies] |
| 12 | +nginx-sys = "0.5.0" |
| 13 | +``` |
| 14 | + |
| 15 | +## Features |
| 16 | + |
| 17 | +- `vendored`: Enables the build scripts to download and build a copy of nginx source and link |
| 18 | + against it. This feature is enabled by default. |
| 19 | + |
| 20 | +## Output variables |
| 21 | + |
| 22 | +Following variables are available to any **direct** dependents of the crate. |
| 23 | + |
| 24 | +Check the [links manifest key] documentation in the Cargo Book for more details. |
| 25 | + |
| 26 | +[links manifest key]: https://doc.rust-lang.org/nightly/cargo/reference/build-scripts.html#the-links-manifest-key |
| 27 | + |
| 28 | +### `DEP_NGINX_FEATURES` |
| 29 | + |
| 30 | +Nginx has various configuration options that may affect the availability of functions, constants |
| 31 | +and structure fields. This is not something that can be detected at runtime, as an attempt to use |
| 32 | +a symbol unavailable in the bindings would result in compilation error. |
| 33 | + |
| 34 | +`DEP_NGINX_FEATURES_CHECK` contains the full list of feature flags supported by `nginx-sys`, i.e. |
| 35 | +everything that can be used for feature checks. The most common use for this variable is to |
| 36 | +specify `cargo:rustc-check-cfg`. |
| 37 | + |
| 38 | +`DEP_NGINX_FEATURES` contains the list of features enabled in the version of nginx being built |
| 39 | +against. |
| 40 | + |
| 41 | +An example of a buildscript with these variables: |
| 42 | +```rust |
| 43 | +// Specify acceptable values for `ngx_feature`. |
| 44 | +println!("cargo::rerun-if-env-changed=DEP_NGINX_FEATURES_CHECK"); |
| 45 | +println!( |
| 46 | + "cargo::rustc-check-cfg=cfg(ngx_feature, values({}))", |
| 47 | + std::env::var("DEP_NGINX_FEATURES_CHECK").unwrap_or("any()".to_string()) |
| 48 | +); |
| 49 | +// Read feature flags detected by nginx-sys and pass to the compiler. |
| 50 | +println!("cargo::rerun-if-env-changed=DEP_NGINX_FEATURES"); |
| 51 | +if let Ok(features) = std::env::var("DEP_NGINX_FEATURES") { |
| 52 | + for feature in features.split(',').map(str::trim) { |
| 53 | + println!("cargo::rustc-cfg=ngx_feature=\"{}\"", feature); |
| 54 | + } |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +And an usage example: |
| 59 | +```rust |
| 60 | +#[cfg(ngx_feature = "debug")] |
| 61 | +println!("this nginx binary was built with debug logging enabled"); |
| 62 | +``` |
| 63 | + |
| 64 | +### `DEP_NGINX_OS` |
| 65 | + |
| 66 | +Version, as detected by the nginx configuration scripts. |
| 67 | + |
| 68 | +`DEP_NGINX_OS_CHECK` contains the full list of supported values, and `DEP_NGINX_OS` the currently |
| 69 | +detected one. |
| 70 | + |
| 71 | +Usage examples: |
| 72 | +```rust |
| 73 | +// Specify acceptable values for `ngx_os` |
| 74 | +println!("cargo::rerun-if-env-changed=DEP_NGINX_OS_CHECK"); |
| 75 | +println!( |
| 76 | + "cargo::rustc-check-cfg=cfg(ngx_os, values({}))", |
| 77 | + std::env::var("DEP_NGINX_OS_CHECK").unwrap_or("any()".to_string()) |
| 78 | +); |
| 79 | +// Read operating system detected by nginx-sys and pass to the compiler. |
| 80 | +println!("cargo::rerun-if-env-changed=DEP_NGINX_OS"); |
| 81 | +if let Ok(os) = std::env::var("DEP_NGINX_OS") { |
| 82 | + println!("cargo::rustc-cfg=ngx_os=\"{}\"", os); |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +```rust |
| 87 | +#[cfg(ngx_os = "freebsd")] |
| 88 | +println!("this nginx binary was built on FreeBSD"); |
| 89 | +``` |
| 90 | + |
| 91 | +### Version and build information |
| 92 | + |
| 93 | +- `DEP_NGINX_VERSION_NUMBER`: a numeric representation with 3 digits for each component: `1026002` |
| 94 | +- `DEP_NGINX_VERSION`: human-readable string in a product/version format: `nginx/1.26.2` |
| 95 | +- `DEP_NGINX_BUILD` : version string with an optional build name (`--build=`) included: |
| 96 | + `nginx/1.25.5 (nginx-plus-r32)` |
| 97 | + |
| 98 | +Usage example: |
| 99 | +```rust |
| 100 | +println!("cargo:rustc-check-cfg=cfg(nginx1_27_0)"); |
| 101 | +println!("cargo:rerun-if-env-changed=DEP_NGINX_VERSION_NUMBER"); |
| 102 | +if let Ok(version) = std::env::var("DEP_NGINX_VERSION_NUMBER") { |
| 103 | + let version: u64 = version.parse().unwrap(); |
| 104 | + |
| 105 | + if version >= 1_027_000 { |
| 106 | + println!("cargo:rustc-cfg=nginx1_27_0"); |
| 107 | + } |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +## Examples |
| 112 | + |
| 113 | +### Get Nginx Version |
| 114 | + |
| 115 | +This example demonstrates how to retrieve the version of the nginx server. |
| 116 | + |
| 117 | +```rust,no_run |
| 118 | +use nginx_sys::nginx_version; |
| 119 | +
|
| 120 | +println!("Nginx version: {}", nginx_version); |
| 121 | +``` |
0 commit comments