Nginx HTTP Server Adopt Nginx for your web applications to make the most of your infrastructure and serve pages faster than ever element Nedelcu [ ] ~men~e~eri~c::i~li~dce PUBLISHING BIRMINGHAM - MUMBAI
Table of Contents Preface Chapter 1: Preparing your Work Environment Setting up a terminal emulator Finding and downloading PuTTY Creating a session Working with PuTTY and the shell Basic shell commands File and directory management User and group management Superuser account User accounts Group management Programs and processes Starting an application System services Process management Discovering the Linux filesystem Directory structure Special files and devices Device types Pseudo devices Mounting a storage device Files and inodes EXT3 filesystem specifications Filenames Inodes Atime. ctime, and mtime Symbolic and hard links File manipulation Reading a file 1 7 7 8 8 10 11 11 15 15 15 17 18 18 19 20 22 22 25 25 26 27 28 29 29 29 30 31 32 33
Table of Contents Editing a file Compression and archiving System administration tools Running a command as Superuser Su command Sudo command System verification and maintenance Disk Free Disk Usage Free memory Software packages Package managers Downloading and installing packages manually Building from source Files and permissions Understanding file permissions Directory permissions Octal representation Changing permissions Changing ownership and group Chapter 2: Downloading and Installing Nginx Setting up the prerequisites GCC - GNU Compiler Collection PCRE library zlib library OpenSSL Downloading Nginx Websites and resources Version branches Features Downloading and extracting Configure options The easy way Path options Prerequisites options Module options Modules enabled by default Modules disabled by default Miscellaneous options Configuration examples About the prefix switch Regular HTTP and HTTPS servers All modules enabled 34 35 37 37 37 38 39 39 39 40 40 40 41 42 43 43 43 44 44 45 46 47 47 48 49 50 50 51 51 52 53 54 55 55 56 58 59 59 60 61 62 63 63 64 [ ii ]
Table ofcontents Mail server proxy Build configuration issues Make sure you installed the prerequisites Directories exist and are writable Compiling and installing Controlling the Nginx service Daemons and services User and group Nginx command-line switches Starting and stopping the daemon Testing the configuration Other switches Adding Nginx as a system service System V scripts What is an init script? Creating an init script for Nginx Installing the script Debian-based distributions Red Hat-based distributions Chapter 3: Basic Nginx Configuration Configuration file syntax Configuration Directives Organization and inclusions Directive blocks Advanced language rules Directives accept specific synlaxes Diminutives in directive values Variables String values Base module directives What are base modules? Nginx process architecture Core module directives Events module Configuration module A configuration for your profile Understanding the default configuration Necessary adjustments Adapting to your hardware Testing your server Creating a test server 64 65 65 65 66 67 67 68 68 69 69 70 71 71 73 73 75 76 76 77 79 79 80 81 83 84 84 85 86 86 86 87 87 88 93 95 95 95 96 97 99 99 ~---~.~-------- [iii]--------------
Table ofcontents Performance tests Httperf Autobench OpenWebload Upgrading Nginx gracefully Chapter 4: HTTP Configuration HTTP Core module Structure blocks Module directives Socket and host configuration Paths and documents Client requests MIME Types Limits and restrictions File processing and caching Other directives Module variables Request headers Response headers Nginx generated The Location block Location modifier Search order and priority Gase 1: Gase 2: Gase 3: Chapter 5: Module Configuration Rewrite module Reminder on regular expressions Purpose PGRE syntax Quantifiers Gaptures Internal requests error_page Rewrite Infinite loops Server Side Includes (551) Conditional structure Directives Common rewrite rules 100 101 102 103 105 106 107 107 108 109 110 114 117 121 123 125 127 130 130 131 132 133 133 136 137 138 138 139 141 141 142 142 142 144 145 146 147 148 149 150 151 153 156 -------------- [iv] --------------
Table ofcontents Performing a search User profile page Multiple parameters Wikipedia-like News website article Discussion board 551 module Module directives and variables SSI Commands File includes Working with variables Conditional structure Configuration Additional modules Website access and logging Index Autoindex Random index Log Limits and restrietions Auth_basic module Access Limit zone Limit request Content and encoding Empty GIF FLV HTTP headers Addition Substitution Gzip filter Gzip static Charset filter Memcached Image filter XSLT About your visitors Browser Map Geo GeolP UserlD filter Referer RealiP SSL and security SSL Setting up an SSL certificate Secure link 156 156 156 157 157 157 157 158 160 160 162 163 163 164 164 164 165 166 166 168 168 168 169 169 170 170 171 171 172 172 173 175 175 176 178 179 179 179 180 180 181 181 182 183 183 183 185 186 [v] --------------
Table ofcontents Other miscellaneous modules Stub status Google-perftools WebDAV Third-party modules Chapter 6: PHP and Python with Nginx Introduction to FastCGI Understanding the mechanism Common Gateway Interface (CGI) Fast Common Gateway Interface (FastCGI) Main directives FastCGI caching Upstream blocks Module syntax Server directive PHP with Nginx Architecture PHP FPM Setting up PHP and PHP-FPM Downloading and extraeting Patching Requirements Building PHP Post-install configuration Running and controlling Nginx configuration Python and Nginx Django Setting up Python and Django Python Django Starting the FastCGI process manager Nginx configuration Chapter 7: Apache and Nginx Together Nginx as reverse proxy Understanding the issue The reverse proxy mechanism Advantages and disadvantages Nginx Proxy module Main directives 187 187 187 188 189 190 191 192 192 193 194 195 201 204 205 206 207 207 208 208 208 209 209 209 210 210 211 212 212 213 213 213 214 215 215 217 217 218 219 220 221 222 ------------- [vi] --------------
Table 0/ Contents Caching, buffering, and temporary files Limits, timeouts, and errors Other directives Variables Configuring Apache and Nginx Reconfiguring Apache Configuration overview Resetting the port number Accepting local requests only Configuring Nginx Enabling proxy options Separating content Advanced configuration Additional steps Forwarding the correct IP address SSL issues and solutions Server control panel issues Chapter 8: From Apache to Nginx Nginx versus Apache Features Core and functioning General functionality Flexibility and community Performance Usage Conclusion Porting your Apache configuration Directives Modules Virtual hosts and configuration sections Configuration sections Creating a virtual host htaccess files Reminder on Apache.htaccess files Nginx equivalence Rewrite rules General remarks On the location On the syntax RewriteRule 225 228 229 230 230 231 231 231 232 233 233 235 237 238 238 239 239 240 241 241 242 242 243 244 244 245 246 246 246 249 250 250 251 254 254 255 257 257 257 258 259 -------------- (vii]--------------
Table o[ Con tents WordPress MediaWiki vbulietin Appendix A: Directive Index Appendix B: Module Reference Access Addition* Auth_basic module Autoindex Browser Charset Core DAV* Empty GIF Events FastCGI FLV* Geo Geo IP* Google-perftools* Gzip Gzip Static* Headers HTTP Core Image Filter* Index Limit Requests Limit Zone Log Map Memcached Proxy Random index* ReallP* Referer Rewrite Secure Link* SSI SSL* 259 261 262 263 265 287 287 287 288 288 288 288 289 289 289 289 290 290 290 290 291 291 291 291 292 292 292 292 293 293 293 293 294 294 294 294 295 295 295 295 -------------[viii]-------------
Table ofcontents Stub status* Substitution* Upstream User 10 XSLT* Appendix C: Troubleshooting General tips on troubleshooting Checking access permissions Testing your configuration Have you reloaded the service? Checking logs Install issues 403 Forbidden custom error page Location block priorities If block issues Inefficient statements Unexpected behavior Index 296 296 296 296 297 299 299 299 300 300 300 301 301 302 303 303 304 305 -------------- [ix] --------------