Book Image

Nginx Module Extension

By : Usama Dar
Book Image

Nginx Module Extension

By: Usama Dar

Overview of this book

<p>Companies can solve significant scaling issues thanks to switching their web platforms to Nginx. A popular web server for building web infrastructure, Nginx transparently and effectively enables the growth of the largest sites on the Internet today, on top of being free and open source.</p> <p>This book is a practical reference providing you with everything you need to know about Nginx module extensions. You will learn how to write powerful and flexible configuration files and your own modules, as well as gaining knowledge about Nginx’s modular architecture, available modules, and their intricate details.</p> <p>Nginx Module Extension starts by teaching you to compile the Nginx source on multiple platforms. Then you will learn the synopsis, directives, and practical examples of the core, optional, and third party Nginx modules before familiarizing yourself with Nginx internals, enabling you to write custom modules.</p> <p>You will learn about the flexibility of the Nginx configuration and how to enable and disable various options while building from the source. You will learn to customize the Main and Events module to fine-tune the web server performance, and you will also learn about standard and optional HTTP modules like GeoIP, Gzip, Access Control, and using SSL, along with an exploration of PostgreSQL, MySQL, and Memcached modules. Finally you will learn internals like module chaining and will see sample code and a line-by-line walkthrough demonstrating how to write your own module.</p>
Table of Contents (12 chapters)

The extended "Hello world" module


Now we will proceed towards creating a simple Nginx module. This module will print a configurable text in your browser whenever you enter a specific location. This is a very simple module and the idea is to just introduce the core concepts of how to create an Nginx module. This is based on and is an enhanced version of the simple Hello world module found at http://blog.zhuzhaoyuan.com/2009/08/creating-a-hello-world-nginx-module/. This module is an example of a handler module.

Writing and compiling a module

The first thing you have to do is to obviously create a folder for your new module. Create it anywhere other than the Nginx source tree. You should create the following two files to start with:

  • config

  • ngx_http_hello_module.c

The contents of the config file will depend on what kind of module you are writing.

For this simple module, it will look like the following code:

ngx_addon_name=ngx_http_hello_module
HTTP_MODULES="$HTTP_MODULES ngx_http_hello_module"...