From Event to Action: Accelerate Your Decision Making with Real-Time Automation
Nginx pres
1. JAMES FULLER
02/13/2013
Simply Scale
w/ Nginx, Memcached, PHP-FPM and APC
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
8. The classic setup - prefork
MPM 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)
10. 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
11. Many reasons to keep
Apache
● apache modules
● very mature software
● can tune for good results
● plays nice with nginx!
15. Nginx performance
advantages
● event based connection handling
● low/predictable memory consumption
● works well in low resource environments
(VPS)
● can handle tens of thousands of concurrent
requests*
16. Nginx as a frontend
● serve static files
● frontend for apache for php
● no apache, via PHP-FPM
● serve content directly from memcached
22. PHP-FPM are you
listening?
● 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
23. Talking to php-fpm
# pass the PHP scripts to FastCGI server
# listening on 127.0.0.1:9000
location ~ .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;
}
24. Nginx for load balancing
● round robin
● least connections
● ip segmenting
● sticky backends
25. upstream server
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com:8080;
server unix:/tmp/backend3;
}
server {
location / {
proxy_pass http://backend;
}
}
26. apache frontend
upstream 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;
}
27. Nginx Modules
Core Log
Access Map OPTIONAL
Addition Secure Link
Auth Basic Memcached
Auto Index Degradation SSL
Proxy
Browser Embedded Perl Stub Status
Referer
Charset FLV Substitution
Rewrite
Empty GIF GeoIP WebDAV
SCGI
FastCGI Google
Split Clients XSLT
Geo Perftools
Gzip SSI Gzip
Headers Upstream Precompression
Index User ID Image Filter
Limit Requests uWSGI MP4
Limit Zone X-Accel Random Index
Limit Conn
Real IP
31. Talking to Memcached with
PHP
● 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)
33. Simple Cache strategy
<?php
$key = 'mystash';
$data = $cache->get($key);
if ($data === false) {
$data = 'fill up the data';
$cache->set($key, $data, ..[OPTIONS]..);
}
34. 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
35. APC
● Opcode cache
● Stores ready-to-run machine code
● will eventually be replaced by zend optimizer
(php 5.5)
● has data caching api
38. Find out what is slow
● web server
● database
● bandwidth / infrastructure
● php code
39. Avoid the file system
● does not scale well
● expensive to make fast
● cloud hosting and multi-server
implementations more complicated
40. Database
● often the culprit
● choose the right technologies
● don't skimp on hardware
41. Other people's benchmarks
● Monitoring and testing is key
● avoid magical thinking:
○ "Magical thinking is thinking that one's 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
42. It's all about the end user
● avoid blocking javascript
● be aware of static assets
● use expires headers correctly
43. 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
45. 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