You are previewing NGINX High Performance.
O'Reilly logo
NGINX High Performance

Book Description

Optimize NGINX for high-performance, scalable web applications

In Detail

NGINX is one of the most common free, open source web servers. Its performance-oriented architecture and small footprint makes it an ideal choice for high-traffic websites.

NGINX offers great performance and optimal resource utilization to its administrators. This practical guide walks you through how to tune one of the leading free open source web servers to attain optimal performance for high-traffic sites. It also explores ways to improve network utilization for high loads.

The tour starts with an overview of the NGINX architecture. You will build and configure NGINX for optimal utilization of the hardware available. The book demonstrates various practices to improve last mile content delivery by using timeouts, caching, and compression. You'll also discover various free open source tools to test and benchmark web server performance, allowing you to verify NGINX performance at every step.

What You Will Learn

  • Compile and run NGINX from source

  • Measure NGINX performance and create baselines

  • Tweak NGINX configuration for last-mile performance

  • Work with PHP, Python, and other languages using FCGI

  • Learn to integrate with other servers over HTTP

  • Set up Memcache for a performance boost

  • Fine-tune the TCP stack for improved network utilization

  • Construct NGINX extensions for various purposes

  • Downloading the example code for this book. 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.

    Table of Contents

    1. NGINX High Performance
      1. Table of Contents
      2. NGINX High Performance
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why Subscribe?
          2. Free Access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Working with NGINX
        1. The NGINX architecture
        2. Installing NGINX from source
          1. Build requirements
            1. The ANSI C compiler and build system
            2. libatomic_ops and AIO – optional requirements
            3. Perl – an optional requirement
            4. The Perl Compatible Regular Expressions library – an optional requirement
            5. OpenSSL – an optional requirement
            6. Zlib – an optional requirement
          2. Configuring NGINX
            1. Configuring NGINX defaults
            2. Configuring NGINX modules
            3. Configuring NGINX for the Web
            4. Configuring NGINX for e-mail
            5. Configuring third-party modules
            6. NGINX – the complete package
          3. Building and installing NGINX
          4. Deploying in NGINX
          5. Deploying NGINX
        3. Summary
      9. 2. Benchmarking the Server
        1. Performance testing
          1. Using timeouts
        2. Baselines
          1. A note about tools
        3. Generating metrics using Siege
          1. Installing Siege
          2. Running Siege
          3. Siege test results
        4. Generating metrics using Apache JMeter
          1. Installing JMeter
          2. Installing Java
            1. Setting JAVA_HOME
          3. Running JMeter
          4. Components of JMeter
            1. Threads
            2. Sampler
            3. Configuration elements
            4. Assertions
            5. Listeners
            6. Test plan
          5. Building JMeter test plans
          6. JMeter test results
        5. Summary
      10. 3. Tweaking NGINX Configuration
        1. NGINX configuration syntax
        2. Configuring NGINX workers
          1. worker_processes
          2. accept_mutex
          3. accept_mutex_delay
          4. worker_connections
          5. worker_rlimit_nofile
          6. multi_accept
          7. use
        3. Configuring NGINX I/O
          1. Sendfile
          2. Direct I/O
          3. Asynchronous I/O
          4. Mixing them up
        4. Configuring TCP
          1. TCP_NODELAY
          2. TCP_CORK
          3. Setting them up
        5. Setting up the server
          1. Measuring gains
        6. Summary
      11. 4. Controlling Buffers, Timeouts, and Compression
        1. Configuring buffers
          1. client_body_buffer_size
          2. client_max_body_size
          3. client_body_in_file_only
          4. client_body_in_single_buffer
          5. client_body_temp_path
          6. client_header_buffer_size
          7. large_client_header_buffers
        2. Configuring timeouts
          1. keepalive
            1. keepalive_timeout
            2. keepalive_requests
            3. keepalive_disable
          2. send_timeout
          3. client_body_timeout
          4. client_header_timeout
        3. Compression
          1. ngx_http_gzip_module
            1. gzip
            2. gzip_comp_level
            3. gzip_min_length
            4. gzip_types
            5. gzip_proxied
            6. gzip_http_version
            7. gzip_vary
            8. gzip_disable
          2. ngx_http_gzip_static_module
            1. gzip_static
          3. ngx_http_gunzip_module
            1. gunzip
        4. Configuring logs
          1. access_log
          2. log_format
          3. log_subrequest
          4. error_log
          5. log_not_found
        5. Setting up the server
          1. Measuring gains
        6. Summary
      12. 5. Configuring the Network Stack
        1. TCP buffers
          1. The TCP window
          2. TCP control algorithms
        2. TCP states
        3. Raising server limits
          1. The queue size
          2. The listen socket queue size
          3. Half-opened connections
          4. Ephemeral ports
          5. Open files
        4. Setting up the server
        5. Summary
      13. 6. Using NGINX Cache
        1. Caching static content
          1. open_file_cache
          2. open_file_cache_valid
          3. open_file_cache_min_uses
          4. open_file_cache_errors
          5. Setting up the server
        2. Caching dynamic content
          1. Using FastCGI and the related cache
            1. Installing PHP
            2. Deploying PHP scripts
            3. Configuring php-fpm
            4. Configuring NGINX FastCGI
              1. fastcgi_pass
              2. fastcgi_param
              3. fastcgi_index
              4. fastcgi_split_path_info
              5. fastcgi_bind
              6. fastcgi_ignore_headers
              7. fastcgi_pass_request_headers / fastcgi_pass_request_body
              8. fastcgi_connect_timeout / fastcgi_send_timeout / fastcgi_read_timeout
              9. fastcgi_store / fastcgi_store_access
            5. Setting up the server
            6. Configuring the FastCGI cache
              1. fastcgi_cache_path
              2. fastcgi_cache_key
              3. fastcgi_cache
              4. fastcgi_cache_valid
              5. fastcgi_no_cache
              6. fastcgi_cache_bypass
              7. fastcgi_cache_methods
              8. fastcgi_cache_use_stale
              9. Setting up the server
          2. Using Proxy and the related cache
            1. Installing Python and Flask
          3. Building a Python application
        3. Configuring NGINX Proxy
          1. proxy_pass
          2. proxy_method
          3. proxy_set_header
          4. proxy_http_version
          5. proxy_pass_request_headers / proxy_pass_request_body
          6. proxy_ignore_headers
          7. proxy_connect_timeout / proxy_send_timeout / proxy_read_timeout
          8. proxy_store / proxy_store_access
          9. proxy_cache_path
          10. proxy_cache_key
          11. proxy_cache
          12. proxy_cache_valid
          13. proxy_no_cache
          14. proxy_cache_bypass
          15. proxy_cache_methods
          16. proxy_cache_use_stale
          17. Setting up the server
        4. Using Memcache
          1. memcached_pass
          2. memcached_connect_timeout / memcached_send_timeout / memcached_read_timeout
          3. memcached_bind
          4. Setting up the server
          5. Measuring gains
        5. Summary
      14. 7. Extending NGINX
        1. The Lua scripting language
        2. The NGINX Lua module
          1. Installation
          2. Directives
            1. lua_package_path
            2. lua_shared_dict
            3. init_by_lua/init_by_lua_file
            4. set_by_lua/set_by_lua_file
            5. content_by_lua/content_by_lua_file
            6. header_filter_by_lua/header_filter_by_lua_file
            7. body_filter_by_lua/body_filter_by_lua_file
            8. access_by_lua/access_by_lua_file
            9. rewrite_by_lua/rewrite_by_lua_file
            10. log_by_lua/log_by_lua_file
        3. The NGINX-Lua API
          1. ngx.arg
          2. ngx.var.varName
          3. ngx.say/ngx.print
          4. ngx.location.capture/ngx.location.capture_multi
          5. ngx.ctx
          6. ngx.status
          7. ngx.header.HeaderField
          8. ngx.req.functions
          9. ngx.shared.DictionaryName
          10. ngx.socket.tcp
        4. NGINX Lua libraries
        5. Setting up the server
          1. The problem statement
          2. Statement
        6. Summary
      15. Index