Leanpub Header

Skip to main content

Writing nginx Modules in Rust

A practical guide to building a real module with ngx-rust

Writing nginx Modules in Rust

Learn to write nginx modules in Rust by building a statistics module that counts requests across workers in shared memory and exposes them as Prometheus metrics.

Minimum price

$14.99

$24.99

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
About

About

About the Book

nginx modules have long been written in C, where every pointer lifetime, every allocation, and every access to shared memory is the author's responsibility. ngx-rust, the official Rust binding from F5/NGINX, lets the compiler take over the part of that responsibility it can check.

This book shows you how to write nginx modules in Rust, all the way to a statistics module you could run in production: it reads configuration directives, counts requests across worker processes in shared memory, serves Prometheus metrics on /status, and is verified by integration tests.

Every explanation is tied to code that actually runs. The examples come from ngx-rust itself, from nginx-acme, from ports of nginx's own stub_status and not_modified filter modules, and from ngx_vts, a Rust version of nginx-module-vts that passes 13 integration test files with 300 assertions. Where an API was missing, the author sent it upstream to ngx-rust, and the book shows how.

What you will learn:

- The Rust you need to read ngx-rust code: ownership, Option and Result, traits and monomorphization, lifetimes, and unsafe, explained through real ngx-rust code rather than toy examples

- How a module is put together: directives, the three configuration levels, the 11 phases, requests and responses

- Where values live: request pools, cycle pools, and shared memory, and why the allocator is a type parameter

- Shared memory across worker processes: zones, slab, rbtree, inter-process locks, and what happens on reload

- Filters and load balancers, where ngx-rust has no wrapper yet

- Building, testing, and debugging a real statistics module, including the pitfalls the author actually hit

Who this book is for: anyone who runs nginx and wants to extend it. No Rust experience is assumed; Part 1 covers what you need. Knowing C helps but is not required.

The book targets ngx-rust main as of September 17, 2026 (commit f5aacab) and nginx 1.30.x, tested on x86_64 Linux and Apple Silicon.

This English edition was translated from the author's Japanese original, available on Zenn, with the help of an AI model. The technical content and code are the author's own.

Share this book

Author

About the Author

Yugo Horie

Yugo Horie maintains nginx-module-vts, the virtual host traffic status module for nginx, and wrote ngx_vts, its Rust port built on ngx-rust, sending changes upstream to ngx-rust along the way.

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub