Slide 1

Slide 1 text

NGINX Installation and Tuning Introduced by Andrew Alexeev Presented by Owen Garrett Nginx, Inc.

Slide 2

Slide 2 text

About this webinar You’re  ready  to  make  your  applica2ons  more  responsive,  scalable,  fast  and   secure.  Then  it’s  2me  to  get  started  with  NGINX.  In  this  webinar,  you  will   learn  how  to  install  NGINX  from  a  package  or  from  source  onto  a  Linux   host.  We’ll  then  look  at  some  common  opera2ng  system  tunings  you  could   make  to  ensure  your  NGINX  install  is  ready  for  prime  2me.  

Slide 3

Slide 3 text

Agenda   •  Installing  NGINX   –  Installa2on  source,  NGINX  features   •  Tuning  NGINX   –  Opera2ng  System  tuning   –  NGINX  soHware  tuning   •  Benchmarking  NGINX   We’re  covering  a  lot  of  material.     Please  feel  free  to  take  screenshots   and  read  up  a9erwards.  

Slide 4

Slide 4 text

BEFORE  YOU  INSTALL  NGINX…  

Slide 5

Slide 5 text

What  can  NGINX  do  for  you?   Internet N Web  Server   Serve  content  from  disk   Applica2on  Gateway   FastCGI,  uWSGI,  Passenger…   Proxy   Caching,  Load  Balancing…   HTTP  traffic   þ Applica2on  Accelera2on   þ SSL  and  SPDY  termina2on   þ Performance  Monitoring   þ High  Availability   Advanced  Features:   þ Bandwidth  Management   þ Content-­‐based  Rou2ng   þ Request  Manipula2on   þ Response  Rewri2ng   þ Authen2ca2on   þ Video  Delivery   þ Mail  Proxy   þ GeoLoca2on  

Slide 6

Slide 6 text

Deployment  Plan   Determine  the  func2onality  you’ll  need   from  NGINX:     •  Authen2ca2on   •  Proxy  to  API  gateways   •  GZIP   •  GeoIP   •  etc.  etc.   Modules  list  at  nginx.org  

Slide 7

Slide 7 text

Three  ques2ons  before  installing  NGINX   1.  What  func

Slide 8

Slide 8 text

Recommended  Install   1.  Standard  modules  (nginx.org)  or  NGINX  Plus   2.  Mainline  (1.7)   3.  Install  from  nginx.org  or  nginx-­‐plus  repository   nginx.org  builds  do  not  include:     •  Modules  with  complex  3rd-­‐party  dependencies:   •  GeoIP,  Image_Filter,  Perl,  XSLT   •  Modules  that  are  part  of  NGINX  Plus   •  Third-­‐party  modules  e.g.  Lua,  Phusion  Passenger   h`p://nginx.com/products/technical-­‐specs/      

Slide 9

Slide 9 text

Difference  between  NGINX  and  NGINX  Plus   h`p://nginx.com/products/feature-­‐matrix/     NGINX   •  High-­‐performance,  open   source  web  server  and   accelera2ng  proxy.   •  Community  support  through   mailing  lists  on  nginx.org,   stackoverflow,  subject   experts  etc.   NGINX  Plus   •  Adds  Enterprise  Load   Balancing  and  Applica2on   Delivery  features.   •  Full  support  and  updates   from  NGINX  Inc.,  the  team   who  built  and  manage   NGINX.  

Slide 10

Slide 10 text

INSTALLING  NGINX  

Slide 11

Slide 11 text

Installa2on  process   $ wget http://nginx.org/keys/nginx_signing.key! $ sudo apt-key add nginx_signing.key! ! # cat > /etc/apt/sources.list.d/nginx.list! deb http://nginx.org/packages/mainline/ubuntu/ trusty nginx! deb-src http://nginx.org/packages/mainline/ubuntu/ trusty nginx! ! # apt-get update! # apt-cache policy nginx! nginx:! Installed: (none)! Candidate: 1.7.0-1~trusty! Version table:! 1.7.0-1~trusty 0! 500 http://nginx.org/packages/mainline/ubuntu/ trusty/nginx amd64 Packages! 1.4.6-1ubuntu3 0! 500 http://us.archive.ubuntu.com/ubuntu/ trusty/main amd64 Packages! h`p://nginx.org/en/linux_packages.html#mainline    

Slide 12

Slide 12 text

