www.it-ebooks.info Nginx HTTP Server Second Edition Make the most of your infrastructure and serve pages faster than ever with Nginx Clément Nedelcu BIRMINGHAM - MUMBAI www.it-ebooks.info Nginx HTTP Server Second Edition Copyright © 2013 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 author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be 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. First published: July 2010 Second edition: July 2013 Production Reference: 1120713 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78216-232-2 www.packtpub.com Cover Image by Suresh Mogre ([email protected]) [ FM-2 ] www.it-ebooks.info Credits Author Project Coordinator Clément Nedelcu Rahul Dixit Reviewers Proofreader Michael Shadle Joel T. Johnson Alex Kapranoff Indexer Rekha Nair Acquisition Editor Usha Iyer Graphics Valentina D'Silva Lead Technical Editor Azharuddin Sheikh Disha Haria Technical Editors Production Coordinator Vrinda Nitesh Bhosale Prachali Bhiwandkar Athira Laji Dominic Pereira Cover Work Prachali Bhiwandkar [ FM-3 ] www.it-ebooks.info About the Author Clément Nedelcu was born in France and studied in UK, French, and Chinese universities. After teaching computer science and programming in several eastern Chinese universities, he worked as a Technology Consultant in France, specializing in web and Microsoft .NET programming as well as Linux server administration. Since 2005, he has also been administering a major network of websites in his spare time. This eventually led him to discover Nginx: it made such a difference that he started his own blog about it. One thing leading to another… I would like to express my gratitude to my wife Julie, my son Leo who was born during the writing of this book and never ceased to cheer me up; my family and my friends who have all been very supportive all along the writing stage. This book is dedicated to Martin Fjordvald for originally directing me to Nginx when my servers were about to kick the bucket. Special thanks to Cliff Wells, Maxim Dounin, and all the folks on the #nginx IRC channel on Freenode. [ FM-4 ] www.it-ebooks.info About the Reviewers Michael Shadle is a self-proclaimed surgeon, when it comes to procedural PHP. He has been using PHP for over ten years along with MySQL and various Linux and BSD distributions. He has switched between many different web servers over the years and considers Nginx to be the best solution yet. During the day he works as a senior Web Developer at Intel Corporation on a handful of public-facing websites. He enjoys using his breadth of knowledge to come up with "out of the box" solutions to solve the variety of issues that come up. During the off-hours, he has a thriving personal consulting, web development practice, and has many more personal project ideas than he can tackle at once. He is a minimalist by heart, and believes that when architecting solutions, starting small and simple allows for a more agile approach in the long run. Michael also coined the phrase, "A simple stack is a happy stack." Alex Kapranoff was born in a family of an electronics engineer and a programmer for old Soviet "Big Iron" computers. He started to write programs at the age of 12 and has never worked outside of the IT industry since then. After getting his Software Engineering degree with honors he had a short stint in the world of enterprise databases and Windows. Then he settled on open-source Unix-like environments for good, first FreeBSD and then Linux, working as a developer for many Russian companies from ISPs to search engines. Most of his experience has been with e-mail/ messaging systems and web security. Right now he is trying his hand at a product and project management position in Yandex, one of the biggest search engines in the world. He took his first look at Nginx working in Rambler side-by-side with Nginx's author Igor Sysoev before the initial public release of the product. Since then, Nginx has been an essential tool in his kit. He won't launch a website, no matter how complex it is, without using Nginx nowadays. He strongly believes in the Free Software Movement, loves Perl, plain C, LISP, cooking, and fishing, and lives with a beautiful girlfriend and an old cat in Moscow, Russia. [ FM-5 ] www.it-ebooks.info www.PacktPub.com Support files, eBooks, discount offers and more You might want to visit www.PacktPub.com for support files and downloads related to your book. 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 [email protected] 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. TM http://PacktLib.PacktPub.com Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books. Why Subscribe? • Fully searchable across every book published by Packt • Copy and paste, print and bookmark content • On demand and accessible via web browser Free Access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access. [ FM-6 ] www.it-ebooks.info Table of Contents Preface 1 Chapter 1: Downloading and Installing Nginx 7 Setting up the prerequisites 7 GCC – GNU Compiler Collection 8 The PCRE library 9 The zlib library 10 OpenSSL 11 Downloading Nginx 11 Websites and resources 11 Version branches 13 Features 14 Downloading and extracting 15 Configure options 15 The easy way 16 Path options 16 Prerequisites options 18 Module options 20 Modules enabled by default 20 Modules disabled by default 21 Miscellaneous options 22 Configuration examples 24 About the prefix switch 24 Regular HTTP and HTTPS servers 25 All modules enabled 25 Mail server proxy 26 Build configuration issues 26 Make sure you installed the prerequisites 26 Directories exist and are writable 27 Compiling and installing 27 www.it-ebooks.info Table of Contents Controlling the Nginx service 28 Daemons and services 28 User and group 28 Nginx command-line switches 29 Starting and stopping the daemon 29 Testing the configuration 30 Other switches 31 Adding Nginx as a system service 31 System V scripts 32 What is an init script? 33 Init script for Debian-based distributions 33 Init script for Red Hat-based distributions 34 Installing the script 34 Debian-based distributions 35 Red Hat-based distributions 35 Summary 36 Chapter 2: Basic Nginx Configuration 37 Configuration file syntax 37 Configuration Directives 38 Organization and inclusions 39 Directive blocks 41 Advanced language rules 42 Directives accept specific syntaxes 42 Diminutives in directive values 43 Variables 44 String values 44 Base module directives 44 What are base modules? 45 Nginx process architecture 45 Core module directives 46 Events module 51 Configuration module 54 A configuration for your profile 54 Understanding the default configuration 54 Necessary adjustments 55 Adapting to your hardware 56 Testing your server 57 Creating a test server 58 Performance tests 59 Httperf 59 Autobench 61 OpenWebLoad 62 [ ii ] www.it-ebooks.info Table of Contents Upgrading Nginx gracefully 64 Summary 64 Chapter 3: HTTP Configuration 65 HTTP Core module 65 Structure blocks 66 Module directives 67 Socket and host configuration 68 listen 68 server_name 68 server_name_in_redirect 69 server_names_hash_max_size 70 server_names_hash_bucket_size 70 port_in_redirect 70 tcp_nodelay 70 tcp_nopush 71 sendfile 71 sendfile_max_chunk 71 send_lowat 72 reset_timedout_connection 72 Paths and documents 72 root 72 alias 73 error_page 73 if_modified_since 74 index 74 recursive_error_pages 75 try_files 75 Client requests 75 keepalive_requests 76 keepalive_timeout 76 keepalive_disable 76 send_timeout 76 client_body_in_file_only 77 client_body_in_single_buffer 77 client_body_buffer_size 77 client_body_temp_path 78 client_body_timeout 78 client_header_buffer_size 78 client_header_timeout 79 client_max_body_size 79 large_client_header_buffers 79 lingering_time 80 lingering_timeout 80 lingering_close 80 ignore_invalid_headers 80 chunked_transfer_encoding 81 max_ranges 81 [ iii ] www.it-ebooks.info