Nginx
1 Web
Server
Implementation
Cookbook
Over
100
recipes
to master
using
the
Nginx
HTTP
serverand
reverse
proxy
Dipankar
Sarkar
[
11
open
so
i I community experienced
" *' '" I
community experience distilled
PUBLISHING
urce'^
Table
of
Contents
Preface
1Chapter
1:The Core
HTTP Module 7Introduction 8
Installing
new modules andcompiling Nginx
8Running Nginx
indebug
mode 10Easy reloading
ofNginx using
the CLI 11Splitting configuration
files for bettermanagement
13Setting
upmultiple
virtual hosts 16Setting
upa default catch-all virtual host 17Using
wildcards in virtual hosts 18Setting
up the number ofworker processescorrectly
19Increasing
the size ofuploaded
files 20Using
dynamic
SSI forsimple
sites 22Adding
content before and afteraparticular
page
23Enabling
autoindexing
of adirectory
25Serving
any random web page from adirectory
27Serving
cookies foridentifying
andlogging
users 29Re-encoding
the response to anotherencoding
30Enabling Gzip compression
on somecontenttypes
31Setting
up404
and other errorpages
32Chapter
2: AH About
Rewrites: TheRewrite Module
35Introduction 35
Setting
up asimple
redirect
36Using
variables in yourrewrite 37Using
cookies for your rewrites 38Using
browseragents
for your rewrites 40Using
rate limits as a condition for rewrites 41Blocking
requests
basedon HTTPreferrers
43Tableof Contents
Setting
upa WordPress sitewith static fileserving
46Setting
upaDrupal
site with static fileserving
52Setting
up aMagento
site with static fileserving
56Converting
yourApache's
.htaccess intoNginx
rewrites 59Using
mapstomakeconfigurations
cleaner 62Chapter
3: Get It AllLogged:
TheLogging
Module
65Introduction 65
Setting
uperrorlog path
and levels 66Logging
it likeApache
67Disabling
logging
of 404 in errorlogs
68Using
differentlogging profiles
in thesamesetup
69Enabling
alog
file cache 71Utilizing separate
errorlogs
pervirtual host 72Setting
uplog
rotation 74Enabling
remotelogging
withsyslog-ng
75Setting
up yourcustomlogs
for easyparsing
76Chapter
4: Slow Them Down: Access andRate
Limiting
Module 83Introduction 83
Limiting requests
foranygiven
session 84Blocking
andallowing
accessusing
IP 85Setting
upsimple
ratelimiting
for a downloaddirectory
86Rate
limiting
searchengine
bots 87Setting
upGeolPusing
theMaxMindcountry
database 88Using
the GeolP moduletoset up access and rate control 90Chapter
5: Let's be Secure:
Security
Modules 93Introduction 93
Setting
upHTTPauth foraccesscontrol 94Generating
a secure link for files 95Setting
upHTTPS for asimple
site 97Using
non standard error codes fordebugging
SSLsetup
100Using
wildcard certificates withmultiple
servers 101Using Nginx
SSL variablesfor rewrite rules 103Chapter
6:Setting Up Applications:
FCGI
and WSGI Modules105
Introduction 105 Setti ngupa PH PFCGI site 106
Setting
upaPython
siteusing
uWSGI 110Modifying
FCGI timeouts 113Utilizing
FCGIcache tospeed
it up 114Using multiple
FCGI backends 115Table of Contents
Chapter
7:Nginx
as a ReverseProxy
119Introduction 119
Using Nginx
as asimple
reverse proxy 120Setting
upa railssiteusing Nginx
as a reverse proxy 121Setting
upcorrectreverse proxy timeouts 123Setting
upcaching
on the reverseproxy 124Using
multiple
backends for the reverse proxy 126Serving
CGI filesusing thttpd
andNginx
128Setting
uploadbalancing
with reverse proxy 130Splitting
requests
based onvarious conditionsusing split-clients
132Chapter
8:Improving
Performance
and SEPUsing Nginx
135Introduction 136
Setting
upTCPoptions
correctly
foroptimizing
performance
136Reducing
thekeep-alives
to free upNginx
workers 137Using
Memcached asthe cache backend 138Configuring
theright
event model and file limits 140Setting
max-ageexpiry
headers for client-sidecaching
141Blocking
scrapers,bots,
andspiders
tosave bandwidth 143Redirection ofwwwto non-wwwdomain for SEO 144
Removing
all white space from response 145Setting
upserver status formonitoring
146Setting
up Muninfor 24x7Nginx
monitoring
147Enabling
GZIPpre-compression
150Preventing
hotlinking using Nginx
151Using
embedded Perl tominify JavaScript
files 152Using
embedded Perltominify
CSSfiles
154Using
embedded Perltoservesitemaps (SEO)
156Setting
upBoost moduleonDrupal
withNginx
158Setting
upstreaming
for Flash files 162Utilizing
the lxlGIFserving
module to doofflineprocessing
163Chapter
9:Using
Other
Third-party
Modules 165Introduction 166
Setting
upanIMAP/POP3
proxyserver 166Setting
up authentication for mail services 167Setting
upSSL for mail authentication 170Using Nginx
as a WEBDAV reverseproxy 172Dynamic image
resizing using Nginx
173Replacing
andsearching
text inNginx
response 175Assembling
ZIP files onthefly
176Using Nginx
as a HTTPpublish-subscribe
server 178Transforming
XML responsesusing
XSLTtemplates
181Setting
upEtags
forstaticcontent 182Easy
logging
of page load times 183Streaming
ofMP4/H.264
files 185Setting
up SCGI sites 186Setting
upexpiring
links 188Chapter
10: Some MoreThird-party
Modules
191
Introduction 191