32
Scalable nginx configuration Igor Sysoev NGINX, Inc.

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

  • Upload
    ontico

  • View
    870

  • Download
    8

Embed Size (px)

DESCRIPTION

Доклад Игоря Сысоева на HighLoad++ 2014.

Citation preview

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