Book Image

Nginx HTTP Server, Third Edition

By : Clement Nedelcu
Book Image

Nginx HTTP Server, Third Edition

By: Clement Nedelcu

Overview of this book

Nginx is a lightweight HTTP server designed for high-traffic websites, with network scalability as the primary objective. With the advent of high speed Internet access, short loading times and fast transfer rates have become a necessity. This free, open source solution will either come as a full replacement of other software such as Apache, or stand in front of your existing infrastructure to improve its overall speed. This book is a detailed guide to setting up Nginx in different ways that correspond to actual production situations: as a standalone server, as a reverse proxy, interacting with applications via FastCGI, and more. In addition, this complete directive reference will be your best friend at all stages of the configuration and maintenance processes. This book is the perfect companion for both Nginx beginners and experienced administrators. For beginners, it will take you through the complete process of setting up this lightweight HTTP server on your system and configuring its various modules so it does exactly what you need quickly and securely. For more experienced administrators, this book provides different approaches that can help you make the most of your current infrastructure. Nginx can be employed in many situations, whether you are looking to construct an entirely new web-serving architecture or simply want to integrate an efficient tool to optimize your site loading speeds.
Table of Contents (17 chapters)
Nginx HTTP Server Third Edition
About the Author
About the Reviewer

Setting up the prerequisites

We have chosen to download the source code of the application and compile it manually, as opposed to installing it using a package manager, such as Yum, Aptitude, or Yast. There are two reasons for this choice. First, the package may not be available in the enabled repositories of your Linux distribution. In addition, the repositories that offer to download and install Nginx automatically often contain outdated versions. More importantly, you need to configure a variety of significant compile-time options. As a result of this choice of manual setup, your system will require some tools and libraries for the compilation process.

Depending on the optional modules that you select at compile time, you will perhaps need different prerequisites. You will be guided through the process of installing the most common ones, such as GCC, PCRE, zlib, and OpenSSL.


If your operating system offers the possibility to install the Nginx package from a repository, and you are confident enough that the available version will suit all of your needs with the modules included by default, you could consider skipping this chapter altogether and simply run one the following commands. We still recommend getting the latest version and building it from source seeing as it contains the latest bug fixes and security patches. For a Debian-based operating system, run the following command:

apt-get install nginx

For Red Hat–based operating systems, use the following command:

yum install nginx

Alternatively, you can download and install prebuilt packages from the official Nginx website at

The GNU Compiler Collection

Nginx is a program written in C, so you will first need to install a compiler tool, such as the GNU Compiler Collection (GCC), on your system. GCC may already be present on your system, but if that is not the case, you will have to install it before going any further.


GCC is a collection of free open source compilers for various languages: C, C++, Java, Ada, FORTRAN, and so on. It is the most commonly used compiler suite in the Linux world, and Windows versions are also available. It supports a vast number of processors such as x86, AMD64, PowerPC, ARM, MIPS, and more.

First, make sure GCC isn't already installed on your system by running the following command:

If you get the following output, it means that GCC is correctly installed on your system and you can skip to the next section:

gcc: no input files

If you receive the following message, you will have to proceed with the installation of the compiler:

~bash: gcc: command not found

GCC can be installed using the default repositories of your package manager. Depending on your distribution, the package manager will vary—yum for a Red Hat-based distribution, apt for Debian and Ubuntu, yast for SuSE Linux, and so on. Here is the typical way to proceed with the download and installation of the GCC package:

[[email protected] ~]# yum groupinstall "Development Tools"

If you use apt-get to install software packages, run the following command:

[[email protected] ~]# apt-get install build-essentials

If you use another package manager with a different syntax, you will probably find the documentation with the man utility. Either way, your package manager should be able to download and install GCC correctly after having solved the dependencies automatically. Note that this command not only installs GCC, it also proceeds with downloading and installing all common requirements to build applications from source, such as code headers and other compilation tools.

The PCRE library

The Perl Compatible Regular Expression (PCRE) library is required to compile Nginx. The Rewrite and HTTP core modules of Nginx use PCRE for the syntax of their regular expressions, as you will discover in later chapters. You will need to install two packages: pcre and pcre-devel. The first package provides the compiled version of the library, whereas the second one provides development headers and sources to compile projects, which are required in our case. Here are example commands that you can run in order to install both the packages.

If you are using yum, run the following command:

[[email protected] ~]# yum install pcre pcre-devel

Or you can install all of the PCRE-related packages by using the following command:

[[email protected] ~]# yum install pcre*

If you use apt-get, run the following command:

[[email protected] ~]# apt-get install libpcre3 libpcre3-dev

If these packages are already installed on your system, you will receive a message saying something like Nothing to do. In other words, the package manager did not install or update any component, as both components are already present on the system.

The zlib library

The zlib library provides developers with compression algorithms. It is required for the use of gzip compression in various modules of Nginx. Again, you can use your package manager to install this component, as it is part of the default repositories. Similar to PCRE, you will need both the library and its source for installation: zlib and zlib-devel.

If using yum, run the following command:

[[email protected] ~]# yum install zlib zlib-devel

Using apt-get, run the following command:

[[email protected] ~]# apt-get install zlib1g zlib1g-dev

These packages install quickly and have no known dependency issues.


The OpenSSL project is a collaborative effort to develop a robust, commercial-grade, full-featured, and open source toolkit implementing the Secure Sockets Layer (SSL v2/v3) and Transport Layer Security (TLS v1) protocols, as well as a full-strength general purpose cryptography library. The project is managed by a worldwide community of volunteers that use the Internet to communicate, plan, and develop the OpenSSL toolkit and its related documentation. For more information, visit

The OpenSSL library will be used by Nginx to serve web pages over a secure connection. We thus need to install the library and its development package. The process remains the same here—you install openssl and openssl-devel:

If using yum, run the following command:

[[email protected] ~]# yum install openssl openssl-devel

If using apt-get, run the following command:

[[email protected] ~]# apt-get install openssl openssl-dev


Please be aware of the laws and regulations in your own country. Some countries do not allow usage of a strong cryptography. The author, publisher, and the developers of the OpenSSL and Nginx projects will not be held liable for any violations or law infringements on your part.

Now that you have installed all of the prerequisites, you are ready to download and compile the Nginx source code.