Nginx 1 Web Server Implementation Cookbook Over 100 recipes to master using the Nginx HTTP server and reverse proxy Dipankar Sarkar [ 11 open so " *' '" i I community experience d PUBLISHING community experience distilled urce'^ BIRMINGHAM - MUMBAI
Preface 1 Chapter 1: The Core HTTP Module 7 Introduction 8 Installing new modules and compiling Nginx 8 Running Nginx in debug mode 10 Easy reloading of Nginx using the CLI 11 Splitting configuration files for better management 13 Setting up multiple virtual hosts 16 Setting up a default catch-all virtual host 17 Using wildcards in virtual hosts 18 Setting up the number of worker processes correctly 19 Increasing the size of uploaded files 20 Using dynamic SSI for simple sites 22 Adding content before and after a particular page 23 Enabling auto indexing of a directory 25 Serving any random web page from a directory 27 Serving cookies for identifying and logging users 29 Re-encoding the response to another encoding 30 Enabling Gzip compression on some content types 31 Setting up 404 and other error pages 32 Chapter 2: AH About Rewrites: The Rewrite Module 35 Introduction 35 Setting up a simple redirect 36 Using variables in your rewrite 37 Using cookies for your rewrites 38 Using browser agents for your rewrites 40 Using rate limits as a condition for rewrites 41 Blocking requests based on HTTP referrers 43 Serving maintenance page when deploying 44
Setting up a WordPress site with static file serving 46 Setting up a Drupal site with static file serving 52 Setting up a Magento site with static file serving 56 Converting your Apache's.htaccess into Nginx rewrites 59 Using maps to make configurations cleaner 62 Chapter 3: Get It All Logged: The Logging Module 65 Introduction 65 Setting up error log path and levels 66 Logging it like Apache 67 Disabling logging of 404 in error logs 68 Using different logging profiles in the same setup 69 Enabling a log file cache 71 Utilizing separate error logs per virtual host 72 Setting up log rotation 74 Enabling remote logging with syslog-ng 75 Setting up your custom logs for easy parsing 76 Chapter 4: Slow Them Down: Access and Rate Limiting Module 83 Introduction 83 Limiting requests for any given session 84 Blocking and allowing access using IP 85 Setting up simple rate limiting for a download directory 86 Rate limiting search engine bots 87 Setting up GeolP using the MaxMind country database 88 Using the GeolP module to set up access and rate control 90 Chapter 5: Let's be Secure: Security Modules 93 Introduction 93 Setting up HTTP auth for access control 94 Generating a secure link for files 95 Setting up HTTPS for a simple site 97 Using non standard error codes for debugging SSL setup 100 Using wildcard certificates with multiple servers 101 Using Nginx SSL variables for rewrite rules 103 Chapter 6: Setting Up Applications: FCGI and WSGI Modules 105 Introduction 105 Setti ng u p a PH P FCGI site 106 Setting up a Python site using uwsgi 110 Modifying FCGI timeouts 113 Utilizing FCGI cache to speed it up 114 Using multiple FCGI backends 115 -m
Chapter 7: Nginx as a Reverse Proxy 119 Introduction 119 Using Nginx as a simple reverse proxy 120 Setting up a rails site using Nginx as a reverse proxy 121 Setting up correct reverse proxy timeouts 123 Setting up caching on the reverse proxy 124 Using multiple backends for the reverse proxy 126 Serving CGI files using thttpd and Nginx 128 Setting up load balancing with reverse proxy 130 Splitting requests based on various conditions using split-clients 132 Chapter 8: Improving Performance and SEP Using Nginx 135 Introduction 136 Setting up TCP options correctly for optimizing performance 136 Reducing the keep-alives to free up Nginx workers 137 Using Memcached as the cache backend 138 Configuring the right event model and file limits 140 Setting max-age expiry headers for client-side caching 141 Blocking scrapers, bots, and spiders to save bandwidth 143 Redirection of www to non-www domain for SEO 144 Removing all white space from response 145 Setting up server status for monitoring 146 Setting up Munin for 24x7 Nginx monitoring 147 Enabling GZIP pre-compression 150 Preventing hotlinking using Nginx 151 Using embedded Perl to minify JavaScript files 152 Using embedded Perl to minify CSS files 154 Using embedded Perl to serve sitemaps (SEO) 156 Setting up Boost module on Drupal with Nginx 158 Setting up streaming for Flash files 162 Utilizing the lxl GIF serving module to do offline processing 163 Chapter 9: Using Other Third-party Modules 165 Introduction 166 Setting up an IMAP/POP3 proxy server 166 Setting up authentication for mail services 167 Setting up SSL for mail authentication 170 Using Nginx as a WEBDAV reverse proxy 172 Dynamic image resizing using Nginx 173 Replacing and searching text in Nginx response 175 Assembling ZIP files on the fly 176 Using Nginx as a HTTP publish-subscribe server 178 - iii
Transforming XML responses using XSLT templates 181 Setting up Etags for static content 182 Easy logging of page load times 183 Streaming of MP4/H.264 files 185 Setting up SCGI sites 186 Setting up expiring links 188 Chapter 10: Some More Third-party Modules 191 Introduction 191 Configuring a fair load balancing 192 Setting up health checks for backend servers 194 Tracking and reporting file upload progress 196 Generating circles for round edges using Nginx 198 Running Python using Phusion Passenger 200 Generating graphs directly from RRDtool in Nginx 202 Using Google performance tools 204 Serving content directly from GridFS 205 Configuring Basic HTTP auth using PAM 207 Configuring Basic HTTP auth using Kerberos 209 Index 211