Book Image

Nginx HTTP Server - Second Edition

By : Clement Nedelcu
Book Image

Nginx HTTP Server - Second Edition

By: Clement Nedelcu

Overview of this book

<p>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.</p> <p>"Nginx HTTP Server - Second Edition" provides 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, the complete directive reference will be your best friend at all stages of the configuration and maintenance processes.</p> <p>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 to get it to do exactly what you need, in a fast and secure way. For more experienced administrators, this book provides different angles of approach 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.</p> <p>This book takes you through the setup and configuration of Nginx by detailing every step of the way, from downloading to configuring your server in a selection of common architectures.</p>
Table of Contents (17 chapters)
Nginx HTTP Server Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Directive Index
Index

Preface

It is a well-known fact that the market of web servers has a long-established leader: Apache. According to recent surveys, as of January 2013, over 55 percent of the World Wide Web is served by this eighteen-year old open source application. However, for the past few years, the same reports reveal the rise of a new competitor: Nginx, a lightweight HTTP server originating from Russia (pronounced engine X). There have been many interrogations surrounding this young web server. Why has the blogosphere become so effervescent about it? What is the reason causing so many server administrators to switch to Nginx since the beginning of 2009? Is this tiny piece of software mature enough to run my 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 as of September 2008, received 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 Facebook, Netflix, WordPress, SourceForge, and many more. Nginx has proven to be a very efficient, lightweight, yet powerful web server.

Along the chapters of this book, you will discover the many features of Nginx and progressively understand why so many administrators have decided to place their trust in this new HTTP server, often at the expense of Apache. There are many aspects in which Nginx is more efficient than its competitors are. Primarily, speed. 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, allowing for a much lighter CPU load and memory consumption. Secondly, ease of use. Configuration files are much simpler to read and tweak 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, modularity. Not only is Nginx a completely open source project released under a BSD-like license, but it also comes with a powerful plug-in system—referred to as "modules." A large variety of modules are included with the original distribution archive, and many third-party ones can be downloaded online. Overall, Nginx combines speed, efficiency, and power, providing you the perfect ingredients for a successful web server. It appears to be the best Apache alternative as of today.

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

What this book covers

Chapter 1, Downloading and Installing Nginx, guides you through the setup process, by downloading and installing Nginx as well as its prerequisites.

Chapter 2, Basic Nginx Configuration, helps you discover the fundamentals of Nginx configuration and set up the Core module.

Chapter 3, HTTP Configuration, details the HTTP Core module which contains most of the major configuration sections and directives.

Chapter 4, Module Configuration, helps you discover the many first-party modules of Nginx among which are the Rewrite and the SSI modules.

Chapter 5, PHP and Python with Nginx, explains how to set up PHP and other third-party applications (if you are interested in serving dynamic websites) to work together with Nginx via FastCGI.

Chapter 6, Apache and Nginx Together, teaches you how to set up Nginx as a reverse proxy server working together with Apache.

Chapter 7, From Apache to Nginx, provides a detailed guide to switching from Apache to Nginx.

Appendix A, Directive Index, lists and describes all configuration directives, sorted alphabetically. Module directives are also described in their respective chapters too.

Appendix B, Module Reference, lists available modules.

Appendix C, Troubleshooting, discusses the most common issues that administrators face when they configure Nginx.

What you need for this book

Nginx is a free and open source software running under various operating systems: Linux-based, Mac OS, Windows operating systems, and many more. As such, there is no real requirement in terms of software. Nevertheless, in this book, and particularly in the first chapter, we will be working in a Linux environment, so running a Linux-based operating system would be a plus. Prerequisites for compiling the application are further detailed in Chapter 1, Downloading and Installing Nginx.

Who this book is for

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

Conventions

In this book, you will find a number of styles of text 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: "The process consists of appending certain switches to the configure script that comes with the source code."

A block of code is set as follows:

#user nobody;
worker_processes 1;

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

apt-get install nginx

Note

Warnings or important notes appear in a box like this.

Tip

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 may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via 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 on www.packtpub.com/authors.

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 for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

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 would 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 http://www.packtpub.com/submit-errata, 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 on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright 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.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.