Slide 1

Slide 1 text

Load Balancing and Scaling with NGINX Introduced by Andrew Alexeev Presented by Owen Garrett Nginx, Inc.

Slide 2

Slide 2 text

About this webinar When one server just isn’t enough, how can you scale out? In this webinar, you'll learn how to build out the capacity of your website. You'll see a variety of scalability approaches and some of the advanced capabilities of NGINX Plus.

Slide 3

Slide 3 text

INTRODUCING NGINX…

Slide 4

Slide 4 text

What is NGINX? Internet N Web Server Serve content from disk Application Server FastCGI, uWSGI, Passenger… Proxy Caching, Load Balancing… HTTP traffic þ Application Acceleration þ SSL and SPDY termination þ Performance Monitoring þ High Availability Advanced Features: þ Bandwidth Management þ Content-based Routing þ Request Manipulation þ Response Rewriting þ Authentication þ Video Delivery þ Mail Proxy þ GeoLocation

Slide 5

Slide 5 text

143,000,000 Websites NGINX Accelerates

Slide 6

Slide 6 text

22% Top 1 million websites 37% Top 1,000 websites

Slide 7

Slide 7 text

NGINX and NGINX Plus NGINX  F/OSS     nginx.org   3rd  party     modules   Large  community  of  >100  modules  

Slide 8

Slide 8 text

NGINX and NGINX Plus NGINX  F/OSS     nginx.org   3rd  party     modules   Large  community  of  >100  modules   NGINX  Plus     Advanced  load  balancing  features   Ease-­‐of-­‐management   Commercial  support  

Slide 9

Slide 9 text

WHY LOAD-BALANCE?

Slide 10

Slide 10 text

Load-balancing Web Servers Internet N þ   Improved  Applica@on  Availability   þ   Management   þ   Increased  Capacity   þ   Advanced  techniques  e.g.  A|B  tes@ng   Why?   þ   DNS  Round  Robin   þ   Hardware  L4  load  balancer   þ   SoMware  Reverse  Proxy  LB   þ   Cloud  solu@on   How?  

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Three Load Balancing case studies Basic  Load  Balancing  with  NGINX     When  you  need  more  control     Advanced  techniques   1 2 3

Slide 13

Slide 13 text

1. Basic Load Balancing •  Simple  scalability   – All  servers  have  same  applica@ons/services   – Load-­‐balancer  extracts  op@mal  performance  

Slide 14

Slide 14 text

