Nginx HTTP Server - Third Edition

Book description

Harness the power of Nginx to make the most of your infrastructure and serve pages faster than ever

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 the newest techniques available, designed with the latest version in mind

Who This Book Is For

By covering both the early setup stages and advanced topics, this book suits web administrators who 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. If you, your visitors, and your operating system have been disappointed by Apache, this book is exactly what you need.

What You Will Learn

  • Get to know the basics of the Nginx configuration: syntax, structure, and semantics
  • Understand the advanced load balancing functionality of Nginx and the newest innovative IO mechanisms
  • Create virtual host configurations effortlessly
  • Discover all the first-party modules: how to enable, configure, and use them
  • Establish advanced rewrite rules with the Nginx Rewrite module
  • Set up Nginx to work with PHP, Python, and more via FastCGI
  • Configure Nginx to work as frontend for your existing HTTP server
  • Manipulate configuration files with ease and adapt them to various situations
  • Discover the common pitfalls and find out how to avoid them

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 free, open source solution will either come as a full replacement of other software such as Apache, or stand in front of your existing infrastructure to improve its overall speed.

This book is a detailed guide to setting up Nginx in different 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 directive reference will be your best friend at all stages of the configuration and maintenance processes.

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 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 tutorial-style book includes detailed instructions on each of the processes it describes, as well as step-by-step tutorials, commented configuration sections, and in-depth module descriptions, so you can make the most of the performance potential offered by Nginx.

Table of contents

  1. Nginx HTTP Server Third Edition
    1. Table of Contents
    2. Nginx HTTP Server Third Edition
    3. Credits
    4. About the Author
    5. About the Reviewer
    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. Downloading and Installing Nginx
      1. Setting up the prerequisites
        1. The GNU Compiler Collection
        2. The PCRE library
        3. The zlib library
        4. OpenSSL
      2. Downloading Nginx
        1. Websites and resources
        2. Version branches
        3. Features
        4. Downloading and extracting
      3. Configure options
        1. The easy way
        2. Path options
        3. Prerequisites 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 must be writable
        8. Compiling and installing the program
      4. 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
      5. Adding Nginx as a system service
        1. System V scripts
        2. About init scripts
        3. The init script for Debian-based distributions
        4. The init script for Red Hat–based distributions
        5. Installing the script
          1. Debian-based distributions
          2. Red Hat–based distributions
      6. NGINX Plus
      7. Summary
    9. 2. 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. The Nginx process architecture
        3. Core module directives
        4. The Events module
        5. The 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
    10. 3. HTTP Configuration
      1. The 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. tcp_nodelay
          8. tcp_nopush
          9. sendfile
          10. sendfile_max_chunk
          11. send_lowat
          12. 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. Module variables
        1. Request headers
        2. Response headers
        3. Nginx generated
      4. 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
      5. Summary
    11. 4. Module Configuration
      1. The 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. SPDY
          4. Secure link
        7. Other miscellaneous modules
          1. Stub status
          2. Degradation
          3. Google-perftools
          4. WebDAV
        8. Third-party modules
      4. Summary
    12. 5. PHP and Python with Nginx
      1. An introduction to FastCGI
        1. Understanding the CGI mechanism
        2. The Common Gateway Interface
        3. Fast Common Gateway Interface
        4. uWSGI and SCGI
        5. The 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. The Nginx configuration
      4. Summary
    13. 6. Apache and Nginx Together
      1. Nginx as reverse proxy
        1. Understanding the issue
        2. The reverse proxy mechanism
        3. Advantages and disadvantages of the mechanism
      2. The 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. 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 the content
        3. Advanced configuration
      4. Improving the reverse proxy architecture
        1. Forwarding the correct IP address
        2. SSL issues and solutions
        3. Server control panel issues
      5. Summary
    14. 7. 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. A recap 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
    15. 8. Introducing Load Balancing and Optimization
      1. An 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 I/O mechanisms
        1. Relieving worker processes
        2. AIO, Sendfile, and DirectIO
      4. Summary

    16. 9. 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 blocks
          2. The 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 SPDY
      3. Creating your ownCloud drive
        1. Getting ownCloud
        2. Nginx configuration
        3. Setting up a self-signed certificate
      4. Summary
    17. 10. Troubleshooting
      1. General tips on troubleshooting
        1. Checking access permissions
        2. Testing your configuration
        3. Have you reloaded the service?
        4. Checking logs
      2. Install issues
      3. The 403 Forbidden custom error page
      4. 400 Bad request
      5. Truncated or invalid FastCGI responses
      6. Location block priorities
      7. If block issues
        1. Inefficient statements
          1. Unexpected behavior
      8. Summary
    18. Index

Product information

  • Title: Nginx HTTP Server - Third Edition
  • Author(s): Clément Nedelcu
  • Release date: November 2015
  • Publisher(s): Packt Publishing
  • ISBN: 9781785280337