Helgi Þormar Þorbjörnsson
DPC, Amsterdam, 8th of June, 2012
Cranking
Nginx
to 11
Slide 2
Slide 2 text
Co-founded Orchestra.io
Work at EngineYard
PEAR Developer
From Iceland
@h on Twitter
Helgi
Slide 3
Slide 3 text
Nginx
Just a web server?
Slide 4
Slide 4 text
✓ Web Server
✓ Proxy
✓ Cache
✓ Mail Proxy
✓ And more!
No! It’s so much more!
Slide 5
Slide 5 text
The journey to 11
Slide 6
Slide 6 text
Important for tweaking
Slide 7
Slide 7 text
Always run configtest before
doing anything!
Slide 8
Slide 8 text
Reload (HUP Signal)
Slide 9
Slide 9 text
Reload (HUP Signal)
‣ Reloads config
‣ Starts up new workers
‣ Old workers stop listening
‣ Finish up any work they have
Slide 10
Slide 10 text
Upgrade (USR2 Signal)
Slide 11
Slide 11 text
Upgrade (USR2 Signal)
‣ Live upgrade of Nginx executable
‣ Starts up a new Master
‣ Run in parallel
‣ Old Workers gracefully shutdown
‣ Old Master can be brought back
Slide 12
Slide 12 text
Location Blocks
Slide 13
Slide 13 text
Foundation of most things we will do
Slide 14
Slide 14 text
Most Common Block
location / {
try_files $uri
$uri/
/index.php$is_args$args;
}
Slide 15
Slide 15 text
location /helgi/is/awesome {
return 202;
}
Slide 16
Slide 16 text
Accepts Regex
Done by adding ~ in front of the regular expression
Slide 17
Slide 17 text
Accepts Regex
# deny access to all .dot-files
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
# deny access to all backups
location ~ ~$ {
access_log off;
log_not_found off;
deny all;
}
Slide 18
Slide 18 text
Error Pages
Slide 19
Slide 19 text
Basic Custom 404
error_page 404 /errors/404.html;
Requires /errors/404.html to be
in the document root
Responsible for all if statements, file
exists checks, returns and more.
Can work with most Nginx variables
such as http_cookie, user agent, uri
and countless others.
Slide 33
Slide 33 text
last
break
redirect
permanent
Finish rewrite and evaluates
all rewrites again
Finish rewrite does no further
rewrite processing
Returns a 302 on the rewrite
Returns a 301 on the rewrite
Slide 34
Slide 34 text
server {
server_name www.helgi.ws;
rewrite ^/(.*)$ helgi.ws/$1 permanent;
}
server {
server_name www.helgi.ws;
rewrite ^ helgi.ws$request_uri permanent;
}
Forward Domains
How to send www.helgi.ws to helgi.ws
Slide 35
Slide 35 text
server {
server_name www.helgi.ws;
rewrite ^/(.*)$ helgi.ws/$1 permanent;
}
server {
server_name www.helgi.ws;
rewrite ^ helgi.ws$request_uri permanent;
}
Forward Domains
How to send www.helgi.ws to helgi.ws
Slide 36
Slide 36 text
Forward Domains
server {
server_name www.helgi.ws;
return 301 $scheme://helgi.ws$request_uri;
}
The Correct Way™
# set input headers
location /foo {
more_set_input_headers 'Host: ShoelessJoes';
more_set_input_headers -t 'text/plain' 'X-Tek: bah';
}
# replace input header X-Tek *only* if it already exists
more_set_input_headers -r 'X-Tek: howdy';
Slide 59
Slide 59 text
Load Balancing
Slide 60
Slide 60 text
upstream web_workers {
server www1.example.com;
server www2.example.com;
server www3.example.com;
server www4.example.com;
}
Simple Round Robin
Slide 61
Slide 61 text
upstream web_workers {
ip_hash;
server www1.example.com;
server www2.example.com;
server www3.example.com;
server www4.example.com;
}
Consistent IP Routing
Slide 62
Slide 62 text
upstream web_workers {
server www1.example.com;
server www2.example.com weight=2 max_fails=2 fail_timeout=15;
server www3.example.com weight=4 max_fails=3;
server www4.example.com weight=4 max_fails=4 fail_timeout=20;
keepalive 8;
}
Different Weights
weight and ip_hash can work
together in Nginx 1.3.1+