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)

Installing binary distribution


Most UNIX and Linux distributions have Nginx included in their package manager repositories. Use package manager commands on your platform to install it. For example, use apt-get on Ubuntu or Debian, and emerge on Gentoo. For Red Hat, Fedora, or CentOS, see the instructions that follow.

You can find binary installation instructions for different platforms such as Red Hat and Ubuntu on the Nginx installation wiki at http://wiki.nginx.org/Install. However, we will briefly describe the process here, quoting from the wiki.

Red Hat, Fedora, and CentOS

To add the Nginx yum repository, create a file named /etc/yum.repos.d/nginx.repo and paste one of the following configurations:

  • For CentOS:

    [nginx]
    name=nginx repo
    baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
    gpgcheck=0
    enabled=1
  • For RHEL:

    [nginx]
    name=nginx repo
    baseurl=http://nginx.org/packages/rhel/$releasever/$basearch/
    gpgcheck=0
    enabled=1

CentOS, RHEL, and Scientific Linux, each populate the $releasever variable differently. Therefore, depending on your OS version, replace $releasever with 5 (for 5.x) or 6 (for 6.x). Therefore, baseurl for 6.x would look like baseurl=http://nginx.org/packages/rhel/6/$basearch/.

Official Debian/Ubuntu packages

Append the following lines to the /etc/apt/sources.list file, and replace the codename with the one appropriate for the release that you are using, for example, Ubuntu 13.10 is codenamed saucy:

  • For Ubuntu 12.10:

    deb http://nginx.org/packages/ubuntu/ saucy nginx
    deb-src http://nginx.org/packages/ubuntu/ saucy nginx

    Please note that when you will run the apt-get update after adding the repository, you will get a GPG error of not being able to verify keys. If this happens and you find it hard to ignore it, do the following:

    wget http://nginx.org/packages/keys/nginx_signing.key
    cat nginx_signing.key | sudo apt-key add -
  • For Debian 6:

    deb http://nginx.org/packages/debian/ squeeze nginx
    deb-src http://nginx.org/packages/debian/ squeeze nginx
    
  • For Ubuntu PPA: This PPA is maintained by volunteers and is not distributed by nginx.org. It has some additional compiled-in modules, and it may be more fitting for your environment. You can get the latest stable version of Nginx from Nginx PPA on Launchpad: You will require root privileges to execute the following commands.

    • For Ubuntu 10.04 and newer:

      sudo -s
      nginx=stable # use nginx=development for latest development version
      add-apt-repository ppa:nginx/$nginx
      apt-get update 
      apt-get install nginx
      

      If you get an error about add-apt-repository not existing, you will want to install python-software-properties. For other Debian/Ubuntu based distributions, you can try the lucid variant of the PPA that is the most likely to work on older package sets.

      sudo -s
      nginx=stable # use nginx=development for latest developmentversion
      echo "deb http://ppa.launchpad.net/nginx/$nginx/ubuntu lucidmain" > /etc/apt/sources.list.d/nginx-$nginx-lucid.list
      apt-key adv --keyserver keyserver.ubuntu.com --recv-keysC300EE8C
      apt-get update
      apt-get install nginx
      

FreeBSD

Update the BSD ports tree using the following command:

# portsnap fetch update

Install the web server using the following commands:

# cd /usr/ports/www/nginx
# make install clean

Type the following command to turn the Nginx server on:

echo 'nginx_enable="YES"' >> /etc/rc.conf

To start Nginx, enter:

# /usr/local/etc/rc.d/nginx start

OpenBSD

OpenBSD, as of Version 5.1, includes Nginx as part of the base system. This means Nginx comes pre-installed along with all the library dependencies. The version is not always the latest and greatest one. However, this allows you to start migrating Apache applications over to Nginx. In the future, it is expected that the default httpd will be Nginx instead of Apache.

Official Win32 binaries

As of Version 0.8.50 of Nginx, the official Windows binary is now available.

The Windows version is provided as binary-only due to the current build process, which uses WineTools at the moment. You will not be able to compile a Windows version from source. Once you download the Windows ZIP file, perform the following steps:

  1. Installation:

    cd c:\
    unzip nginx-1.2.3.zip
    ren nginx-1.2.3 nginx
    cd nginx
    start nginx
    
  2. Control:

    nginx -s [ stop | quit | reopen | reload ]
    

For problems, look in the c:\nginx\logs\error.log file or in EventLog.