Basic load balancing server {! listen 80;! ! location / {! proxy_pass http://backend;! }! }! ! upstream backend {! zone backend 64k;! ! server webserver1:80;! server webserver2:80; ! server webserver3:80;! server webserver4:80;! }!

Slide 15

Slide 15 text

Basic load balancing •  Use  logging  to  debug:    “$upstream_addr”   log_format combined2 '$remote_addr - $remote_user [$time_local] '! '"$request" $status $body_bytes_sent '! '"$upstream_addr"';!   192.168.56.1  -­‐  -­‐  [09/Mar/2014:23:08:56  +0000]  "GET  /  HTTP/1.1"  200  30  "127.0.1.1:80"   192.168.56.1  -­‐  -­‐  [09/Mar/2014:23:08:56  +0000]  "GET  /favicon.ico  HTTP/1.1"  200  30  "127.0.1.2:80"   192.168.56.1  -­‐  -­‐  [09/Mar/2014:23:08:57  +0000]  "GET  /  HTTP/1.1"  200  30  "127.0.1.3:80"   192.168.56.1  -­‐  -­‐  [09/Mar/2014:23:08:57  +0000]  "GET  /favicon.ico  HTTP/1.1"  200  30  "127.0.1.4:80"   192.168.56.1  -­‐  -­‐  [09/Mar/2014:23:08:57  +0000]  "GET  /  HTTP/1.1"  200  30  "127.0.1.1:80"   192.168.56.1  -­‐  -­‐  [09/Mar/2014:23:08:57  +0000]  "GET  /favicon.ico  HTTP/1.1"  200  30  "127.0.1.2:80"   192.168.56.1  -­‐  -­‐  [09/Mar/2014:23:08:58  +0000]  "GET  /  HTTP/1.1"  200  30  "127.0.1.3:80"   192.168.56.1  -­‐  -­‐  [09/Mar/2014:23:08:58  +0000]  "GET  /favicon.ico  HTTP/1.1"  200  30  "127.0.1.4:80"  

Slide 16

Slide 16 text

Basic Load Balancing •  Round-­‐robin  is  the  default   –  Suitable  for  consistent  pages   •  Least  Connec@ons   –  Suitable  for  varying  pages   •  IP  Hash   –  Fixed  mapping,  basic  session   persistence   upstream backend {! server webserver1:80;! server webserver2:80;! }! upstream backend {! least_conn;! server webserver1:80;! server webserver2:80;! }! upstream backend {! ip_hash;! server webserver1:80;! server webserver2:80;! }!

Slide 17

Slide 17 text

Managing the Upstream Group •  Direct  config  edi@ng:   – nginx  –s  reload   – upstream.conf  file:   •  On-­‐the-­‐fly  Reconfigura@on      [NGINX  Plus  only]   upstream backend {! server webserver1:80;! server webserver2:80;! server webserver3:80;! server webserver4:80;! }! $ curl 'http://localhost/upstream_conf?upstream=backend&id=3&down=1'!

Slide 18

Slide 18 text

2. When you need more control… •  In  many  scenarios,  you  want  more  control  over   where  traffic  is  routed  to:   –  Primary  and  secondary  servers  (aka  master/slave)   –  Transac@on  state  is  accumulated  on  one  server  

Slide 19

Slide 19 text

Internet ‘Master’ and ‘Slave’ servers •  Wordpress  admin  traffic  (e.g.  image  uploads)   N ‘Master’   ‘Slave’   Copy  image   uploads  from   master  to  slave  

Slide 20

Slide 20 text

‘Master’ and ‘Slave’ servers •  Wordpress  admin  traffic  (e.g.  image  uploads)   N server {! listen 80;! ! location ~ ^/(wp-admin|wp-login) {! proxy_pass http://wpadmin;! }! }! ! upstream wpadmin {! server server1:80;! server server2:80 backup; ! }! ‘Master’   ‘Slave’  

Slide 21

Slide 21 text

Session Persistence [NGINX Plus only] •  For  when  transac@on  state  is  accumulated  on  one  server   –  Shopping  carts   –  Advanced  interac@ons   –  Non-­‐RESTful  Applica@ons   •  NGINX  Plus  offers  two  methods:     –  s@cky  cookie   –  s@cky  route   “Session  persistence  also   helps  performance”  

Slide 22

Slide 22 text

Advanced Techniques •  You  can  control  load-­‐balancing  programma@cally   –  A|B  Tes@ng   –  Migra@on  between  applica@ons  

Slide 23

Slide 23 text

A|B Testing Internet N ‘backends’  upstream  group   Test   server   95%   5%   Par@@on  traffic.       Send  5%  to  new  applica@on  instance  

Slide 24

Slide 24 text

A|B Testing split_clients "${remote_addr}AAA" $servers {! 95% backends;! 5% 192.168.56.1:80;! }! ! server {! listen 80;! ! location / {! proxy_pass http://$servers;! ! }! }!

Slide 25

Slide 25 text

Application Migration Internet N ‘backendsA’  upstream  group   ‘backendsB’  upstream  group   Create  new  genera@on  of  applica@on   Migrate  users  from  old  to  new   Preserve  sessions,  no  interrup@ons  

Slide 26

Slide 26 text

Application Migration map $cookie_group $group {! ~(?P.+)$ $value;! default backendB; # The default upstream group! }! ! server {! listen 80;! ! location / {! add_header Set-Cookie "group=$group; path=/"! proxy_pass http://$group;! }! }!

Slide 27

Slide 27 text

Three Load Balancing case studies Basic  Load  Balancing  with  NGINX     When  you  need  more  control     Advanced  techniques   1 2 3

Slide 28

Slide 28 text

Closing thoughts •  37%  of  the  busiest  websites  use  NGINX   •  Check  out  the  load-­‐balancing  ar@cles  on  nginx.com/ blog   •  Future  webinars:  nginx.com/webinars   Try  NGINX  F/OSS  (nginx.org)  or  NGINX  Plus  (nginx.com)