Nginx pres

10,457 views

Published on

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
10,457
On SlideShare
0
From Embeds
0
Number of Embeds
5,067
Actions
Shares
0
Downloads
52
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Nginx pres

  1. 1. JAMES FULLER 02/13/2013Simply Scalew/ Nginx, Memcached, PHP-FPM and APC
  2. 2. In this talk...● my experiences working in a high-traffic environment● intro to nginx for apache users● running php with php-fpm● memcached & caching tips● identifying bottlenecks / open discussion
  3. 3. The Early YearsCheap Shared Hosting
  4. 4. Into the fire
  5. 5. A Simple ScalingArchitecture NGINX PHP-FPM MEMCACHED PHP-FPM PHP-FPM MYSQL MASTER PHP-FPM MYSQL SLAVE
  6. 6. ApacheAwesome when you need to assault your server
  7. 7. The classic setup - preforkMPM w/ mod_php● prefork is default mode prior to v2.4● forks (creates) new process per web resource requested● runs php via a module (mod_php)
  8. 8. Apache preforkperformance killers● Loads all modules for each forked process● Keepalive timeout● AllowOverride (.htaccess)
  9. 9. Apache 2.4● Event MPM is now the default MPM in Apache 2.4● Event MPM designed to solve the keepalive problem● Can talk to PHP-FPM via mod_proxy_fcgi● Needs thread safe php
  10. 10. Many reasons to keepApache● apache modules● very mature software● can tune for good results● plays nice with nginx!
  11. 11. NGINXRussian for Fast
  12. 12. What is NGINX?● HTTP Server● Reverse Proxy● IMAP/POP3 proxy server● Open Source
  13. 13. Who is using nginx?
  14. 14. Nginx performanceadvantages● event based connection handling● low/predictable memory consumption● works well in low resource environments (VPS)● can handle tens of thousands of concurrent requests*
  15. 15. Nginx as a frontend● serve static files● frontend for apache for php● no apache, via PHP-FPM● serve content directly from memcached
  16. 16. Nginx Configurationuser nginx;worker_processes 1;error_log /var/log/nginx/error.log warn;pid /var/run/nginx.pid;events { worker_connections 1024;}http { server { location / { #do something } }}
  17. 17. a series of blockshttp { access_log /var/log/nginx/access.log main; include /etc/nginx/conf.d/*.conf; gzip on; server { server_name myserver.com www.myserver.com listen 80; location / { } }}
  18. 18. server blocksserver { listen 80 default_server; server_name star.yourdomain.com *.yourdomain.com; root /PATH/TO/WEBROOT; error_page 404 errors/404.html; access_log logs/star.yourdomain.com.access.log; index index.php index.html index.htm;}
  19. 19. location blocks# static file 404s not logged + expires header set to maxagelocation ~* .(jpg|jpeg|gif|css|png|js|ico|html)$ { access_log off; expires max;}# deny htaccesslocation ~ /.ht { deny all;}
  20. 20. PHP-FPMits like mod_php without Apache
  21. 21. PHP-FPM are youlistening?● included with php as of 5.3.3● runs as a daemon ○ listens for requests via socket or port (FastCGI)● Decouple web server from executing php code
  22. 22. Talking to php-fpm# pass the PHP scripts to FastCGI server# listening on 127.0.0.1:9000location ~ .php$ { root html; fastcgi_pass 127.0.0.1:9000; #fastcgi_pass unix:/tmp/php.socket fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; include fastcgi_params;}
  23. 23. Nginx for load balancing● round robin● least connections● ip segmenting● sticky backends
  24. 24. upstream serverupstream backend { server backend1.example.com weight=5; server backend2.example.com:8080; server unix:/tmp/backend3;}server { location / { proxy_pass http://backend; }}
  25. 25. apache frontendupstream backend { server 192.168.1.2; server 192.168.1.3; server 192.168.1.4; #etc}location ~ .php$ { proxy_pass http://backend; proxy_set_header X-Real-IP $remote_addr;}
  26. 26. Nginx ModulesCore LogAccess Map OPTIONAL Addition Secure LinkAuth Basic MemcachedAuto Index Degradation SSL ProxyBrowser Embedded Perl Stub Status RefererCharset FLV Substitution RewriteEmpty GIF GeoIP WebDAV SCGIFastCGI Google Split Clients XSLTGeo PerftoolsGzip SSI GzipHeaders Upstream PrecompressionIndex User ID Image FilterLimit Requests uWSGI MP4Limit Zone X-Accel Random IndexLimit Conn Real IP
  27. 27. Caching in PHPCache is king
  28. 28. What is Memcached?● invented by Brad Fitzpatrick to solve livejournal performance problems● distributed key value store● super fast
  29. 29. Set up Memcachedinstances● small sites can use as little as 25MB● best results and reliability in a cluster
  30. 30. Talking to Memcached withPHP● naming disaster● php has two memcached extensions ○ memcache ○ memcached - better support for advanced features ○ wtf?● memcache(d) php extension only on nix platforms (no windows)
  31. 31. Connecting to Memcached<?php$cache = new Memcache;$cache ->addServer(192.168.1.2, 11211, ..[OPTIONS]..);$cache ->addServer(192.168.1.3, 11211, ..[OPTIONS]..);$cache ->addServer(192.168.1.4, 11211, ..[OPTIONS]..);$cache ->addServer(192.168.1.5, 11211, ..[OPTIONS]..);
  32. 32. Simple Cache strategy<?php$key = mystash;$data = $cache->get($key);if ($data === false) { $data = fill up the data; $cache->set($key, $data, ..[OPTIONS]..);}
  33. 33. Be smart about caching● cache expensive things (network, db)● stale content can be much worse than slow content● have a plan to expire cache entries
  34. 34. APC● Opcode cache● Stores ready-to-run machine code● will eventually be replaced by zend optimizer (php 5.5)● has data caching api
  35. 35. Bottlenecks & BenchmarkingTry to identify bottlenecks
  36. 36. Find out what is slow● web server● database● bandwidth / infrastructure● php code
  37. 37. Avoid the file system● does not scale well● expensive to make fast● cloud hosting and multi-server implementations more complicated
  38. 38. Database● often the culprit● choose the right technologies● dont skimp on hardware
  39. 39. Other peoples benchmarks● Monitoring and testing is key● avoid magical thinking: ○ "Magical thinking is thinking that ones thoughts by themselves can bring about effects in the world or that thinking something corresponds with doing it"● use tools like newrelic with actual users
  40. 40. Its all about the end user● avoid blocking javascript● be aware of static assets● use expires headers correctly
  41. 41. Seriously, try New Relic● FREE basic account, with PRO TRIAL● runs as a deamon + php extension● allows deep inspection of web transactions● need ability to install package on server
  42. 42. Homework
  43. 43. Dig one level deeper● We work in abstractions● Understand the systems that deliver your code to the browser● Identify the problems before implementing the solutions
  44. 44. Resources● nginx wiki - http://wiki.nginx.org/● php-fpm - http://php.net/manual/en/install.fpm.php● memcached - http://memcached.org/● memcache extensions ○ memcache - http://php.net/manual/en/book. memcache.php ○ memcache(d) - http://php.net/manual/en/book. memcached.php● apc - http://php.net/manual/en/book.apc.php● newrelic - http://newrelic.com/
  45. 45. Questions? James Fuller @j_blotus http://www.jblotus.com

×