5 things you didn’t know
NGINX could do
Sarah Novotny
Nginx, Inc.
Slide 2
Slide 2 text
Many people know NGINX as an HTTP request and load
balancing server that powers many of the world's
busiest websites. But, there are a lot of ancillary pieces
that go into the software to make it a whole web
application accelerator.
Slide 3
Slide 3 text
What is NGINX?
Internet
N
Web Server
Serve content from disk
Application Server
FastCGI, uWSGI, Passenger…
Proxy
Caching, Load Balancing… HTTP traffic
Slide 4
Slide 4 text
146,000,000
Websites
NGINX Accelerates
Slide 5
Slide 5 text
Advanced Features
þ Bandwidth Management
þ Content-based Routing
þ Request Manipulation
þ Response Rewriting
þ Application Acceleration
þ SSL and SPDY termination
þ Authentication
þ Video Delivery
þ Mail Proxy
þ GeoLocation
þ Performance Monitoring
þ High Availability
Slide 6
Slide 6 text
23%
Top 1 million websites
39%
Top 10,000 websites
Slide 7
Slide 7 text
Some
things
you
might
not
know
Form
spamming
Compress
assets
Thread
exhaus8on
Rewrite
content
Online
upgrades
Configure
flags
A/B
tes8ng
Include
direc8ve
Manipulate
proxy
headers
Slide 8
Slide 8 text
Some
things
you
might
not
know
Compress assets for delivery
Stop form spamming
Protect Apache from thread exhaustion attacks
Rewrite content inline
Online upgrades
Configure flags
Slide 9
Slide 9 text
Compress data to reduce
bandwidth
• Reduce
bandwidth
requirements
per
client
– Content
Compression
reduces
text
and
HTML
– Image
resampling
reduces
image
sizes
Slide 10
Slide 10 text
HTTP gzip module
• Provides Gzip capabilities so that responses
from NGINX are compressed to reduce file size
• Directives can be used in the http, server and
location contexts
• Key directives
– gzip
– gzip_types
– gzip_proxied
Slide 11
Slide 11 text
Gzip example
Enable gzip
gzip on;
Apply gzip for text, html and
CSS
gzip_types text/plain text/html text/css;
Enable gzip compression for
any proxied request
gzip_proxy any;
It is not
advisable to
enable gzip
for binary
content types
such as
images, word
documents or
videos
Slide 12
Slide 12 text
HTTP image filter
• Provides inline image manipulation to
transform images for optimal delivery
• Directives can be used in the location
context
• Key directives
– image_filter size;
– image_filter resize width height;
– image_filter crop width height;
We talk about the ‘N second rule’:
– 10 seconds
(Jakob Nielsen, March 1997)
– 8 seconds
(Zona Research, June 2001)
– 4 seconds
(Jupiter Research, June 2006)
– 3 seconds
(PhocusWright, March 2010)
Slide 15
Slide 15 text
Stop brute force retries
• Stop brute force password attacks
• Stop form spamming
– Use the NGINX limit request module
Slide 16
Slide 16 text
HTTP limit req module
• Allows granular control of request
processing rate
• Directives an be used in http, server and
location contexts
• Key directives
– limit_req_zone
– limit_req
Protect Apache from thread
exhaustion attacks
• Use NGINX in front of Apache
• Mitigates ‘slow loris’, ‘keep dead’ and
‘front page of hacker news’ attacks
Slide 19
Slide 19 text
What is thread exhaustion?
hJp
process
hJp
process
hJp
process
hJp
process
hJp
process
hJp
process
hJp
process
Client-side:
Multiple
Connections
HTTP Keepalives
Server-side:
Limited
concurrency
Slide 20
Slide 20 text
How
NGINX
mi8gates
thread
exhaus8on
N
Large
numbers
of
clients,
with
long-‐term
keepalive
connec8ons
NGINX
reduces
connec8ons
to
the
minimum
number
necessary
Slide 21
Slide 21 text
Rewrite content inline
• Use the power of substitution to simplify updates
• Directives can be used in the http, server and
location contexts
• Key directives
– sub_filter_once
– sub_filter
– sub_filter_types
Slide 22
Slide 22 text
HTTP sub filter example
location / {
sub_filter_once off;
sub_filter_types text/html;
sub_filter “__copyright_date__” “2014”;
}
Slide 23
Slide 23 text
Online Binary updates and
configuration changes
• Update either the configuration files or
the binary without losing any
connections
Binary Upgrade
[root@localhost ~]# kill –WINCH 1991
[root@localhost ~]# kill –QUIT 1991
• Verify things are working as expected
(you can still back out gracefully at this point)
Slide 29
Slide 29 text
nginx –V gives a nearly
complete configuration
script for compiling
Configure Flags
Measurement
and
analysis
is
leQ
as
an
exercise
to
the
reader
Slide 34
Slide 34 text
Include
Direc8ve
• Includes files
• Directives can be used in the any
context
• Key directives
– include
Slide 35
Slide 35 text
HTTP include example
http {!
! !include /etc/nginx/conf.d/mime.types;!
! !include /etc/nginx/conf.d/*.conf;!
! !include /etc/nginx/sites-enabled/*;!
}
Slide 36
Slide 36 text
Manipulate proxy headers
• Mask
content
source
(like
assets
in
S3)
• Manage
proxy
behavior
• Inject
your
own
headers
(host
header
or
x-‐
forward-‐for
etc)
Slide 37
Slide 37 text
Proxy
Header
Manipula8on
• Allows perception management of
content delivery through headers
• Directives can be used in the http, server
and location contexts
• Key directives
– proxy_hide_header
– proxy_set_header
– proxy_ignore_header