Verify  that  it  is  working   # /etc/init.d/nginx status! * nginx is running! ! # /usr/sbin/nginx –v! nginx version: nginx/1.7.0!

Slide 13

Slide 13 text

TUNING  NGINX   #1:  UNDERSTAND  WHAT’S  HAPPENING  

Slide 14

Slide 14 text

Common  tools   •  vmstat    

Slide 15

Slide 15 text

Common  tools   •  strace  

Slide 16

Slide 16 text

Other  tools   •  tcpdump  /   wireshark   •  Chrome     dev  tools   •  System  log    (dmesg –c)  

Slide 17

Slide 17 text

TUNING  NGINX:   #2:  TUNING  THE  OPERATING  SYSTEM  

Slide 18

Slide 18 text

Tuning  the  opera2ng  system   •  Basic  tunables:   –  Backlog  queue:  limits  number  of   pending  connec2ons   –  File  descriptors:  limit  number  of   ac2ve  connec2ons   –  Ephemeral  ports:  limit  number  of   upstream  connec2ons  

Slide 19

Slide 19 text

Configuring  Tunables  -­‐  HOWTO   •  /proc:   ! !! ! !# echo "1" > /proc/sys/net/ipv4/tcp_syncookies! ! •  sysctl.conf:   ! !! ! !# vi /etc/sysctl.conf! ! ! ! !# Prevent against the common 'syn flood attack'! ! ! !net.ipv4.tcp_syncookies = 1! ! !! ! !# sysctl –p!

Slide 20

Slide 20 text

The  Backlog  Queue   •  What  happens  when  a  connec2on  is  received?   –  èSYN  /  çSYNACK  [syn_backlog  queue]  or  syncookie   –  èACK  [listen  backlog  queue]  /  NGINX:accept()! –  net.ipv4.tcp_max_syn_backlog! –  net.ipv4.tcp_syncookies! –  net.core.somaxconn! •  NGINX: listen backlog=1024! –  net.core.netdev_max_backlog !

Slide 21

Slide 21 text

File  Descriptors   •  What  happens  when  a  connec2on  is  processed?   File  descriptors  are  the  key  resource  –  es2mate  2  per  connec2on.   –  fs.file_max! –  /etc/security/limits.conf! –  worker_rlimit_nofile 200000;!

Slide 22

Slide 22 text

Ephemeral  Ports   •  What  happens  when  NGINX  proxies  connec2ons?   Each  TCP  connec2on  requires  a  unique  4-­‐tuple:   [src_ip:src_port, dst_ip:dst_port] ! Ephemeral  port  range  and  life2me:   –  net.ipv4.ip_local_port_range! –  net.ipv4.tcp_fin_timeout!

Slide 23

Slide 23 text

Keep  checking  kernel  messages        # dmesg -c! ! ! !# tail -f /var/log/kern.log!  

Slide 24

Slide 24 text

TUNING  NGINX:   #3:  TUNING  THE  SOFTWARE  

Slide 25

Slide 25 text

Tuning  NGINX     #1:  You  don’t  need  to  “tune”  very  much     #2:  Don’t  tune  just  for  a  benchmark     #3:  Use  our  Prof  Services  team  to  help  

Slide 26

Slide 26 text

Common  tunings   ! worker_processes auto;  –  set  to  ‘auto’  or  higher! worker_connections  –  set  to  less  than  file  descriptor   count.   accept_mutex:  disable  for  busy  services  

Slide 27

Slide 27 text

The  proxy  should  use  keepalives   Close  TCP  Connec

Slide 28

Slide 28 text

BENCHMARKING  NGINX  

Slide 29

Slide 29 text

Why  benchmark  NGINX?   1.  To find how fast NGINX can go 2.  To tune NGINX for your workload 3.  To find where the bottlenecks are 4.  All of the above

Slide 30

Slide 30 text

IN  CONCLUSION…  

Slide 31

Slide 31 text

In  conclusion:   •  Install  from  the  nginx  repo   –  NGINX  or  NGINX  Plus   •  Basic  tuning  and  configura2on   –  dmesg  /  kern.log   •  Benchmark  /  stress  test   •  NGINX  Professional  Services  and  Training   h`p://nginx.com/    

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

h`ps://speakerdeck.com/dctrwatson/c1m-­‐and-­‐nginx     h`ps://www.youtube.com/watch?v=yL4Q7D4ynxU     h`ps://gist.github.com/dctrwatson/0b3b52050254e273ff11