O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Nginx HTTP Server - Fourth Edition

Book Description

Make the most of your infrastructure and serve pages faster than ever with Nginx.

About This Book

  • Discover possible interactions between Nginx and Apache to get the best of both worlds
  • Learn to exploit the features offered by Nginx for your web applications
  • Get your hands on the most updated version of Nginx (1.13.2) to support all your web administration requirements

Who This Book Is For

This book is a perfect match to web administrators who are interested in solutions to optimize their infrastructure. Whether you are looking into replacing your existing web server software or integrating a new tool to cooperate with applications that are already up and running, this book is your ideal resource.

What You Will Learn

  • Download and install Nginx on your system
  • Prepare a basic configuration and test your initial setup
  • Discover the core functionality of the HTTP module
  • Make the most of first- and third-party Nginx modules
  • Set up Nginx to work with PHP, Python, and other applications
  • Learn how to set up Nginx to work with Apache
  • Fully replace Apache with Nginx
  • Optimize your architecture with threads or load balancing
  • Identify errors in configuration and learn basic troubleshooting techniques
  • Consult the exhaustive directive and module index for reference

In Detail

Nginx is a lightweight HTTP server designed for high-traffic websites, with network scalability as the primary objective. With the advent of high-speed internet access, short loading times and fast transfer rates have become a necessity.

This book is a detailed guide to setting up Nginx in ways that correspond to actual production situations: as a standalone server, as a reverse proxy, interacting with applications via FastCGI, and more. In addition, this complete direct reference will be indispensable at all stages of the configuration and maintenance processes. This book mainly targets the most recent version of Nginx (1.13.2) and focuses on all the new additions and improvements, such as support for HTTP/2, improved dynamic modules, security enhancements, and support for multiple SSL certificates. This book is the perfect companion for both Nginx beginners and experienced administrators. For beginners, it will take you through the complete process of setting up this lightweight HTTP server on your system and configuring its various modules so that it does exactly what you need quickly and securely. For more experienced administrators, this book provides different approaches that can help you make the most of your current infrastructure.

Nginx can be employed in many situations, whether you are looking to construct an entirely new web-serving architecture or simply want to integrate an efficient tool to optimize your site loading speeds.

Style and approach

