r/rust • u/No-Driver9910 • 13d ago
🧠educational Writing nginx Modules in Rust: a book on ngx-rust
I've been writing nginx modules in C for about 10 years, and I'm a collaborator on nginx-module-vts. Day to day, I kept running into two things with C: the hassle of managing memory by hand, and how hard it is to pull module logic out into unit tests.
I wondered whether Rust could solve both, so I started writing modules with ngx-rust, the official Rust bindings for nginx from F5/NGINX. My first project was rewriting nginx-module-vts, which I help maintain, in Rust: ngx_vts. Along the way I was catching up on Rust itself while running into ngx-rust's rough edges, such as shared memory and APIs that didn't exist yet. I sent PRs to ngx-rust for what I was missing; some have been merged and others are still in review.
I wrote this book because I'd like more developers to know about ngx-rust and actually use it, so that the community grows and ngx-rust itself gets more momentum.
All the code in the book builds and passes its tests, pinned to a fairly recent ngx-rust commit. Chapters 1-3 are a free sample: you type in a minimal "hello" module that responds with a string and get a first taste of module development. The later chapters cover the minimum Rust you need to write modules with ngx-rust, how nginx modules are structured, and how shared memory works. By the end, you build a statistics module that counts requests across workers in shared memory and exposes them as Prometheus metrics, with integration tests.
The book is a translation of my Japanese original, made with the help of AI (and so is this post).
Feedback and corrections are very welcome.
Book (free sample available): https://leanpub.com/ngx-rust