Scalable nginx configuration 
Igor Sysoev 
NGINX, Inc.
NGINX, Inc. 
• Summer 2011, Moscow office 
• Fall 2011, company and Series A round announced in 
San Francisco. First commercial customer: Netflix 
• Summer 2013, NGINX+ release 
• Spring 2014, first nginx summit held in San Francisco 
• Fall 2014, nginx.conf/2014 in San Francisco 
2
NGINX, Inc. 
• Head office: San Francisco 
16 people, sales, business development, marketing, 
pre-sales, product management 
! 
• Engineering office: Moscow 
17 people, software development, support and 
professional service (we are hiring!) 
3
Products 
• nginx f/oss 
• paid support and professional services available 
! 
• NGINX+ 
• closed source, binary only 
• advanced load balancing 
• monitoring and configuration 
• video streaming 
! 
See nginx.com for more 
4
Scalable nginx configuration 
! 
O(1) maintenance 
5
Apache: order matters 
GET /script/ 
! 
<Location /> 
DirectoryIndex index.html 
<Location> 
! 
<Location /script/> 
DirectoryIndex index.php 
<Location> 
! 
/script/index.php 
GET /script/ 
! 
<Location /script/> 
DirectoryIndex index.php 
<Location> 
! 
<Location /> 
DirectoryIndex index.html 
<Location> 
! 
/script/index.html 
6
Apache: add more fun 
• global server 
• <VirtualHost> 
• <Directory> and <DirectoryMatch> 
• <Files> and <FilesMatch> 
• <Location> and <LocationMatch> 
7
Apache: add more fun 
• global server 
• <VirtualHost> 
• <Directory> and <DirectoryMatch> 
• <Files> and <FilesMatch> 
• <Location> and <LocationMatch> 
• .htaccess files spread all over site structure 
8
Apache: add more fun 
• global server 
• <VirtualHost> 
• <Directory> and <DirectoryMatch> 
• <Files> and <FilesMatch> 
• <Location> and <LocationMatch> 
• .htaccess files spread all over site structure 
AccessFileName .hide-and-seek 
9
Apache: add more fun 
• global server 
• <VirtualHost> 
• <Directory> and <DirectoryMatch> 
• <Files> and <FilesMatch> 
• <Location> and <LocationMatch> 
• .htaccess files spread all over site structure 
AccessFileName .hide-and-seek 
• RewriteRules 
10
nginx: order does not matter 
GET /script/ 
! 
location / { 
index index.html; 
} 
! 
location /script/ { 
index index.php; 
} 
! 
/script/index.php 
GET /script/ 
! 
location /script/> 
index index.php; 
} 
! 
location / { 
index index.html; 
} 
! 
/script/index.php 
11
nginx: configuration overview 
• server 
• listen address:port 
• server_name 
• location 
• location /prefix 
• location ~regular expressions 
12
Life is more complex than theory 
location ~ .(js|css|gif|jpe?g|png)$ { 
… 
} 
! 
location ~ .php$ { 
fastcgi_pass … 
… 
} 
13
nginx: configuration overview 
• server 
• listen address:port 
• server_name 
• location 
• recursive prefix search 
• regular expressions 
14
Inclusive locations 
location /admin/ { 
location ~ .php$ { 
=> /admin/index.php 
} 
} 
! 
location ~ .php$ { 
! 
} 
15
Skip regular expression part 
location ^~ /images/ { 
… 
} 
16
Wrong way! 
location /img/ { 
rewrite ^/img/(.+)$ /index.php?img=$1; 
} 
! 
location ~ .php$ { 
fastcgi_pass backend; 
fastcgi_param SCRIPT_FILENAME /path/to$fastcgi_script_name; 
} 
17
Configuration inheritance 
http { 
sendfile on; 
server { 
root /site/root; 
location / { 
… 
} 
location /script/ { 
root /site/php; 
} 
18
Configuration inclusion 
http { 
… 
server { 
include static.conf; 
location / { 
… 
} 
location /script/ { 
… 
} 
} 
static.conf 
! 
location /i/ { 
… 
} 
location /js/ { 
… 
} 
location /css/ { 
… 
} 
19
Use copy-paste 
! 
Don’t Repeat Yourself (DRY) is a myth 
Use copy-paste! 
20
Similar locations 
location ~ ^/(images|css|js)/ { 
root /path/static; 
} 
21
Similar locations 
location /images/ { 
root /path/static; 
} 
! 
location /css/ { 
root /path/static; 
} 
! 
location /js/ { 
root /path/static; 
} 
22
Similar locations 
root /path/static; 
! 
location /images/ { } 
! 
location /css/ { } 
! 
location /js/ { } 
23
Inclusive locations 
location /admin/ { 
auth_basic admin; 
} 
! 
! 
! 
! 
location ~ .php$ { 
fastcgi_pass backend; 
} 
24
Inclusive locations 
location /admin/ { 
auth_basic admin; 
location ~ .php$ { 
fastcgi_pass backend; 
} 
} 
! 
location ~ .php$ { 
fastcgi_pass backend; 
} 
25
Wrong way! 
location /img/ { 
rewrite ^/img/(.+)$ /index.php?img=$1; 
} 
! 
location ~ .php$ { 
fastcgi_pass backend; 
fastcgi_param SCRIPT_FILENAME /path/to$fastcgi_script_name; 
} 
26
Regular expression isolation 
location /img/ { 
location ~ ^/img/(?<img>.+)$ { 
fastcgi_pass backend; 
fastcgi_param SCRIPT_FILENAME /path/to/index.php; 
fastcgi_param QUERY_STRING img=$img; 
} 
} 
27
Regular expression in map 
map $http_user_agent $mobile { 
“~(iPhone|Android|Opera Mini)” 1; 
default 0; 
} 
28
Rewrites? 
! 
NO! 
29
“if” is Evil 
if (true) { 
gzip off; 
} 
! 
if (true) { 
etag off; 
} 
30
Use “if” only to return HTTP code 
if ($bot) { 
return 200 “No bots here!”; 
} 
31
TL;DR 
• only prefix locations, avoid regular expression locations 
• isolate regular expressions 
• regular expression in maps 
• duplication is your friend 
• no rewrites 
• use ”if” only to return HTTP code 
32

Масштабируемая конфигурация Nginx, Игорь Сысоев (Nginx)

  • 1.
    Scalable nginx configuration Igor Sysoev NGINX, Inc.
  • 2.
    NGINX, Inc. •Summer 2011, Moscow office • Fall 2011, company and Series A round announced in San Francisco. First commercial customer: Netflix • Summer 2013, NGINX+ release • Spring 2014, first nginx summit held in San Francisco • Fall 2014, nginx.conf/2014 in San Francisco 2
  • 3.
    NGINX, Inc. •Head office: San Francisco 16 people, sales, business development, marketing, pre-sales, product management ! • Engineering office: Moscow 17 people, software development, support and professional service (we are hiring!) 3
  • 4.
    Products • nginxf/oss • paid support and professional services available ! • NGINX+ • closed source, binary only • advanced load balancing • monitoring and configuration • video streaming ! See nginx.com for more 4
  • 5.
    Scalable nginx configuration ! O(1) maintenance 5
  • 6.
    Apache: order matters GET /script/ ! <Location /> DirectoryIndex index.html <Location> ! <Location /script/> DirectoryIndex index.php <Location> ! /script/index.php GET /script/ ! <Location /script/> DirectoryIndex index.php <Location> ! <Location /> DirectoryIndex index.html <Location> ! /script/index.html 6
  • 7.
    Apache: add morefun • global server • <VirtualHost> • <Directory> and <DirectoryMatch> • <Files> and <FilesMatch> • <Location> and <LocationMatch> 7
  • 8.
    Apache: add morefun • global server • <VirtualHost> • <Directory> and <DirectoryMatch> • <Files> and <FilesMatch> • <Location> and <LocationMatch> • .htaccess files spread all over site structure 8
  • 9.
    Apache: add morefun • global server • <VirtualHost> • <Directory> and <DirectoryMatch> • <Files> and <FilesMatch> • <Location> and <LocationMatch> • .htaccess files spread all over site structure AccessFileName .hide-and-seek 9
  • 10.
    Apache: add morefun • global server • <VirtualHost> • <Directory> and <DirectoryMatch> • <Files> and <FilesMatch> • <Location> and <LocationMatch> • .htaccess files spread all over site structure AccessFileName .hide-and-seek • RewriteRules 10
  • 11.
    nginx: order doesnot matter GET /script/ ! location / { index index.html; } ! location /script/ { index index.php; } ! /script/index.php GET /script/ ! location /script/> index index.php; } ! location / { index index.html; } ! /script/index.php 11
  • 12.
    nginx: configuration overview • server • listen address:port • server_name • location • location /prefix • location ~regular expressions 12
  • 13.
    Life is morecomplex than theory location ~ .(js|css|gif|jpe?g|png)$ { … } ! location ~ .php$ { fastcgi_pass … … } 13
  • 14.
    nginx: configuration overview • server • listen address:port • server_name • location • recursive prefix search • regular expressions 14
  • 15.
    Inclusive locations location/admin/ { location ~ .php$ { => /admin/index.php } } ! location ~ .php$ { ! } 15
  • 16.
    Skip regular expressionpart location ^~ /images/ { … } 16
  • 17.
    Wrong way! location/img/ { rewrite ^/img/(.+)$ /index.php?img=$1; } ! location ~ .php$ { fastcgi_pass backend; fastcgi_param SCRIPT_FILENAME /path/to$fastcgi_script_name; } 17
  • 18.
    Configuration inheritance http{ sendfile on; server { root /site/root; location / { … } location /script/ { root /site/php; } 18
  • 19.
    Configuration inclusion http{ … server { include static.conf; location / { … } location /script/ { … } } static.conf ! location /i/ { … } location /js/ { … } location /css/ { … } 19
  • 20.
    Use copy-paste ! Don’t Repeat Yourself (DRY) is a myth Use copy-paste! 20
  • 21.
    Similar locations location~ ^/(images|css|js)/ { root /path/static; } 21
  • 22.
    Similar locations location/images/ { root /path/static; } ! location /css/ { root /path/static; } ! location /js/ { root /path/static; } 22
  • 23.
    Similar locations root/path/static; ! location /images/ { } ! location /css/ { } ! location /js/ { } 23
  • 24.
    Inclusive locations location/admin/ { auth_basic admin; } ! ! ! ! location ~ .php$ { fastcgi_pass backend; } 24
  • 25.
    Inclusive locations location/admin/ { auth_basic admin; location ~ .php$ { fastcgi_pass backend; } } ! location ~ .php$ { fastcgi_pass backend; } 25
  • 26.
    Wrong way! location/img/ { rewrite ^/img/(.+)$ /index.php?img=$1; } ! location ~ .php$ { fastcgi_pass backend; fastcgi_param SCRIPT_FILENAME /path/to$fastcgi_script_name; } 26
  • 27.
    Regular expression isolation location /img/ { location ~ ^/img/(?<img>.+)$ { fastcgi_pass backend; fastcgi_param SCRIPT_FILENAME /path/to/index.php; fastcgi_param QUERY_STRING img=$img; } } 27
  • 28.
    Regular expression inmap map $http_user_agent $mobile { “~(iPhone|Android|Opera Mini)” 1; default 0; } 28
  • 29.
  • 30.
    “if” is Evil if (true) { gzip off; } ! if (true) { etag off; } 30
  • 31.
    Use “if” onlyto return HTTP code if ($bot) { return 200 “No bots here!”; } 31
  • 32.
    TL;DR • onlyprefix locations, avoid regular expression locations • isolate regular expressions • regular expression in maps • duplication is your friend • no rewrites • use ”if” only to return HTTP code 32