This book aims to serve as a handy reference of all Nginx first-party modules and directives, allowing the reader to develop their own web configuration more efficiently.

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. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
      1. Conventions used
    4. Get in touch
      1. Reviews
  2. Downloading and Installing Nginx
    1. Installing via package managers
      1. Nginx provided packages
    2. Compiling from source
      1. GNU Compiler Collection
      2. The PCRE library
      3. The zlib library
      4. OpenSSL
    3. Downloading Nginx
      1. Websites and resources
      2. Version branches
      3. Features
      4. Downloading and extracting
    4. Configure options
      1. The easy way
      2. Path options
      3. Prerequisite options
      4. Module options
        1. Modules enabled by default
        2. Modules disabled by default
      5. Miscellaneous options
      6. Configuration examples
        1. About the prefix switch
        2. Regular HTTP and HTTPS servers
        3. All modules enabled
        4. Mail server proxy
      7. Build configuration issues
        1. Make sure you installed the prerequisites
        2. Directories exist and are writable
      8. Compiling and installing
    5. Controlling the Nginx service
      1. Daemons and services
      2. User and group
      3. Nginx command-line switches
      4. Starting and stopping the daemon
      5. Testing the configuration
      6. Other switches
    6. Adding Nginx as a system service
      1. System V scripts
      2. About init scripts
      3. Init script for older Debian-based distributions
      4. Init script for SystemD-based distributions
      5. Installing the script
        1. Debian-based distributions
        2. Red Hat-based distributions
    7. Nginx Plus
    8. Summary
  3. Basic Nginx Configuration
    1. Configuration file syntax
      1. Configuration directives
      2. Organization and inclusions
      3. Directive blocks
      4. Advanced language rules
        1. Directives accept specific syntaxes
        2. Diminutives in directive values
        3. Variables
        4. String values
    2. Base module directives
      1. What are base modules?
      2. Nginx process architecture
      3. Core module directives
      4. Events module
      5. Configuration module
      6. Necessary adjustments
    3. Testing your server
      1. Creating a test server
      2. Performance tests
        1. Httperf
        2. Autobench
        3. OpenWebLoad
      3. Upgrading Nginx gracefully
    4. Summary
  4. HTTP Configuration
    1. HTTP core module
      1. Structure blocks
    2. Module directives
      1. Socket and host configuration
        1. listen
        2. server_name
        3. server_name_in_redirect
        4. server_names_hash_max_size
        5. server_names_hash_bucket_size
        6. port_in_redirect
        7. absolute_redirect
        8. tcp_nodelay
        9. tcp_nopush
        10. sendfile
        11. sendfile_max_chunk
        12. send_lowat
        13. reset_timedout_connection
      2. Paths and documents
        1. root
        2. alias
        3. error_page
        4. if_modified_since
        5. index
        6. recursive_error_pages
        7. try_files
      3. Client requests
        1. keepalive_requests
        2. keepalive_timeout
        3. keepalive_disable
        4. send_timeout
        5. client_body_in_file_only
        6. client_body_in_single_buffer
        7. client_body_buffer_size
        8. client_body_temp_path
        9. client_body_timeout
        10. client_header_buffer_size
        11. client_header_timeout
        12. client_max_body_size
        13. large_client_header_buffers
        14. lingering_time
        15. lingering_timeout
        16. lingering_close
        17. ignore_invalid_headers
        18. chunked_transfer_encoding
        19. max_ranges
      4. MIME types
        1. types
        2. default_type
        3. types_hash_max_size
        4. types_hash_bucket_size
      5. Limits and restrictions
        1. limit_except
        2. limit_rate
        3. limit_rate_after
        4. satisfy
        5. internal
      6. File processing and caching
        1. disable_symlinks
        2. directio
        3. directio_alignment
        4. open_file_cache
        5. open_file_cache_errors
        6. open_file_cache_min_uses
        7. open_file_cache_valid
        8. read_ahead
      7. Other directives
        1. log_not_found
        2. log_subrequest
        3. merge_slashes
        4. msie_padding
        5. msie_refresh
        6. resolver
        7. resolver_timeout
        8. server_tokens
        9. underscores_in_headers
        10. variables_hash_max_size
        11. variables_hash_bucket_size
        12. post_action
    3. Using HTTP/2
      1. Module directives
        1. http2_chunk_size
        2. http2_body_preread_size
        3. http2_idle_timeout
        4. http2_max_concurrent_streams
        5. http2_max_field_size
        6. http2_max_header_size
        7. http2_max_requests
        8. http2_recv_buffer_size
        9. http2_recv_timeout
      2.  Module variables
    4. Module variables
      1. Request headers
      2. Response headers
      3. Nginx generated
    5. The location block
      1. Location modifier
        1. The = modifier
        2. No modifier
        3. The ~ modifier
        4. The ~* modifier
        5. The ^~ modifier
        6. The @ modifier
      2. Search order and priority
        1. Case 1
        2. Case 2
        3. Case 3
    6. Summary
  5. Module Configuration
    1. Rewrite module
      1. Reminder on regular expressions
        1. Purpose
        2. PCRE syntax
        3. Quantifiers
        4. Captures
      2. Internal requests
        1. error_page
        2. Rewrite
        3. Infinite loops
        4. Server Side Includes
      3. Conditional structure
      4. Directives
      5. Common rewrite rules
        1. Performing a search
        2. User profile page
        3. Multiple parameters
        4. Wikipedia-like
        5. News website article
        6. Discussion board
    2. SSI module
      1. Module directives and variables
      2. SSI commands
        1. File includes
        2. Working with variables
        3. Conditional structure
        4. Configuration
    3. Additional modules
      1. Website access and logging
        1. Index
        2. Autoindex
        3. Random index
        4. Log
      2. Limits and restrictions
        1. Auth_basic module
        2. Access
        3. Limit connections
        4. Limit request
        5. Auth request
      3. Content and encoding
        1. Empty GIF
        2. FLV and MP4
        3. HTTP headers
        4. Addition
        5. Substitution
        6. Gzip filter
        7. Gzip static
        8. Gunzip filter
        9. Charset filter
        10. Memcached
        11. Image filter
        12. XSLT
      4. About your visitors
        1. Browser
        2. Map
        3. Geo
        4. GeoIP
        5. UserID filter
        6. Referer
        7. Real IP
      5. Split clients
      6. SSL and security
        1. SSL
        2. Setting up an SSL certificate
          1. SSL stapling
        3. Secure link
      7. Other miscellaneous modules
        1. Stub status
        2. Degradation
        3. Google-perftools
        4. WebDAV
      8. Third-party modules
    4. Summary
  6. PHP and Python with Nginx
    1. Introduction to FastCGI
      1. Understanding the CGI mechanism
      2. Common Gateway Interface
      3. Fast Common Gateway Interface (FastCGI)
      4. uWSGI and SCGI
      5. Main directives
      6. FastCGI caching and buffering
    2. PHP with Nginx
      1. Architecture
      2. PHP-FPM
      3. Setting up PHP and PHP-FPM
        1. Downloading and extracting
        2. Requirements
        3. Building PHP
        4. Post-install configuration
        5. Running and controlling
      4. Nginx configuration
    3. Python and Nginx
      1. Django
      2. Setting up Python and Django
        1. Python
        2. Django
        3. Starting the FastCGI process manager
      3. Nginx configuration
    4. Summary
  7. Nginx as an Application Server
    1. The reverse proxy mechanism
    2. Nginx proxy module
      1. Main directives
      2. Caching, buffering, and temporary files
      3. Limits, timeouts, and errors
      4. SSL-related directives
      5. Other directives
      6. Variables
    3. Nginx and microservices
    4. nginScript
      1. Supported features
      2. Unsupported features
      3. Main directives
      4. Handler example
    5. Nginx Unit
    6. Summary
  8. Apache and Nginx Together
    1. How Nginx and Apache benefit from each other
      1. An example case
    2. Configuring Apache and Nginx
      1. Reconfiguring Apache
        1. Configuration overview
        2. Resetting the port number
        3. Accepting local requests only
      2. Configuring Nginx
        1. Enabling proxy options
        2. Separating content
      3. Advanced configuration
    3. Improving the reverse proxy architecture
      1. Forwarding the correct IP address
      2. SSL issues and solutions
      3. Server control panel issues
    4. Summary
  9. From Apache to Nginx
    1. Nginx versus Apache
      1. Features
        1. Core and functioning
        2. General functionality
      2. Flexibility and community
      3. Performance
      4. Usage
      5. Conclusion
    2. Porting your Apache configuration
      1. Directives
      2. Modules
      3. Virtual hosts and configuration sections
        1. Configuration sections
        2. Creating a virtual host
      4. .htaccess files
        1. Reminder on Apache .htaccess files
        2. Nginx equivalence
    3. Rewrite rules
      1. General remarks
        1. On the location
        2. On the syntax
        3. RewriteRule
      2. WordPress
      3. MediaWiki
      4. vBulletin
    4. Summary
  10. Introduction to Load Balancing and Optimization
    1. Introduction to load balancing
      1. Understanding the concept of load balancing
      2. Session affinity
      3. The upstream module
      4. Request distribution mechanisms
    2. Using Nginx as a TCP load balancer
      1. The stream module
      2. An example of MySQL load balancing
    3. Thread pools and IO mechanisms
      1. Relieving worker processes
      2. AIO, Sendfile, and DirectIO
    4. Summary
  11. Case Studies
    1. Deploying a WordPress site
      1. Preparing your server and obtaining WordPress
        1. System requirements
        2. PHP configuration
        3. MySQL configuration
        4. Downloading and extracting WordPress
      2. Nginx configuration
        1. HTTP block
        2. Server block
        3. Location blocks
      3. WordPress configuration
    2. Securing communications with HTTPS
      1. Self-signed certificates and certificate authorities
      2. Obtaining your SSL certificate
      3. Enabling HTTPS in your Nginx configuration
      4. Setting up and testing HTTP/2
    3. Creating your ownCloud drive
      1. Getting ownCloud
      2. Nginx configuration
      3. Setting up a self-signed certificate
    4. Summary
  12. Troubleshooting
    1. General tips on Nginx troubleshooting
      1. Checking access permissions
      2. Testing your configuration
      3. Have you reloaded the service?
      4. Checking logs
    2. Installing a log parser
    3. Install issues
    4. The 403 forbidden custom error page
    5. 400 Bad Request
    6. Truncated or invalid FastCGI responses
    7. Location block priorities
    8. If block issues
      1. Inefficient statements
        1. Unexpected behavior
    9. Summary
  13. Other Books You May Enjoy
    1. Leave a review - let other readers know what you think