NGINX High Performance (Rahul Sharma) (Z-Library)

Author: Rahul Sharma

艺术

System administrators, developers, and engineers looking for ways to achieve maximum performance from NGINX will find this book beneficial. If you are looking for solutions such as how to handle more users from the same system or load your website pages faster, then this is the book for you.

📄 File Format: PDF
💾 File Size: 8.7 MB
6
Views
0
Downloads
0.00
Total Donations

📄 Text Preview (First 20 pages)

ℹ️

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

📄 Page 1
www.it-ebooks.info
📄 Page 2
NGINX High Performance Optimize NGINX for high-performance, scalable web applications Rahul Sharma BIRMINGHAM - MUMBAI www.it-ebooks.info
📄 Page 3
NGINX High Performance Copyright © 2015 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 2015 Production reference: 1100715 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78528-183-9 www.packtpub.com www.it-ebooks.info
📄 Page 4
Credits Author Rahul Sharma Reviewers Titouan Galopin Tatsuhiko Kubo Commissioning Editor Veena Pagare Acquisition Editor Shaon Basu Content Development Editor Sumeet Sawant Technical Editor Shashank Desai Copy Editor Sarang Chari Project Coordinator Shweta H Birwatkar Proofreader Safis Editing Indexer Monica Ajmera Mehta Production Coordinator Nilesh R. Mohite Cover Work Nilesh R. Mohite www.it-ebooks.info
📄 Page 5
About the Author Rahul Sharma works as a principal consultant with Xebia India. He has 10 years of experience in building and designing applications on the Java and J2EE platforms. He is an open source enthusiast and has contributed to a variety of open source projects, such as Apache Crunch, Apache Provisionr, Apache HDT, and so on. In his career, he has worked with companies of various sizes, from enterprises to start-ups, and has used NGINX along the way. He often speaks at various technical meetups and shares his knowledge on his personal blog at https://devlearnings.wordpress.com/. www.it-ebooks.info
📄 Page 6
About the Reviewers Titouan Galopin is a certified PHP/Symfony French web architect from Paris. He has worked for various companies, including Ademis, Emakina, LIIP, and Coburo. Titouan graduated in computer science and information technology from the Paris-Saclay University, and he is currently pursuing an engineering degree from the University of Technology of Compiègne. First as a web developer and then as a web architect since 2012, Titouan has worked on NGINX intensively to improve its performance as much as possible. In the few years of his work, he has accumulated a lot of experience with NGINX, and he is now an expert in optimizing it to serve web content. Tatsuhiko Kubo is a software engineer in infrastructure engineering and has a strong passion for open source software. His contribution to the development and publication of works on various kinds of open source software is his life's work. His usual work includes the development and operation of various kinds of middleware. www.it-ebooks.info
📄 Page 7
www.PacktPub.com Support files, eBooks, discount offers, and more For support files and downloads related to your book, please visit www.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. TM https://www2.packtpub.com/books/subscription/packtlib Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read 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 a 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 9 entirely free books. Simply use your login credentials for immediate access. www.it-ebooks.info
📄 Page 8
[ i ] Table of Contents Preface vii Chapter 1: Working with NGINX 1 The NGINX architecture 2 Installing NGINX from source 3 Build requirements 4 The ANSI C compiler and build system 4 libatomic_ops and AIO – optional requirements 5 Perl – an optional requirement 5 The Perl Compatible Regular Expressions library – an optional requirement 5 OpenSSL – an optional requirement 6 Zlib – an optional requirement 6 Configuring NGINX 6 Configuring NGINX defaults 7 Configuring NGINX modules 8 Configuring NGINX for the Web 9 Configuring NGINX for e-mail 13 Configuring third-party modules 13 NGINX – the complete package 13 Building and installing NGINX 15 Deploying in NGINX 16 Deploying NGINX 17 Summary 20 Chapter 2: Benchmarking the Server 21 Performance testing 21 Using timeouts 22 Baselines 23 A note about tools 23 Generating metrics using Siege 24 Installing Siege 24 Running Siege 26 www.it-ebooks.info
📄 Page 9
Table of Contents [ ii ] Siege test results 28 Generating metrics using Apache JMeter 29 Installing JMeter 29 Installing Java 30 Setting JAVA_HOME 31 Running JMeter 32 Components of JMeter 33 Threads 33 Sampler 33 Configuration elements 33 Assertions 34 Listeners 34 Test plan 34 Building JMeter test plans 34 JMeter test results 40 Summary 41 Chapter 3: Tweaking NGINX Configuration 43 NGINX configuration syntax 43 Configuring NGINX workers 45 worker_processes 45 accept_mutex 46 accept_mutex_delay 46 worker_connections 47 worker_rlimit_nofile 47 multi_accept 48 use 48 Configuring NGINX I/O 50 Sendfile 51 Direct I/O 51 Asynchronous I/O 52 Mixing them up 53 Configuring TCP 54 TCP_NODELAY 54 TCP_CORK 55 Setting them up 55 Setting up the server 56 Measuring gains 57 Summary 58 Chapter 4: Controlling Buffers, Timeouts, and Compression 59 Configuring buffers 59 client_body_buffer_size 60 www.it-ebooks.info
📄 Page 10
Table of Contents [ iii ] client_max_body_size 60 client_body_in_file_only 60 client_body_in_single_buffer 61 client_body_temp_path 61 client_header_buffer_size 62 large_client_header_buffers 62 Configuring timeouts 62 keepalive 63 keepalive_timeout 64 keepalive_requests 65 keepalive_disable 65 send_timeout 66 client_body_timeout 66 client_header_timeout 66 Compression 67 ngx_http_gzip_module 67 gzip 67 gzip_comp_level 67 gzip_min_length 68 gzip_types 68 gzip_proxied 68 gzip_http_version 69 gzip_vary 69 gzip_disable 69 ngx_http_gzip_static_module 70 gzip_static 70 ngx_http_gunzip_module 70 gunzip 71 Configuring logs 71 access_log 71 log_format 72 log_subrequest 72 error_log 72 log_not_found 73 Setting up the server 73 Measuring gains 74 Summary 77 Chapter 5: Configuring the Network Stack 79 TCP buffers 79 The TCP window 82 TCP control algorithms 83 TCP states 84 www.it-ebooks.info
📄 Page 11
Table of Contents [ iv ] Raising server limits 86 The queue size 86 The listen socket queue size 86 Half-opened connections 87 Ephemeral ports 87 Open files 88 Setting up the server 89 Summary 90 Chapter 6: Using NGINX Cache 91 Caching static content 91 open_file_cache 92 open_file_cache_valid 92 open_file_cache_min_uses 93 open_file_cache_errors 93 Setting up the server 93 Caching dynamic content 94 Using FastCGI and the related cache 94 Installing PHP 94 Deploying PHP scripts 96 Configuring php-fpm 98 Configuring NGINX FastCGI 99 Setting up the server 102 Configuring the FastCGI cache 103 Using Proxy and the related cache 107 Installing Python and Flask 108 Building a Python application 109 Configuring NGINX Proxy 111 proxy_pass 112 proxy_method 112 proxy_set_header 112 proxy_http_version 113 proxy_pass_request_headers / proxy_pass_request_body 113 proxy_ignore_headers 113 proxy_connect_timeout / proxy_send_timeout / proxy_read_timeout 114 proxy_store / proxy_store_access 114 proxy_cache_path 115 proxy_cache_key 115 proxy_cache 116 proxy_cache_valid 116 proxy_no_cache 116 proxy_cache_bypass 117 proxy_cache_methods 117 www.it-ebooks.info
📄 Page 12
Table of Contents [ v ] proxy_cache_use_stale 117 Setting up the server 118 Using Memcache 119 memcached_pass 120 memcached_connect_timeout / memcached_send_timeout / memcached_read_timeout 120 memcached_bind 121 Setting up the server 121 Measuring gains 123 Summary 124 Chapter 7: Extending NGINX 125 The Lua scripting language 125 The NGINX Lua module 128 Installation 128 Directives 130 lua_package_path 131 lua_shared_dict 131 init_by_lua/init_by_lua_file 131 set_by_lua/set_by_lua_file 131 content_by_lua/content_by_lua_file 132 header_filter_by_lua/header_filter_by_lua_file 132 body_filter_by_lua/body_filter_by_lua_file 133 access_by_lua/access_by_lua_file 133 rewrite_by_lua/rewrite_by_lua_file 134 log_by_lua/log_by_lua_file 134 The NGINX-Lua API 134 ngx.arg 135 ngx.var.varName 135 ngx.say/ngx.print 135 ngx.location.capture/ngx.location.capture_multi 135 ngx.ctx 136 ngx.status 136 ngx.header.HeaderField 136 ngx.req.functions 136 ngx.shared.DictionaryName 137 ngx.socket.tcp 137 NGINX Lua libraries 138 Setting up the server 139 The problem statement 139 Statement 139 Summary 142 Index 143 www.it-ebooks.info
📄 Page 13
www.it-ebooks.info
📄 Page 14
[ vii ] Preface NGINX is one of the most widely used web servers on the Internet. The server is often named when one is looking to deliver better performance with the same hardware. The server has a state-of-the-art event-based architecture, which enables it to deliver hundreds of thousands of concurrent connections on standard hardware. As a first step, adopting NGINX leads to better results. However, as with any piece of software, NGINX can also be optimized to serve content faster. This book provides ways to optimize NGINX for last-mile performance. It also aims to provide insights into the NGINX architecture for you to understand it better. The book is not an NGINX learning book and is intended for people with some experience with NGINX. The book only explains those parts of the NGINX configuration that have an impact on performance. Besides NGINX optimization, the book also talks about the process of benchmarking and about baselines to quantify the gains made. This is an end-to-end book that helps you to tweak the NGINX server's performance. What this book covers Chapter 1, Working with NGINX, talks about NGINX's high-performance architecture. It also explains the various modules available in NGINX. The chapter lists details of the amenability of the server to customization. In the end, the chapter builds a simple configuration to deploy example web pages in NGINX. Chapter 2, Benchmarking the Server, explains performance testing to generate baselines. Siege and JMeter are tools used to measure and benchmark the performance of your server. The chapter aims to generate metrics for the web pages deployed in Chapter 1, Working with NGINX, which can be compared to see performance changes. Chapter 3, Tweaking NGINX Configuration, covers the parameters: Worker and Worker_process, the use method and multi_accept, Sendfile, directio and aio, tcp_nodely, and tcp_nopush. www.it-ebooks.info
📄 Page 15
Preface [ viii ] Chapter 4, Controlling Buffers, Timeouts, and Compression, lists the features: keeplive, send timeouts, client buffers, gzip compression, and controlling logs. Chapter 5, Configuring the Network Stack, lists details about tweaking the TCP options to achieve better network utilization. This chapter also talks about various server defaults that need to be customized. Chapter 6, Using NGINX Cache, shows that NGINX can cache static content, as well as dynamic content. The server provides various directives to cache content. The chapter lists ways to use caches, namely FastCGI Cache, NGINX Proxy Cache, and Memcache, to render content. Chapter 7, Extending NGINX, provides details of HttpLuaModule, which can be used to extend NGINX for all kinds of activities. The module enables support for Lua, which is a powerful, fast, lightweight, embeddable scripting language. The chapter aims to build an SEO check using Lua. What you need for this book You'll require the following software: • NGINX 1.7.x • JMeter • Siege • PHP and PHP-FPM • Python and Flask • Memcache Who this book is for A system administrator or developer looking for ways to extract the maximum performance from NGINX will find this book useful. If you are facing various challenges, such as handling more users from the same system and aiming to load your website pages faster, then this book is for you. 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. www.it-ebooks.info
📄 Page 16
Preface [ ix ] 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 worker then uses the available event-notification interfaces, such as epoll and kqueue, to process each connection in an efficient event loop." A block of code is set as follows: location /hello { alias "$ABSOLUTE_PATH_TO_CODE"; } Any command-line input or output is written as follows: curl -I http://nginx.org New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "This module allows us to return either the 444 error or the 204 error in low-memory conditions." 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 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 feedback@packtpub.com, 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. www.it-ebooks.info
📄 Page 17
Preface [ x ] 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 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 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 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 https://www.packtpub.com/books/ content/support and enter the name of the book in the search field. The required information will appear under the Errata section. 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 copyright@packtpub.com 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 questions@packtpub.com if you are having a problem with any aspect of the book, and we will do our best to address it. www.it-ebooks.info
📄 Page 18
[ 1 ] Working with NGINX Igor Sysoev started developing NGINX in 2002. The first version was aimed at delivering static content on web scale. It was released to the public in 2004, thus solving Daniel Kegel's C10K problem of 10,000 simultaneous connections. NGINX adapted a modular, asynchronous, event-based, nonblocking architecture, which works well to deliver tens of thousands of concurrent connections on a server with typical hardware. In this chapter, we will cover the following topics: • The NGINX architecture • Installing NGINX from source • Configuring NGINX for HTTP • Deploying a "Hello world!" page www.it-ebooks.info
📄 Page 19
Working with NGINX [ 2 ] The NGINX architecture NGINX has its foundation in event-based architecture (EBA). In EBA, components interact predominantly using event notifications instead of direct method calls. These event notifications, occurring from different tasks, are then queued for processing by an event handler. The event handler runs in an event loop, where it processes an event, de-queues it, and then moves on to the next event. Thus, the work executed by a thread is very similar to that of a scheduler, multiplexing multiple connections to a single flow of execution. The following diagram shows this: When compared with the thread-based architecture, EBA gives better performance output. In EBA, there are a fixed number of threads performing tasks and no new threads are formed. Thus, we achieve better utilization of the CPU and an improved memory footprint. There is also no overhead of excessive context switching and no need for a thread stack for each connection. Ideally, the CPU becomes the only apparent bottleneck of an event-driven application. NGINX runs one master process and several worker processes. The master process reads/evaluates the configuration and maintains the worker processes. All request processing is done by the worker processes. NGINX does not start workers for every request. Instead, it has a fixed pool of workers for request processing. Each worker accepts new requests from a shared listen queue. The worker then uses the available event-notification interfaces, such as epoll and kqueue, to process each connection in an efficient event loop. The idea is to optimize the utilization of the server's resources using nonblocking/asynchronous mechanisms when possible. By doing so, each worker is able to process thousands of connections. The following diagram shows this: www.it-ebooks.info
📄 Page 20
Chapter 1 [ 3 ] NGINX has an extensible modular architecture. There is a core module (ngx_core_ module), which is responsible for connection handling. Then, there are modules for every kind of processing that NGINX offers, for instance an HTTP module (ngx_http_core_module) for HTTP processing, an e-mail module (ngx_mail_core_ module) for e-mail processing, a proxy module (ngx_http_proxy_module), and so on. The modular system is quite extensible and allows third-party developers to extend NGINX beyond its existing capabilities. Each worker loads a chain of modules as specified in the NGINX configuration. Every request that a worker handles goes through the loaded chain of modules. Installing NGINX from source NGINX can be downloaded from http://nginx.org/. The site provides a stable package and a mainline version package. Both versions are quite stable, and either of them can be used to build NGINX for production environments. The mainline version contains all active development. This essentially means that all new features and noncritical bug fixes are developed here. At times, this may also cause certain third-party modules to break due to a change in the internal APIs. The stable version only gets the critical bug fixes. New features and noncritical bug fixes are not ported to the stable version. NGINX has a well-maintained documentation available at http://nginx.org/en/docs/. The documentation is a great source of information related to NGINX features, internals, and recipes. www.it-ebooks.info
The above is a preview of the first 20 pages. Register to read the complete e-book.

💝 Support Author

0.00
Total Amount (¥)
0
Donation Count

Login to support the author

Login Now
Back to List