Skip to content

Commit 1c8a27a

Browse files
committed
docs: actualize build instructions and format readme
1 parent c35d4ed commit 1c8a27a

File tree

2 files changed

+240
-108
lines changed

2 files changed

+240
-108
lines changed

README.md

Lines changed: 145 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -3,141 +3,175 @@
33
[![Project Status: WIP – Initial development is in progress, but there has not yet been a stable, usable release suitable for the public.](https://www.repostatus.org/badges/latest/wip.svg)](https://www.repostatus.org/#wip)
44
[![Community Support](https://badgen.net/badge/support/community/cyan?icon=awesome)](https://github.com/nginx/ngx-rust/discussions)
55

6+
<!-- cargo-sync-readme start -->
7+
8+
# Rust bindings for NGINX
9+
10+
The `ngx` crate provides a high-level Rust interface for the [NGINX] C APIs,
11+
allowing creation of NGINX dynamic modules completely in Rust.
12+
13+
[NGINX]: https://nginx.org/
614

715
## Project status
816

917
This project is in active development. It is stable enough to be used in our
1018
own products, but the APIs are not stabilized and breaking changes are expected.
1119

12-
# Description
20+
## Build
1321

14-
This project provides Rust SDK interfaces to the [NGINX](https://nginx.com) proxy allowing the creation of NGINX
15-
dynamic modules completely in Rust.
22+
NGINX modules can be built against a particular version of NGINX. The following
23+
environment variables can be used to specify the NGINX build to use:
1624

17-
In short, this SDK allows writing NGINX modules using the Rust language.
25+
- `NGINX_BUILD_DIR` - absolute path to the NGINX build directory.
26+
Usually it's `objs` under the source directory, but can be changed with the
27+
`--builddir=` argument of the configure script.
1828

19-
## Build
29+
- `NGINX_SOURCE_DIR` - absolute path to the NGINX source directory, configured
30+
with the [`configure`](https://nginx.org/en/docs/configure.html) command.
2031

21-
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:
32+
Only the `./configure` step of the NGINX build is mandatory because bindings
33+
generator don't depend on any of the artifacts generated by `make`.
2234

23-
* `ZLIB_VERSION` (default 1.3.1) - zlib version
24-
* `PCRE2_VERSION` (default 10.45 for NGINX 1.22.0 and later, or 8.45 for earlier) - PCRE1 or PCRE2 version
25-
* `OPENSSL_VERSION` (default 3.5.0 for NGINX 1.22.0 and later, or 1.1.1w for earlier) - OpenSSL version
26-
* `NGX_VERSION` (default 1.28.0) - NGINX OSS version
27-
* `NGX_DEBUG` (default to false) - if set to true, then will compile NGINX `--with-debug` option
35+
For example, this is how you would compile the [examples](#examples) using a
36+
specific version of NGINX:
2837

29-
For example, this is how you would compile the [examples](examples) using a specific version of NGINX and enabling
30-
debugging:
31-
```
32-
NGX_DEBUG=true NGX_VERSION=1.23.0 cargo build --package=examples --examples --release
38+
```sh
39+
NGINX_BUILD_DIR=/path/to/nginx-1.28.0/objs cargo build --package=examples --examples
3340
```
3441

35-
To build Linux-only modules, use the "linux" feature:
36-
```
37-
cargo build --package=examples --examples --features=linux --release
38-
```
42+
### Building with the NGINX build script
3943

40-
After compilation, the modules can be found in the path `target/release/examples/` ( with the `.so` file extension for
41-
Linux or `.dylib` for MacOS).
44+
You can build your Rust-based module as a part of the NGINX build process by
45+
providing a `config` script implementation and passing the `--add-module`/
46+
`--add-dynamic-module` options to the configure script.
4247

43-
Additionally, the folder `.cache/nginx/{NGX_VERSION}/{TARGET}` (`{TARGET}` means rustc's target triple string) will contain the compiled version of NGINX used to build
44-
the SDK. You can start NGINX directly from this directory if you want to test the module.
48+
See the following example integration scripts: [`examples/config`] and
49+
[`examples/config.make`].
4550

46-
The following environment variables can be used to change locations of cache directory and NGINX directory:
51+
[`examples/config`]: https://github.com/bavshin-f5/ngx-rust/blob/main/examples/config
52+
[`examples/config.make`]: https://github.com/bavshin-f5/ngx-rust/blob/main/examples/config.make
4753

48-
* `CACHE_DIR` (default `[nginx-sys's root directory]/.cache`) - the directory containing cache files, means PGP keys, tarballs, PGP signatures, and unpacked source codes. It also contains compiled NGINX in default configuration.
49-
* `NGINX_INSTALL_ROOT_DIR` (default `{CACHE_DIR}/nginx`) - the directory containing the series of compiled NGINX in its subdirectories
50-
* `NGINX_INSTALL_DIR` (default `{NGINX_INSTALL_BASE_DIR}/{NGX_VERSION}/{TARGET}`) - the directory containing the NGINX compiled in the build
54+
### Building with vendored NGINX sources
5155

52-
### Mac OS dependencies
56+
It is possible to build module with a vendored copy of the NGINX sources
57+
provided in the `nginx-src` crate by enabling feature `vendored`:
5358

54-
In order to use the optional GNU make build process on MacOS, you will need to install additional tools. This can be
55-
done via [homebrew](https://brew.sh/) with the following command:
56-
```
59+
By default, this will use the latest stable release of NGINX and require
60+
system-wide installation of build dependencies (OpenSSL, PCRE2, Zlib).
61+
62+
The behavior of vendored builds can be customized with environment variables,
63+
as documented in the [nginx-src](./nginx-src/) crate README.
64+
65+
**NOTE**: We recommend to build the module binaries against the exact source and
66+
configuration of the NGINX build that you are planning to use in production,
67+
and that is unlikely to be possible with the vendored source.
68+
69+
`configure` arguments alter the APIs and the symbols visible to the Rust code,
70+
and some OS distributions are known to ship nginx packages with API-breaking
71+
patches applied.
72+
73+
### Dependencies
74+
75+
The following dependencies are required to build a Rust NGINX module on Linux
76+
or BSD platform:
77+
78+
- NGINX build dependencies: C compiler, make, OpenSSL, PCRE2, and Zlib.
79+
- Rust toolchain (1.81.0 or later)
80+
- [libclang] for rust-bindgen
81+
82+
The installation process and the package names are system-dependent. Please,
83+
consult the documentation for your distribution.
84+
85+
Note that on some systems you will need `-devel` or `-dev` versions of the
86+
packages.
87+
88+
For example, [Dockerfile] contains the installation commands for Debian Linux.
89+
90+
[Dockerfile]: https://github.com/nginx/ngx-rust/blob/main/Dockerfile
91+
[libclang]: https://rust-lang.github.io/rust-bindgen/requirements.html
92+
93+
### macOS dependencies
94+
95+
In order to use the optional GNU make build process on macOS, you will need
96+
to install additional tools. This can be done via [homebrew](https://brew.sh/)
97+
with the following command:
98+
99+
```sh
57100
brew install make openssl grep
58101
```
59102

60-
Additionally, you may need to set up LLVM and clang. Typically, this is done as follows:
103+
Additionally, you may need to set up LLVM and clang. Typically, this is done
104+
as follows:
61105

62-
```
106+
```sh
63107
# make sure xcode tools are installed
64108
xcode-select --install
65109
# instal llvm
66110
brew install --with-toolchain llvm
67111
```
68112

69-
### Linux dependencies
113+
<!-- cargo-sync-readme end -->
70114

71-
See the [Dockerfile](Dockerfile) for dependencies as an example of required packages on Debian Linux.
115+
### Examples
72116

73-
### Build example
117+
Example modules are available in [examples] folder.
118+
You can use `cargo build --package=examples --examples` to build these examples.
119+
After compilation, the modules can be found in the path `target/debug/examples/`
120+
(with the `.so` file extension for Linux or `.dylib` for macOS).
121+
Add `--features=linux` to build Linux specific modules.
74122

75-
Example modules are available in [examples](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.
123+
**NOTE**: adding the "linux" feature on macOS will cause a build failure.
76124

77125
For example (all examples plus linux specific):
78-
`cargo build --package=examples --examples --features=linux`
79-
80-
### Build with external NGINX source tree
81-
82-
If you require a customized NGINX configuration, you can build a module against an existing pre-configured source tree.
83-
To do that, you need to set the `NGINX_BUILD_DIR` variable to an _absolute_ path of the NGINX build directory (`--builddir`, defaults to the `objs` in the source directory).
84-
Only the `./configure` step of the NGINX build is mandatory because bindings don't depend on any of the artifacts generated by `make`.
85-
86126

127+
```sh
128+
cargo build --package=examples --examples --features=linux
87129
```
88-
NGINX_BUILD_DIR=$PWD/../nginx/objs cargo build --package=examples --examples
89130

90-
```
91-
92-
Furthermore, this approach can be leveraged to build a module as a part of the NGINX build process by adding the `--add-module`/`--add-dynamic-module` options to the configure script.
93-
See the following example integration scripts: [`examples/config`](examples/config) and [`examples/config.make`](examples/config.make).
131+
[examples]: https://github.com/nginx/ngx-rust/tree/main/examples
94132

95133
### Docker
96134

97-
We provide a multistage [Dockerfile](Dockerfile):
98-
99-
# build all dynamic modules examples and specify NGINX version to use
100-
docker buildx build --build-arg NGX_VERSION=1.23.3 -t ngx-rust .
135+
We provide a multistage [Dockerfile]:
101136

102-
# start NGINX using [curl](examples/curl.conf) module example:
103-
docker run --rm -d -p 8000:8000 ngx-rust nginx -c examples/curl.conf
137+
```sh
138+
# build all dynamic modules examples and specify NGINX version to use
139+
docker buildx build --build-arg NGX_VERSION=1.29.1 -t ngx-rust .
104140

105-
# test it - you should see 403 Forbidden
106-
curl http://127.0.0.1:8000 -v -H "user-agent: curl"
141+
# start NGINX using [curl](examples/curl.conf) module example:
142+
docker run --rm -d -p 8000:8000 ngx-rust nginx -c examples/curl.conf
107143

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

109-
# test it - you should see 404 Not Found
110-
curl http://127.0.0.1:8000 -v -H "user-agent: foo"
147+
# test it - you should see 404 Not Found
148+
curl http://127.0.0.1:8000 -v -H "user-agent: foo"
149+
```
111150

112151
## Usage
113152

114-
A complete module example using the SDK can be found [here](examples/curl.rs). You can build it with
115-
`cargo build --package=examples --example=curl` then set up NGINX to use it:
153+
A complete module example using the SDK can be found [here](examples/curl.rs).
154+
You can build it with `cargo build --package=examples --example=curl` then set
155+
up NGINX to use it, as shown below:
116156

117-
For example:
118157
```nginx
119158
daemon off;
120-
master_process off;
121-
122-
# unix:
123-
# load_module modules/libcurl.so;
124-
125-
# error_log logs/error.log debug;
126-
error_log /dev/stdout debug;
159+
error_log stderr debug;
127160
128-
working_directory /tmp/cores/;
129-
worker_rlimit_core 500M;
161+
load_module modules/libcurl.so;
130162
131163
events {
132164
}
133165
134166
http {
135-
access_log /dev/stdout;
136167
server {
137168
listen 8000;
138169
server_name localhost;
170+
171+
access_log /dev/stdout;
172+
139173
location / {
140-
alias /srv/http;
174+
root /srv/http;
141175
# ... Other config stuff ...
142176
143177
curl on;
@@ -147,15 +181,38 @@ http {
147181
```
148182

149183
## Support
150-
This SDK is currently unstable. Right now, our primary goal is collect feedback and stabilize it be before
151-
offering support. Feel free [contributing](CONTRIBUTING.md) by creating issues, PRs, or github discussions.
152184

153-
Currently, the only supported platforms are:
154-
* Darwin (Mac OSX)
155-
* Linux platform
185+
This SDK is currently unstable. Right now, our primary goal is collect feedback
186+
and stabilize it before offering support.
187+
Feel free to [contribute](CONTRIBUTING.md) by creating issues, PRs, or github
188+
discussions.
189+
190+
Currently, the supported platforms are:
191+
192+
- Darwin (macOS)
193+
- FreeBSD
194+
- Linux
195+
196+
See the Rust toolchain [platform support] document for supported versions and
197+
architectures.
198+
199+
[platform support]: https://doc.rust-lang.org/rustc/platform-support.html
200+
201+
### Tested OS and platforms
202+
203+
In addition, we verified that the modules based on this SDK can be built and
204+
work on the following platforms.
205+
206+
- Illumos
207+
- NetBSD 9-10
208+
- OpenBSD 7
209+
- Solaris 11.4
210+
- Windows 10+/Windows Server 2022+
156211

157212
## Roadmap
158-
If you have ideas for releases in the future, please suggest them in the github discussions.
213+
214+
If you have ideas for releases in the future, please suggest them in the github
215+
discussions.
159216

160217
## Contributing
161218

@@ -164,8 +221,11 @@ We welcome pull requests and issues!
164221
Please refer to the [Contributing Guidelines](CONTRIBUTING.md) when doing a PR.
165222

166223
## Authors and acknowledgment
167-
This project uses some great work from [dcoles/nginx-rs](https://github.com/dcoles/nginx-rs),
168-
[arvancloud/nginx-rs](https://github.com/arvancloud/nginx-rs).
224+
225+
This project uses some great work from:
226+
227+
- [dcoles/nginx-rs](https://github.com/dcoles/nginx-rs),
228+
- [arvancloud/nginx-rs](https://github.com/arvancloud/nginx-rs).
169229

170230
## Projects using the SDK
171231

@@ -175,4 +235,6 @@ This project uses some great work from [dcoles/nginx-rs](https://github.com/dcol
175235
## License
176236

177237
All code in this repository is licensed under the
178-
[Apache License v2 license](LICENSE).
238+
[Apache License, Version 2.0](./LICENSE).
239+
240+
&copy; [F5, Inc.](https://www.f5.com/)

0 commit comments

Comments
 (0)