Nginx HTTP Server
Fourth Edition
Harness the power of Nginx to make the most of your infrastructure and serve pages faster than ever before
Martin Fjordvald
Clement Nedelcu
BIRMINGHAM - MUMBAI
Nginx HTTP ServerFourth Edition
Copyright 2018 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
Commissioning Editor: Vijin Boricha
Acquisition Editor: Shrilekha Inani
Content Development Editor: Sharon Raj
Technical Editor: Vishal K. Mewada
Copy Editor: Safis Editing
Project Coordinator: Virginia Dias
Proofreader: Safis Editing
Indexer: Pratik Shirodkar
Production Coordinator: Shantanu Zagade
First published: July 2010
Second edition: July 2013
Third edition: November 2015
Fourth edition: February 2018
Production reference: 1120218
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78862-355-1
www.packtpub.com
To my wife Richole Huang, for her love, patience, and understanding throughout our marriage.
mapt.io
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Why subscribe?
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
PacktPub.com
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details.
At www.PacktPub.com , you can also read a collection of free technical articles, sign up for a range of free newsletters, and receive exclusive discounts and offers on Packt books and eBooks.
Contributors
About the authors
Martin Fjordvald is a 29-year-old Danish entrepreneur who started his company straight out of high school. Backed by a popular website, he became a jack of all trades having to deal with the business, programming, and marketing side of his business. The popularity of his website grew and so did the performance requirements of his code and servers.
He got involved with the community project to document Nginx early on and has written several blog posts and wiki articles detailing how Nginx works.
Special thanks to the entire Packt team for their patience during the course of writing this book.
Clement Nedelcu was born in France and studied at UK, French, and Chinese universities. After teaching computer science, programming, and systems administration at several eastern Chinese universities, he worked as a technology consultant in France, specialized in the web and .NET software development as well as Linux server administration. Since 2005, he has also been administering a major network of websites in his spare time, which allowed him to discover Nginx. Clement now works as CTO. for a Hong-Kong-based company developing management software for schools.
About the reviewer
Amet Umerov works as a Linux system engineer with promising start-up. InsideDNA, which provides cloud-based genomics and data analytics to the biotechnology sector working on oncology drugs.
Amet develops computing platforms for reproducible research in bioinformatics.
Specifically, he builds and supports highly parallel, scalable, and stable computing environments for compute-intense analytics. He uses Nginx as the frontend for web applications and load balancers for Docker environments.
I would like to express my gratitude to my family, friends, and colleagues.
Packt is searching for authors like you
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Table of Contents
Preface
It is a well-known fact that the web server market has a long-established leader: Apache. According to recent surveys, as of October 2015, almost 35 percent of the World Wide Web is served by this 20-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 Russiapronounced engine X. There have been many questions surrounding this young web server. What is the reason causing so many server administrators to switch to Nginx since the beginning of year 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 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 proven to be a very efficient, lightweight yet powerful web server. Throughout this book, you will discover 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, first and foremost, 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 a much lighter CPU load and memory consumption. Second, 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 systemreferred to as modules. A large variety of modules are included with the original distribution archive, and many third-party ones can be downloaded online. All in all, 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.