NGINX Cookbook
by Derek DeJonghe
Copyright 2022 OReilly Media, Inc. All rights reserved.
Printed in the United States of America.
Published by OReilly Media, Inc. , 1005 Gravenstein Highway North, Sebastopol, CA 95472.
OReilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com .
- Acquisitions Editor: Melissa Duffield
- Development Editor: Gary OBrien
- Production Editor: Christopher Faucher
- Copyeditor: Kim Cofer
- Proofreader: JM Olejarz
- Indexer: Potomac Indexing, LLC
- Interior Designer: David Futato
- Cover Designer: Karen Montgomery
- Illustrator: Kate Dullea
- November 2020: First Edition
- May 2022: Second Edition
Revision History for the First Edition
- 2022-05-16: First Release
See http://oreilly.com/catalog/errata.csp?isbn=9781492078487 for release details.
The OReilly logo is a registered trademark of OReilly Media, Inc. NGINX Cookbook, the cover image, and related trade dress are trademarks of OReilly Media, Inc.
The views expressed in this work are those of the author, and do not represent the publishers views. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
This work is part of a collaboration between OReilly and NGINX. See our statement of editorial independence .
978-1-0098-12626-1
[LSI]
Foreword
Welcome to the 2022 edition of the NGINX Cookbook. OReilly has been publishing the NGINX Cookbook for seven years, and we continue to iterate and improve on the content. NGINX today is the worlds most popular web server, according to W3Techs, and runs nearly 50% of the top 1000 busiest sites. Since we released the first version of NGINX in 2002, the product has evolved to become a Swiss Army Knife in the best sense. We architected NGINX for flexibility and scale, which made it possible to extend our capabilities beyond web serving to load balancing, reverse proxy, and API gateway as well. We take it as a testament to the value of NGINX that many of the load-balancing services offered by major public clouds and CDNs are actually based on NGINX code.
NGINX has also continued to expand into new realms; our web application firewall now applies machine learning to adapt to new threats as they arise. NGINX Ingress Controller for Kubernetes is natively integrated with NGINX and provides key capabilities for managing both east-west and north-south traffic, a critical requirement in the world of Kubernetes. None of this would matter if NGINX did not continue to deliver speed, resilience, and agilityall fundamental requirements of modern distributed applications.
The NGINX Cookbook is the go-to guide to NGINX from the people who know the code best. Whether you are running NGINX Open Source for a small project or NGINX Plus for a mission-critical enterprise deployment across multiple regions, and whether you are running locally or in the cloud, the Cookbook shows you how to get the most out of NGINX. It features dozens of easy-to-follow recipes covering how to install NGINX, configure it for almost any use case, secure and scale it, and troubleshoot common issues.
The 2022 edition updates many sections, covering new functionality in NGINX and adding entirely new sections and chapters focused on cloud native computing deployments and the new tools and deployment patterns for that paradigm shift. The world of technology and applications is changing fast, and NGINX is changing with it to continue contributing to your success.
The original vision of NGINX was highly scalable, reliable, fast, and secure web serving. Everything we do today is built on that original vision and designed to help our community deploy the apps they need, in any environment, at any scale, with confidence and trust. Please enjoy this latest edition and feel free to tell us what you think (in our Slack channel or online). We are listening.
Christopher Guzikowski
Product Marketing, NGINX, Inc.
Preface
The NGINX Cookbook aims to provide easy-to-follow examples of real-world problems in application delivery. Throughout this book, you will explore the many features of NGINX and how to use them. This guide is fairly comprehensive, and touches on most of the main capabilities of NGINX.
Throughout this book, there will be references to both the free and open source NGINX software, as well as the commercial products from NGINX Inc.: NGINX Plus and NGINX Controller. Features and directives that are only available as part of the paid subscription to NGINX Plus will be denoted as such. Because NGINX Plus is an application delivery controller (ADC), and provides many advanced features, its important to highlight these features to gain a full view of the possibilities of the platform .
The book will begin by explaining the installation process of NGINX and NGINX Plus, as well as some basic getting-started steps for readers new to NGINX. From there, the sections will progress to load balancing in all forms, accompanied by chapters about traffic management, caching, and automation. , covers a lot of ground but is important, because NGINX is often the first point of entry for web traffic to your application, and the first line of application-layer defense against web attacks and vulnerabilities. There are a number of chapters that cover cutting-edge topics such as HTTP/2, media streaming, cloud, service mesh, and container environmentswrapping up with more traditional operational topics such as monitoring, debugging, performance, and operational tips. The book will end with an introduction to NGINX Instance Manager and NGINX Controller, an application-centric management platform.
I personally use NGINX as a multitool, and believe this book will enable you to do the same. Its software that I believe in and enjoy working with. Im happy to share this knowledge with you, and hope that as you read through this book you relate the recipes to your real-world scenarios and will employ these solutions.
Conventions Used in This Book
The following typographical conventions are used in this book:
ItalicIndicates new terms, URLs, email addresses, filenames, and file extensions.
Constant width
Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords.
Note
This element signifies a general note.