#nginx #ffi #sys

sys nginx-sys

FFI bindings to NGINX

3 unstable releases

0.2.1 Oct 26, 2023
0.2.0 Aug 28, 2023
0.1.0 May 17, 2023

#440 in HTTP server

Download history 25/week @ 2023-11-04 27/week @ 2023-11-11 26/week @ 2023-11-18 63/week @ 2023-11-25 19/week @ 2023-12-02 26/week @ 2023-12-09 12/week @ 2023-12-16 25/week @ 2023-12-23 10/week @ 2023-12-30 22/week @ 2024-01-06 33/week @ 2024-01-13 24/week @ 2024-01-20 12/week @ 2024-01-27 9/week @ 2024-02-03 28/week @ 2024-02-10 91/week @ 2024-02-17

149 downloads per month
Used in ngx

Apache-2.0

35KB
591 lines

Rust crates.io Project Status: Concept – Minimal or no implementation has been done yet, or the repository is only intended to be a limited example, demo, or proof-of-concept. Community Support

Project status

This project is still a work in progress and not production ready.

Description

This project provides Rust SDK interfaces to the NGINX proxy allowing the creation of NGINX dynamic modules completely in Rust.

In short, this SDK allows writing NGINX modules using the Rust language.

Build

NGINX modules can be built against a particular version of NGINX. The following environment variables can be used to specify a particular version of NGINX or an NGINX dependency:

  • ZLIB_VERSION (default 1.3) -
  • PCRE2_VERSION (default 10.42)
  • OPENSSL_VERSION (default 3.0.7)
  • NGX_VERSION (default 1.23.3) - NGINX OSS version
  • NGX_DEBUG (default to false)- if set to true, then will compile NGINX --with-debug option

For example, this is how you would compile the examples using a specific version of NGINX and enabling debugging:

NGX_DEBUG=true NGX_VERSION=1.23.0 cargo build --package=examples --examples --release

To build Linux-only modules, use the "linux" feature:

cargo build --package=examples --examples --features=linux --release

After compilation, the modules can be found in the path target/release/examples/ ( with the .so file extension for Linux or .dylib for MacOS).

Additionally, the folder .cache/nginx/{NGX_VERSION}/{OS}/ will contain the compiled version of NGINX used to build the SDK. You can start NGINX directly from this directory if you want to test the module.

Mac OS dependencies

In order to use the optional GNU make build process on MacOS, you will need to install additional tools. This can be done via homebrew with the following command:

brew install make openssl grep

Additionally, you may need to set up LLVM and clang. Typically, this is done as follows:

# make sure xcode tools are installed
xcode-select --install
# instal llvm
brew install --with-toolchain llvm

Linux dependencies

See the Dockerfile for dependencies as an example of required packages on Debian Linux.

Build example

Example modules are available in examples folder. You can use cargo build --package=examples --examples to build these examples. After building, you can find the .so or .dylib in the target/debug folder. Add --features=linux to build linux specific modules. NOTE: adding the "linux" feature on MacOS will cause a build failure.

For example (all examples plus linux specific): cargo build --package=examples --examples --features=linux

Docker

We provide a multistage Dockerfile:

# build all dynamic modules examples and specify NGINX version to use
docker buildx build --build-arg NGX_VERSION=1.23.3 -t ngx-rust .

# start NGINX using [curl](examples/curl.conf) module example:
docker run --rm -d  -p 8000:8000 ngx-rust nginx -c examples/curl.conf

# test it - you should see 403 Forbidden
curl http://127.0.0.1:8000 -v -H "user-agent: curl"


# test it - you should see 404 Not Found
curl http://127.0.0.1:8000 -v -H "user-agent: foo"

Usage

A complete module example using the SDK can be found here. You can build it with cargo build --package=examples --example=curl then set up NGINX to use it:

For example:

daemon off;
master_process off;

# unix:
# load_module modules/libcurl.so;

# error_log logs/error.log debug;
error_log /dev/stdout debug;

working_directory /tmp/cores/;
worker_rlimit_core 500M;

events {
}

http {
    access_log /dev/stdout;
    server {
        listen 8000;
        server_name localhost;
        location / {
            alias /srv/http;
            # ... Other config stuff ...

            curl on;
        }
    }
}

Support

This SDK is currently unstable. Right now, our primary goal is collect feedback and stabilize it be before offering support. Feel free contributing by creating issues, PRs, or github discussions.

Currently, the only supported platforms are:

  • Darwin (Mac OSX)
  • Linux platform

Roadmap

If you have ideas for releases in the future, please suggest them in the github discussions.

Contributing

We welcome pull requests and issues!

Please refer to the Contributing Guidelines when doing a PR.

Authors and acknowledgment

This project uses some great work from dcoles/nginx-rs, arvancloud/nginx-rs.

License

All code in this repository is licensed under the Apache License v2 license.


lib.rs:

nginx-sys

The nginx-sys crate provides low-level bindings for the nginx C API, allowing Rust applications to interact with nginx servers and modules.

Usage

Add nginx-sys as a dependency in your Cargo.toml:

[dependencies]
nginx-sys = "0.1.0"

Features

  • build: Enables the build scripts to compile and link against the nginx C library. This feature is enabled by default.

Examples

Get Nginx Version

This example demonstrates how to retrieve the version of the nginx server.

use nginx_sys::nginx_version;

let version = unsafe { nginx_version() };
println!("Nginx version: {}", version);

No runtime deps

~0–3.5MB
~56K SLoC