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


It is a well-known fact that the market for web servers has a long-established leader: Apache. According to recent surveys conducted in October 2015, almost 35 percent of the World Wide Web is served by this twenty-year old open source application. However, the same reports reveal the rise of a new competitor in the past few years: Nginx, a lightweight HTTP server originating from Russia and pronounced "engine x". What has caused so many server administrators to switch to Nginx since the beginning of the 2009? Is this tiny piece of software mature enough to run a high-traffic website?

To begin with, Nginx is not as young as one might think. Originally started in 2002, the project was first carried out by a standalone developer, Igor Sysoev, for the needs of an extremely high-traffic Russian website, namely Rambler, which received, as of September 2008, over 500 million HTTP requests per day. The application is now used to serve some of the most popular websites on the Web, such as Reddit, Wikipedia, WordPress, Dropbox, and many more. Nginx has proved to be a very efficient, lightweight yet powerful web server. Throughout the chapters in this book, you will discover the numerous features of Nginx and progressively understand why so many administrators decide to place their trust in this new HTTP server, often at the expense of Apache.

There are several aspects in which Nginx is more efficient than its competitors. First, and foremost, it's faster. By making use of asynchronous sockets, Nginx does not spawn processes as many times as it receives requests. One process per core suffices to handle thousands of connections, leading to a much lighter CPU load and memory consumption. Secondly, its simplicity of use is remarkable. Configuration files are much easier to read and tweak with Nginx than with other web server solutions, such as Apache; a couple of lines are enough to set up a complete virtual host configuration.

Last but not least, server administrators appreciate it for its modularity. Not only is Nginx a completely open source project released under a BSD-like license, but it also comes with a powerful plugin system referred to as "modules". A large variety of modules are included with the original distribution archive, and a number of third-party ones can be downloaded online.

All in all, Nginx combines speed, efficiency, and power to provide you with the perfect ingredients for a successful web server. It appears to be the best Apache alternative as of today.

What this book covers

Chapter 1, Downloading and Installing Nginx, guides you through the early setup stages of downloading and configuring your own build of the program.

Chapter 2, Basic Nginx Configuration, covers the essential aspects of the Nginx configuration structure and syntax.

Chapter 3, HTTP Configuration, takes you through the configuration of HTTP server components, enabling you to serve a simple static site.

Chapter 4, Module Configuration, provides an in-depth approach to the large variety of modules available with the standard Nginx package.

Chapter 5, PHP and Python with Nginx, is a comprehensive guide to setting up backend programs to serve dynamic content through Nginx.

Chapter 6, Apache and Nginx Together, describes how both server applications can cooperate on the same architecture to improve existing websites and services.

Chapter 7, From Apache to Nginx, provides key information for fully switching your server or web infrastructure from Apache to Nginx.

Chapter 8, Introducing Load Balancing and Optimization, provides useful leads for server administrators who manage sites under heavy loads.

Chapter 9, Case Studies, offers a practical approach to several real-life examples, including some of the most common tasks performed with Nginx.

Chapter 10, Troubleshooting, covers the most common issues encountered while setting up Nginx or during the production stages.

What you need for this book

Although Nginx is available for Windows from version 0.7.52 onward, it is common knowledge that Linux- or BSD-based distributions are preferred to host production sites. During the various processes described in this book, we will assume that you are hosting your website on a Linux operating system, such as Debian, Ubuntu, CentOS, or other well-known distributions.

Who this book is for

By covering both the early setup stages and advanced topics, this book suits web administrators who are interested in solutions to optimize their infrastructure, whether you are looking into replacing your existing web server software or integrating a new tool to cooperate with applications that are already up-and-running. If you, your visitors, and your operating system have been disappointed by Apache, this book is exactly what you need.


In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "Or if your system does not come with the service command:"

A block of code is set as follows:

include /file/path.conf;
include sites/*.conf;

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

user nginx nginx;
master_process on;
worker_processes 4;
events {
  worker_connections 1024;
  use epoll;

Any command-line input or output is written as follows:

# cp /usr/local/nginx/conf/nginx.conf /home/user/backups/nginx.conf.bak

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "If these packages are already installed on your system, you will receive a message saying something like Nothing to do."


Warnings or important notes appear in a box like this.


Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.


Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to and enter the name of the book in the search field. The required information will appear under the Errata section.


Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.


If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.