nginx pres
TRANSCRIPT
![Page 1: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/1.jpg)
Simply Scalew/ Nginx, Memcached, PHP-FPM and APC
JAMES FULLER 02/13/2013
![Page 2: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/2.jpg)
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
![Page 3: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/3.jpg)
The Early Years
Cheap Shared Hosting
![Page 4: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/4.jpg)
Into the fire
![Page 5: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/5.jpg)
A Simple Scaling Architecture
NGINX PHP-FPM
PHP-FPM
PHP-FPM
PHP-FPM
MEMCACHED
MYSQL MASTER
MYSQL SLAVE
![Page 6: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/6.jpg)
![Page 7: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/7.jpg)
ApacheAwesome when you need to assault your server
![Page 8: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/8.jpg)
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)
![Page 9: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/9.jpg)
Apache prefork performance killers● Loads all modules for each forked process
● Keepalive timeout
● AllowOverride (.htaccess)
![Page 10: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/10.jpg)
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
![Page 11: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/11.jpg)
Many reasons to keep Apache● apache modules
● very mature software
● can tune for good results
● plays nice with nginx!
![Page 12: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/12.jpg)
NGINXRussian for Fast
![Page 13: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/13.jpg)
What is NGINX?● HTTP Server
● Reverse Proxy
● IMAP/POP3 proxy server
● Open Source
![Page 14: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/14.jpg)
Who is using nginx?
![Page 15: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/15.jpg)
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*
![Page 16: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/16.jpg)
Nginx as a frontend
● serve static files
● frontend for apache for php
● no apache, via PHP-FPM
● serve content directly from memcached
![Page 17: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/17.jpg)
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
}
}
}
![Page 18: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/18.jpg)
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 / {
}
}
}
![Page 19: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/19.jpg)
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;
}
![Page 20: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/20.jpg)
location blocks# static file 404's not logged + expires header set to maxage
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
access_log off;
expires max;
}
# deny htaccess
location ~ /\.ht {
deny all;
}
![Page 21: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/21.jpg)
PHP-FPMit's like mod_php without Apache
![Page 22: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/22.jpg)
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
![Page 23: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/23.jpg)
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;
}
![Page 24: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/24.jpg)
Nginx for load balancing● round robin
● least connections
● ip segmenting
● sticky backends
![Page 25: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/25.jpg)
upstream serverupstream backend {
server backend1.example.com weight=5;
server backend2.example.com:8080;
server unix:/tmp/backend3;
}
server {
location / {
proxy_pass http://backend;
}
}
![Page 26: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/26.jpg)
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;
}
![Page 27: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/27.jpg)
Nginx ModulesCore
Access
Auth Basic
Auto Index
Browser
Charset
Empty GIF
FastCGI
Geo
Gzip
Headers
Index
Limit Requests
Limit Zone
Limit Conn
Log
Map
Memcached
Proxy
Referer
Rewrite
SCGI
Split Clients
SSI
Upstream
User ID
uWSGI
X-Accel
Addition
Degradation
Embedded Perl
FLV
GeoIP
Google Perftools
Gzip Precompression
Image Filter
MP4
Random Index
Real IP
Secure Link
SSL
Stub Status
Substitution
WebDAV
XSLT
OPTIONAL
![Page 28: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/28.jpg)
Caching in PHPCache is king
![Page 29: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/29.jpg)
What is Memcached?
● invented by Brad Fitzpatrick to solve livejournal performance problems
● distributed key value store
● super fast
![Page 30: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/30.jpg)
Set up Memcached instances
● small sites can use as little as 25MB
● best results and reliability in a cluster
![Page 31: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/31.jpg)
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)
![Page 32: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/32.jpg)
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]..);
![Page 33: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/33.jpg)
Simple Cache strategy<?php$key = 'mystash';$data = $cache->get($key);
if ($data === false) {
$data = 'fill up the data'; $cache->set($key, $data, ..[OPTIONS]..);}
![Page 34: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/34.jpg)
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
![Page 35: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/35.jpg)
APC● Opcode cache
● Stores ready-to-run machine code
● will eventually be replaced by zend optimizer (php 5.5)
● has data caching api
![Page 36: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/36.jpg)
![Page 37: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/37.jpg)
Bottlenecks & Benchmarking
Try to identify bottlenecks
![Page 38: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/38.jpg)
Find out what is slow● web server
● database
● bandwidth / infrastructure
● php code
![Page 39: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/39.jpg)
Avoid the file system● does not scale well
● expensive to make fast
● cloud hosting and multi-server implementations more complicated
![Page 40: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/40.jpg)
Database
● often the culprit
● choose the right technologies
● don't skimp on hardware
![Page 41: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/41.jpg)
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
![Page 42: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/42.jpg)
It's all about the end user
● avoid blocking javascript
● be aware of static assets
● use expires headers correctly
![Page 43: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/43.jpg)
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
![Page 44: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/44.jpg)
Homework
![Page 45: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/45.jpg)
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
![Page 46: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/46.jpg)
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/
![Page 47: Nginx pres](https://reader034.vdocuments.site/reader034/viewer/2022042507/554bb74ab4c9053a298b49f4/html5/thumbnails/47.jpg)
Questions?
James Fuller
@j_blotus
http://www.jblotus.com