Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

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

2,229 views

Published on

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

Published in: Internet
  • Be the first to comment

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

  1. 1. Scalable nginx configuration Igor Sysoev NGINX, Inc.
  2. 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. 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. 4. 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
  5. 5. Scalable nginx configuration ! O(1) maintenance 5
  6. 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. 7. Apache: add more fun • global server • <VirtualHost> • <Directory> and <DirectoryMatch> • <Files> and <FilesMatch> • <Location> and <LocationMatch> 7
  8. 8. Apache: add more fun • global server • <VirtualHost> • <Directory> and <DirectoryMatch> • <Files> and <FilesMatch> • <Location> and <LocationMatch> • .htaccess files spread all over site structure 8
  9. 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 9
  10. 10. 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
  11. 11. 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
  12. 12. nginx: configuration overview • server • listen address:port • server_name • location • location /prefix • location ~regular expressions 12
  13. 13. Life is more complex than theory location ~ .(js|css|gif|jpe?g|png)$ { … } ! location ~ .php$ { fastcgi_pass … … } 13
  14. 14. nginx: configuration overview • server • listen address:port • server_name • location • recursive prefix search • regular expressions 14
  15. 15. Inclusive locations location /admin/ { location ~ .php$ { => /admin/index.php } } ! location ~ .php$ { ! } 15
  16. 16. Skip regular expression part location ^~ /images/ { … } 16
  17. 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. 18. Configuration inheritance http { sendfile on; server { root /site/root; location / { … } location /script/ { root /site/php; } 18
  19. 19. Configuration inclusion http { … server { include static.conf; location / { … } location /script/ { … } } static.conf ! location /i/ { … } location /js/ { … } location /css/ { … } 19
  20. 20. Use copy-paste ! Don’t Repeat Yourself (DRY) is a myth Use copy-paste! 20
  21. 21. Similar locations location ~ ^/(images|css|js)/ { root /path/static; } 21
  22. 22. Similar locations location /images/ { root /path/static; } ! location /css/ { root /path/static; } ! location /js/ { root /path/static; } 22
  23. 23. Similar locations root /path/static; ! location /images/ { } ! location /css/ { } ! location /js/ { } 23
  24. 24. Inclusive locations location /admin/ { auth_basic admin; } ! ! ! ! location ~ .php$ { fastcgi_pass backend; } 24
  25. 25. Inclusive locations location /admin/ { auth_basic admin; location ~ .php$ { fastcgi_pass backend; } } ! location ~ .php$ { fastcgi_pass backend; } 25
  26. 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. 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. 28. Regular expression in map map $http_user_agent $mobile { “~(iPhone|Android|Opera Mini)” 1; default 0; } 28
  29. 29. Rewrites? ! NO! 29
  30. 30. “if” is Evil if (true) { gzip off; } ! if (true) { etag off; } 30
  31. 31. Use “if” only to return HTTP code if ($bot) { return 200 “No bots here!”; } 31
  32. 32. 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

×