Nginx,	 MySQL	 Replication…	 
WordPress	 
	 サーバの運用 (構築編)
WordCamp	 Kansai	 2014
OKAMOTO
Wataru
@wokamoto
WordPress	 Server
*	 WordPress	 運用時の最小構成	 
*	 Web	 サーバを	 Apache	 から	 Nginx	 に

*	 CDN	 の導入	 
*	 Web	 サーバを冗長化
*	 DB	 サーバを冗長化
Menu	 for	 Today
Minimum	 
Configuration
WordPress	 運用時の最小構成
Minimum	 Configuration
*Apache	 (	 mod_rewrite	 )	 
!
*php	 	 	 5.2.4	 or	 higher	 
!
*MySQL	 5.0	 or	 higher
<ifModule	
  mod_expires.c>	
  
ExpiresActive	
  On	
  
ExpiresDefault	
  "access	
  plus	
  1	
  seconds"	
  
ExpiresByType	
  text/html	
  "access	
  plus	
  1	
  seconds"	
  
ExpiresByType	
  image/gif	
  "access	
  plus	
  30	
  days"	
  
ExpiresByType	
  image/jpeg	
  "access	
  plus	
  30	
  days"	
  
ExpiresByType	
  image/png	
  "access	
  plus	
  30	
  days"	
  
ExpiresByType	
  text/css	
  "access	
  plus	
  30	
  days"	
  
ExpiresByType	
  text/javascript	
  "access	
  plus	
  30	
  days"	
  
ExpiresByType	
  application/x-­‐javascript	
  "access	
  plus	
  30	
  days"	
  
ExpiresByType	
  application/x-­‐shockwave-­‐flash	
  "access	
  plus	
  30	
  days"	
  
</ifModule>
有効期限の設定
Minimum	 Configuration
<link	
  rel="stylesheet"	
  type="text/css"	
  media=“all"	
  
	
  href="<?php	
  echo	
  get_stylesheet_uri();	
  ?>	
  
?ver=<?php	
  echo	
  filemtime(get_stylesheet_directory()	
  .	
  '/style.css');	
  ?>"	
  />
キャッシュのコントロール
add_filter('stylesheet_uri',	
  function($stylesheet_uri){	
  
	
  	
  $stylesheet	
  =	
  get_stylesheet_directory()	
  .	
  '/style.css';	
  
	
  	
  $stylesheet_uri	
  .=	
  '?ver='	
  .	
  filemtime($stylesheet);	
  
	
  	
  return	
  $stylesheet_uri;	
  
});
または
Minimum	 Configuration
<IfModule	
  mod_deflate.c>	
  
SetOutputFilter	
  DEFLATE	
  
BrowserMatch	
  ^Mozilla/4	
  gzip-­‐only-­‐text/html	
  
BrowserMatch	
  ^Mozilla/4.0[678]	
  no-­‐gzip	
  
BrowserMatch	
  bMSI[E]	
  !no-­‐gzip	
  !gzip-­‐only-­‐text/html	
  
SetEnvIfNoCase	
  Request_URI	
  .(?:gif|jpe?g|png|ico)$	
  no-­‐gzip	
  dont-­‐vary	
  
SetEnvIfNoCase	
  Request_URI	
  _.utxt$	
  no-­‐gzip	
  
#DeflateCompressionLevel	
  4	
  
AddOutputFilterByType	
  DEFLATE	
  text/plain	
  
AddOutputFilterByType	
  DEFLATE	
  text/html	
  
AddOutputFilterByType	
  DEFLATE	
  text/xml	
  
AddOutputFilterByType	
  DEFLATE	
  text/css	
  
AddOutputFilterByType	
  DEFLATE	
  application/xml	
  
AddOutputFilterByType	
  DEFLATE	
  application/x-­‐javascript	
  
AddOutputFilterByType	
  DEFLATE	
  application/x-­‐httpd-­‐php	
  
</IfModule>
圧縮転送の設定
Minimum	 Configuration
#	
  BEGIN	
  WordPress	
  
<IfModule	
  mod_rewrite.c>	
  
RewriteEngine	
  On	
  
RewriteBase	
  /	
  
RewriteCond	
  %{REQUEST_FILENAME}	
  !-­‐f	
  
RewriteCond	
  %{REQUEST_FILENAME}	
  !-­‐d	
  
RewriteRule	
  .	
  /index.php	
  [L]	
  
</IfModule>	
  
	
  	
  
#	
  END	
  WordPress	
  
カスタムパーマリンクの設定
Minimum	 Configuration
#	
  BEGIN	
  WordPress	
  
<IfModule	
  mod_rewrite.c>	
  
RewriteEngine	
  On	
  
RewriteBase	
  /	
  
RewriteCond	
  %{REQUEST_FILENAME}	
  !.(js|css|jpe?g|png|gif|ico)$	
  
RewriteCond	
  %{REQUEST_FILENAME}	
  !-­‐f	
  
RewriteCond	
  %{REQUEST_FILENAME}	
  !-­‐d	
  
RewriteRule	
  .	
  /index.php	
  [L]	
  
</IfModule>	
  
	
  	
  
#	
  END	
  WordPress	
  
Minimum	 Configuration
カスタムパーマリンクの設定
.htaceess 使ったら、
負けだと思っている
.htaceess は、極力使わない
Minimum	 Configuration
/usr/share/mysql	
  
	
  my-­‐innodb-­‐heavy-­‐4G.cnf	
  	
  InnoDB	
  memory	
  4G	
  
	
  my-­‐huge.cnf	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  memory	
  1G	
  -­‐	
  2G	
  
	
  my-­‐large.cnf	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  memory	
  -­‐	
  512MB	
  
	
  my-­‐medium.cnf	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  memory	
  -­‐	
  128MB	
  
	
  my-­‐small.cnf	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  memory	
  -­‐	
  64MB	
  
MySQL	 のチューニング
Minimum	 Configuration
$	
  wget	
  -­‐O	
  -­‐	
  mysqltuner.pl	
  |	
  perl	
  


	
  >>	
  	
  MySQLTuner	
  1.3.0	
  -­‐	
  Major	
  Hayden	
  <major
	
  >>	
  	
  Bug	
  reports,	
  feature	
  requests,	
  and	
  dow
	
  >>	
  	
  Run	
  with	
  '-­‐-­‐help'	
  for	
  additional	
  optio
[!!]	
  Successfully	
  authenticated	
  with	
  no	
  pas
[OK]	
  Currently	
  running	
  supported	
  MySQL	
  vers
[OK]	
  Operating	
  on	
  64-­‐bit	
  architecture	
  
!
-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐	
  Storage	
  Engine	
  Statistics	
  
Minimum	 Configuration
MySQL	 のチューニング
-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐	
  Storage	
  Engine	
  Statistics	
  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐
[-­‐-­‐]	
  Status:	
  +ARCHIVE	
  +BLACKHOLE	
  +CSV	
  -­‐FEDERATED	
  +I
[-­‐-­‐]	
  Data	
  in	
  InnoDB	
  tables:	
  496K	
  (Tables:	
  12)	
  
[-­‐-­‐]	
  Data	
  in	
  PERFORMANCE_SCHEMA	
  tables:	
  0B	
  (Tables:
[OK]	
  Total	
  fragmented	
  tables:	
  0
Minimum	 Configuration
MySQL	 のチューニング
-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐	
  Security	
  Recommendations	
  	
  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐
[!!]	
  User	
  '@localhost'	
  has	
  no	
  password	
  set.	
  
[!!]	
  User	
  '@v210-­‐172-­‐187-­‐188.z0d1.static.cnode.jp'	
  
[!!]	
  User	
  'root@127.0.0.1'	
  has	
  no	
  password	
  set.	
  
[!!]	
  User	
  'root@::1'	
  has	
  no	
  password	
  set.	
  
[!!]	
  User	
  'root@localhost'	
  has	
  no	
  password	
  set.	
  
[!!]	
  User	
  'root@v210-­‐172-­‐187-­‐188.z0d1.static.cnode.
Minimum	 Configuration
MySQL	 のチューニング
-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐	
  Performance	
  Metrics	
  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐
[-­‐-­‐]	
  Up	
  for:	
  5h	
  5m	
  4s	
  (111	
  q	
  [0.006	
  qps],	
  8	
  conn,	
  T
[-­‐-­‐]	
  Reads	
  /	
  Writes:	
  96%	
  /	
  4%	
  
[-­‐-­‐]	
  Total	
  buffers:	
  232.0M	
  global	
  +	
  1.5M	
  per	
  thread
[OK]	
  Maximum	
  possible	
  memory	
  usage:	
  424.0M	
  (2%	
  of	
  i
[OK]	
  Slow	
  queries:	
  0%	
  (0/111)	
  
[OK]	
  Highest	
  usage	
  of	
  available	
  connections:	
  0%	
  (1/
[OK]	
  Key	
  buffer	
  size	
  /	
  total	
  MyISAM	
  indexes:	
  16.0M/
[OK]	
  Query	
  cache	
  efficiency:	
  41.3%	
  (38	
  cached	
  /	
  92	
  
[OK]	
  Query	
  cache	
  prunes	
  per	
  day:	
  0	
  
[OK]	
  Sorts	
  requiring	
  temporary	
  tables:	
  0%	
  (0	
  temp	
  s
Minimum	 Configuration
MySQL	 のチューニング
-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐	
  Recommendations	
  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐
General	
  recommendations:	
  
	
  	
  	
  	
  MySQL	
  started	
  within	
  last	
  24	
  hours	
  -­‐	
  recommenda
	
  	
  	
  	
  Enable	
  the	
  slow	
  query	
  log	
  to	
  troubleshoot	
  bad	
  q
	
  	
  	
  	
  When	
  making	
  adjustments,	
  make	
  tmp_table_size/ma
	
  	
  	
  	
  Reduce	
  your	
  SELECT	
  DISTINCT	
  queries	
  without	
  LIM
Variables	
  to	
  adjust:	
  
	
  	
  	
  	
  tmp_table_size	
  (>	
  64M)	
  
	
  	
  	
  	
  max_heap_table_size	
  (>	
  64M)	
  
!
Minimum	 Configuration
MySQL	 のチューニング
Nginx
Web	 サーバを	 Apache	 から	 Nginx	 に
Nginx	 ?
*	 OSS	 の軽量	 HTTP	 サーバ	 
!
!
*	 スレッドモデルでは無く、イベントループモデルを採用	 
!
!
*	 トラフィック量上位100万サイトでのシェア20%
Nginx	 ?
*	 OSS	 の軽量	 HTTP	 サーバ	 
!
!
*	 スレッドモデルでは無く、イベントループモデルを採用	 
!
!
*	 トラフィック量上位100万サイトでのシェア20%
Nginx	 ?
*	 OSS	 の軽量	 HTTP	 サーバ	 
!
!
*	 スレッドモデルでは無く、イベントループモデルを採用	 
!
!
*	 トラフィック量上位100万サイトでのシェア20%
Nginx	 ?
WordPres.com	 でも採用
$	
  wget	
  -­‐S	
  -­‐-­‐spider	
  http://wordpress.com	
  
wordpress.com|66.155.11.243|:80	
  に接続しています...	
  接続しました。	
  
HTTP	
  による接続要求を送信しました、応答を待っています...	
  	
  
	
  	
  HTTP/1.1	
  200	
  OK	
  
	
  	
  Server:	
  nginx	
  
	
  	
  Date:	
  Tue,	
  15	
  Apr	
  2014	
  05:58:07	
  GMT	
  
	
  	
  Content-­‐Type:	
  text/html;	
  charset=utf-­‐8	
  
	
  	
  Connection:	
  keep-­‐alive	
  
	
  	
  Vary:	
  Accept-­‐Encoding	
  
	
  	
  X-­‐Frame-­‐Options:	
  SAMEORIGIN	
  
	
  	
  Vary:	
  Cookie	
  
	
  	
  Set-­‐Cookie:	
  wordpress_homepage=existing;	
  expires=Wed,	
  30-­‐Apr-­‐2014	
  
05:58:07	
  GMT	
  
Nginx	 ?
PHP	 は	 fast-CGI	 で
Apache の mod_php のように Web サーバと同一プロセ
スで php を動作させることはできません。!
!
php-fpm などを使って別プロセスで動作させる必要があり
ます。!
Nginx	 ?
Nginx	 +	 php-fpm
nginx.conf	 の例
server	
  {	
  
	
  	
  listen	
  	
  	
  	
  	
  	
  80	
  default;	
  
	
  	
  server_name	
  _;	
  
	
  	
  root	
  	
  	
  	
  	
  	
  	
  	
  /path/to/app;	
  
	
  	
  index	
  	
  	
  	
  	
  	
  	
  index.php	
  index.html	
  index.htm;	
  
	
  	
  charset	
  	
  	
  	
  	
  utf-­‐8;	
  
!
	
  	
  location	
  ~	
  .php$	
  {	
  
	
  	
  	
  	
  fastcgi_pass	
  	
  	
  unix:/var/run/php-­‐fpm.sock;	
  
	
  	
  	
  	
  fastcgi_index	
  	
  index.php;	
  
	
  	
  	
  	
  fastcgi_param	
  SCRIPT_FILENAME	
  
Nginx	 +	 php-fpm
php-fpm.conf	 の例
[www]	
  
listen	
  =	
  /var/run/php-­‐fpm.sock	
  
listen.owner	
  =	
  nginx	
  
listen.group	
  =	
  nginx	
  
listen.mode	
  =	
  0666	
  
!
user	
  =	
  nginx	
  
group	
  =	
  nginx	
  
!
pm	
  =	
  dynamic	
  
pm.max_children	
  =	
  15	
  
pm.start_servers	
  =	
  5	
  
Nginx	 +	 php-fpm
Nginx	 
Performance
Nginx	 Reverse	 Proxy	 Cache	 の紹介
Nginx	 Performance
Apache,	 mod_php,	 WordPress
ab -n 100 -c 10 (WordPress トップページ)!
!
* Requests per second: 4.26 [#/sec]!
!
* Time per request: 2346.174 [ms]!
!
* Connection Times Total: 2309 [ms]
Nginx	 Performance
Apache,	 mod_php,	 WordPress
Nginx	 Performance
Nginx,	 php-fpm,	 WordPress
ab -n 100 -c 10 (WordPress トップページ)!
!
* Requests per second: 5.79 [#/sec]!
!
* Time per request: 1726.535 [ms]!
!
* Connection Times Total: 1617 [ms]
Nginx	 Performance
Nginx,	 php-fpm,	 WordPress
Nginx	 Performance
Reverse	 Proxy	 Cache	 
!
!
ab -n 100 -c 10 (WordPress トップページ)!
!
* Requests per second: 141.24 [#/sec]!
!
* Time per request: 708.007 [ms]!
!
* Connection Times Total: 636 [ms]
Nginx	 Performance
Nginx	 Reverse	 Proxy	 Cache,	 WordPress
Nginx	 
Reverse	 Proxy	 Cache
nginx.conf	 の例
http	
  {	
  
	
  	
  	
  	
  	
  	
  :	
  
	
  	
  proxy_cache_path	
  	
  /var/cache/nginx/proxy_cache	
  levels=1:2	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  keys_zone=czone:32m	
  max_size=256m	
  inactive=1440m;	
  
	
  	
  proxy_temp_path	
  	
  	
  /var/cache/nginx/proxy_temp;	
  
	
  	
  proxy_cache_key	
  	
  	
  "$scheme://$host$request_uri";	
  
	
  	
  proxy_set_header	
  	
  Host	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  $host;	
  
	
  	
  proxy_set_header	
  	
  Remote-­‐Addr	
  	
  	
  	
  	
  	
  	
  	
  $remote_addr;	
  
	
  	
  proxy_set_header	
  	
  X-­‐Forwarded-­‐Host	
  	
  	
  $host;	
  
	
  	
  proxy_set_header	
  	
  X-­‐Forwarded-­‐Server	
  $host;	
  
	
  	
  proxy_set_header	
  	
  X-­‐Forwarded-­‐For	
  	
  	
  	
  $proxy_add_x_forwarded_for;	
  
!
	
  	
  upstream	
  backend	
  {	
  
	
  	
  	
  	
  server	
  unix:/var/run/nginx-­‐backend.sock;	
  
	
  	
  }	
  
	
  	
  	
  	
  	
  	
  :	
  
}
Reverse	 Proxy	 Cache
http	
  {	
  
	
  	
  	
  	
  	
  	
  :	
  
	
  	
  proxy_cache_path	
  	
  /var/cache/nginx/proxy_cache	
  levels=1:2	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  keys_zone=czone:32m	
  max_size=256m	
  inactive=1440m;	
  
	
  	
  proxy_temp_path	
  	
  	
  /var/cache/nginx/proxy_temp;	
  
	
  	
  proxy_cache_key	
  	
  	
  "$scheme://$host$request_uri";	
  
	
  	
  proxy_set_header	
  	
  Host	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  $host;	
  
	
  	
  proxy_set_header	
  	
  Remote-­‐Addr	
  	
  	
  	
  	
  	
  	
  	
  $remote_addr;	
  
	
  	
  proxy_set_header	
  	
  X-­‐Forwarded-­‐Host	
  	
  	
  $host;	
  
	
  	
  proxy_set_header	
  	
  X-­‐Forwarded-­‐Server	
  $host;	
  
	
  	
  proxy_set_header	
  	
  X-­‐Forwarded-­‐For	
  	
  	
  	
  $proxy_add_x_forwarded_for;	
  
!
	
  	
  upstream	
  backend	
  {	
  
	
  	
  	
  	
  server	
  unix:/var/run/nginx-­‐backend.sock;	
  
	
  	
  }	
  
	
  	
  	
  	
  	
  	
  :	
  
}
Reverse	 Proxy	 Cache
nginx.conf	 の例
http	
  {	
  
	
  	
  	
  	
  	
  	
  :	
  
	
  	
  proxy_cache_path	
  	
  /var/cache/nginx/proxy_cache	
  levels=1:2	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  keys_zone=czone:32m	
  max_size=256m	
  inactive=1440m;	
  
	
  	
  proxy_temp_path	
  	
  	
  /var/cache/nginx/proxy_temp;	
  
	
  	
  proxy_cache_key	
  	
  	
  "$scheme://$host$request_uri";	
  
	
  	
  proxy_set_header	
  	
  Host	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  $host;	
  
	
  	
  proxy_set_header	
  	
  Remote-­‐Addr	
  	
  	
  	
  	
  	
  	
  	
  $remote_addr;	
  
	
  	
  proxy_set_header	
  	
  X-­‐Forwarded-­‐Host	
  	
  	
  $host;	
  
	
  	
  proxy_set_header	
  	
  X-­‐Forwarded-­‐Server	
  $host;	
  
	
  	
  proxy_set_header	
  	
  X-­‐Forwarded-­‐For	
  	
  	
  	
  $proxy_add_x_forwarded_for;	
  
!
	
  	
  upstream	
  backend	
  {	
  
	
  	
  	
  	
  server	
  unix:/var/run/nginx-­‐backend.sock;	
  
	
  	
  }	
  
	
  	
  	
  	
  	
  	
  :	
  
}
Reverse	 Proxy	 Cache
nginx.conf	 の例
http	
  {	
  
	
  	
  	
  	
  	
  	
  :	
  
	
  	
  proxy_cache_path	
  	
  /var/cache/nginx/proxy_cache	
  levels=1:2	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  keys_zone=czone:32m	
  max_size=256m	
  inactive=1440m;	
  
	
  	
  proxy_temp_path	
  	
  	
  /var/cache/nginx/proxy_temp;	
  
	
  	
  proxy_cache_key	
  	
  	
  "$scheme://$host$request_uri";	
  
	
  	
  proxy_set_header	
  	
  Host	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  $host;	
  
	
  	
  proxy_set_header	
  	
  Remote-­‐Addr	
  	
  	
  	
  	
  	
  	
  	
  $remote_addr;	
  
	
  	
  proxy_set_header	
  	
  X-­‐Forwarded-­‐Host	
  	
  	
  $host;	
  
	
  	
  proxy_set_header	
  	
  X-­‐Forwarded-­‐Server	
  $host;	
  
	
  	
  proxy_set_header	
  	
  X-­‐Forwarded-­‐For	
  	
  	
  	
  $proxy_add_x_forwarded_for;	
  
!
	
  	
  upstream	
  backend	
  {	
  
	
  	
  	
  	
  server	
  unix:/var/run/nginx-­‐backend.sock;	
  
	
  	
  }	
  
	
  	
  	
  	
  	
  	
  :	
  
}
Reverse	 Proxy	 Cache
nginx.conf	 の例
server	
  {	
  
	
  	
  listen	
  	
  	
  	
  	
  	
  80	
  default;	
  
	
  	
  server_name	
  _;	
  
	
  	
  root	
  	
  	
  	
  	
  	
  	
  	
  /path/to/wordpress;	
  
	
  	
  index	
  	
  	
  	
  	
  	
  	
  index.php	
  index.html	
  index.htm;	
  
!
	
  	
  location	
  ~*	
  .(js|css|jpe?g|gif|png|swf|wmv|flv|ico)$	
  {	
  
	
  	
  	
  	
  expires	
  30d;	
  
	
  	
  }	
  
!
	
  	
  location	
  /	
  {	
  
	
  	
  	
  	
  set	
  $do_not_cache	
  0;	
  
	
  	
  	
  	
  if	
  ($request_method	
  =	
  POST)	
  {	
  
	
  	
  	
  	
  	
  	
  set	
  $do_not_cache	
  1;	
  
	
  	
  	
  	
  }	
  
	
  	
  	
  	
  proxy_no_cache	
  	
  	
  	
  	
  $do_not_cache;	
  
	
  	
  	
  	
  proxy_cache_bypass	
  $do_not_cache;	
  
	
  	
  	
  	
  proxy_redirect	
  	
  	
  	
  	
  off;	
  
	
  	
  	
  	
  proxy_cache	
  	
  	
  	
  	
  	
  	
  	
  czone;	
  
	
  	
  	
  	
  proxy_cache_key	
  	
  	
  	
  "$scheme://$host$request_uri";	
  
	
  	
  	
  	
  proxy_cache_valid	
  	
  200	
  0m;	
  
	
  	
  	
  	
  proxy_pass	
  http://backend;	
  
	
  	
  }	
  
}
server	
  {	
  
	
  	
  listen	
  	
  	
  	
  	
  	
  80	
  default;	
  
	
  	
  server_name	
  _;	
  
	
  	
  root	
  	
  	
  	
  	
  	
  	
  	
  /path/to/wordpress;	
  
	
  	
  index	
  	
  	
  	
  	
  	
  	
  index.php	
  index.html	
  index.htm;	
  
!
	
  	
  location	
  ~*	
  .(js|css|jpe?g|gif|png|swf|wmv|flv|ico)$	
  {	
  
	
  	
  	
  	
  expires	
  30d;	
  
	
  	
  }	
  
!
	
  	
  location	
  /	
  {	
  
	
  	
  	
  	
  set	
  $do_not_cache	
  0;	
  
	
  	
  	
  	
  if	
  ($request_method	
  =	
  POST)	
  {	
  
	
  	
  	
  	
  	
  	
  set	
  $do_not_cache	
  1;	
  
	
  	
  	
  	
  }	
  
	
  	
  	
  	
  proxy_no_cache	
  	
  	
  	
  	
  $do_not_cache;	
  
	
  	
  	
  	
  proxy_cache_bypass	
  $do_not_cache;	
  
	
  	
  	
  	
  proxy_redirect	
  	
  	
  	
  	
  off;	
  
	
  	
  	
  	
  proxy_cache	
  	
  	
  	
  	
  	
  	
  	
  czone;	
  
	
  	
  	
  	
  proxy_cache_key	
  	
  	
  	
  "$scheme://$host$request_uri";	
  
	
  	
  	
  	
  proxy_cache_valid	
  	
  200	
  0m;	
  
	
  	
  	
  	
  proxy_pass	
  http://backend;	
  
	
  	
  }	
  
}
server	
  {	
  
	
  	
  listen	
  	
  	
  	
  	
  	
  80	
  default;	
  
	
  	
  server_name	
  _;	
  
	
  	
  root	
  	
  	
  	
  	
  	
  	
  	
  /path/to/wordpress;	
  
	
  	
  index	
  	
  	
  	
  	
  	
  	
  index.php	
  index.html	
  index.htm;	
  
!
	
  	
  location	
  ~*	
  .(js|css|jpe?g|gif|png|swf|wmv|flv|ico)$	
  {	
  
	
  	
  	
  	
  expires	
  30d;	
  
	
  	
  }	
  
!
	
  	
  location	
  /	
  {	
  
	
  	
  	
  	
  set	
  $do_not_cache	
  0;	
  
	
  	
  	
  	
  if	
  ($request_method	
  =	
  POST)	
  {	
  
	
  	
  	
  	
  	
  	
  set	
  $do_not_cache	
  1;	
  
	
  	
  	
  	
  }	
  
	
  	
  	
  	
  proxy_no_cache	
  	
  	
  	
  	
  $do_not_cache;	
  
	
  	
  	
  	
  proxy_cache_bypass	
  $do_not_cache;	
  
	
  	
  	
  	
  proxy_redirect	
  	
  	
  	
  	
  off;	
  
	
  	
  	
  	
  proxy_cache	
  	
  	
  	
  	
  	
  	
  	
  czone;	
  
	
  	
  	
  	
  proxy_cache_key	
  	
  	
  	
  "$scheme://$host$request_uri";	
  
	
  	
  	
  	
  proxy_cache_valid	
  	
  200	
  0m;	
  
	
  	
  	
  	
  proxy_pass	
  http://backend;	
  
	
  	
  }	
  
}
server	
  {	
  
	
  	
  listen	
  	
  	
  	
  	
  	
  unix:/var/run/nginx-­‐backend.sock;	
  
	
  	
  server_name	
  _;	
  
	
  	
  root	
  	
  	
  	
  	
  	
  	
  	
  /path/to/wordpress;	
  
	
  	
  index	
  	
  	
  	
  	
  	
  	
  index.php	
  index.html	
  index.htm;	
  
	
  	
  charset	
  	
  	
  	
  	
  utf-­‐8;	
  
!
	
  	
  gzip	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  off;	
  
	
  	
  gzip_vary	
  	
  	
  	
  	
  	
  	
  	
  	
  off;	
  
!
	
  	
  location	
  ~	
  .php$	
  {	
  
	
  	
  	
  	
  fastcgi_pass	
  	
  	
  unix:/var/run/php-­‐fpm.sock;	
  
	
  	
  	
  	
  fastcgi_index	
  	
  index.php;	
  
	
  	
  	
  	
  fastcgi_param	
  	
  SCRIPT_FILENAME	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  $document_root$fastcgi_script_name;	
  
	
  	
  	
  	
  include	
  	
  	
  	
  	
  	
  	
  	
  fastcgi_params;	
  
	
  	
  	
  	
  fastcgi_pass_header	
  "X-­‐Accel-­‐Redirect";	
  
	
  	
  	
  	
  fastcgi_pass_header	
  "X-­‐Accel-­‐Buffering";	
  
	
  	
  	
  	
  fastcgi_pass_header	
  "X-­‐Accel-­‐Charset";	
  
	
  	
  	
  	
  fastcgi_pass_header	
  "X-­‐Accel-­‐Expires";	
  
	
  	
  	
  	
  fastcgi_pass_header	
  "X-­‐Accel-­‐Limit-­‐Rate";	
  
	
  	
  }	
  
}
server	
  {	
  
	
  	
  listen	
  	
  	
  	
  	
  	
  unix:/var/run/nginx-­‐backend.sock;	
  
	
  	
  server_name	
  _;	
  
	
  	
  root	
  	
  	
  	
  	
  	
  	
  	
  /path/to/app;	
  
	
  	
  index	
  	
  	
  	
  	
  	
  	
  index.php	
  index.html	
  index.htm;	
  
	
  	
  charset	
  	
  	
  	
  	
  utf-­‐8;	
  
!
	
  	
  gzip	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  off;	
  
	
  	
  gzip_vary	
  	
  	
  	
  	
  	
  	
  	
  	
  off;	
  
!
	
  	
  location	
  ~	
  .php$	
  {	
  
	
  	
  	
  	
  fastcgi_pass	
  	
  	
  unix:/var/run/php-­‐fpm.sock;	
  
	
  	
  	
  	
  fastcgi_index	
  	
  index.php;	
  
	
  	
  	
  	
  fastcgi_param	
  	
  SCRIPT_FILENAME	
  
	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  $document_root$fastcgi_script_name;	
  
	
  	
  	
  	
  include	
  	
  	
  	
  	
  	
  	
  	
  fastcgi_params;	
  
	
  	
  	
  	
  fastcgi_pass_header	
  "X-­‐Accel-­‐Redirect";	
  
	
  	
  	
  	
  fastcgi_pass_header	
  "X-­‐Accel-­‐Buffering";	
  
	
  	
  	
  	
  fastcgi_pass_header	
  "X-­‐Accel-­‐Charset";	
  
	
  	
  	
  	
  fastcgi_pass_header	
  "X-­‐Accel-­‐Expires";	
  
	
  	
  	
  	
  fastcgi_pass_header	
  "X-­‐Accel-­‐Limit-­‐Rate";	
  
	
  	
  }	
  
}
cache	 時間を	 php	 で制御
<?php	
  
header('X-­‐Accel-­‐Expires:	
  '.	
  60	
  *	
  60	
  *	
  24);	
  
?>
Reverse	 Proxy	 Cache
Reverse	 Proxy	 Cache
WP	 plugin	 Nginx	 Cache	 Controller	 -	 

	 https://wordpress.org/plugins/nginx-champuru/
Reverse	 Proxy	 Cache
WP	 plugin	 Nginx	 Mobile	 Theme	 -	 

	 https://wordpress.org/plugins/nginx-mobile-theme/
Contents	 Delivery	 
Network
CDN	 の導入
CDN
CDN	 (	 Contents	 Delivery	 Network	 )	 ?
CDNAkamai	 -	 	 
	 http://www.akamai.co.jp/enja/
CDN
CloudFront	 -	 

	 	 http://aws.amazon.com/jp/cloudfront/
CDN
CloudFlare	 -	 

	 	 https://www.cloudflare.com/
CDN
Jetpack	 Photon	 -	 

	 http://jetpack.me/support/photon/
wp-content/uploads 以下だけを cdn
から配信したい
CDN
WordPress	 で利用するには?
CDNWP	 plugin	 絡新婦	 -	 

	 http://wordpress.org/plugins/nephila-clavata/
CDN
S3	 ->	 CloudFront
CDN
オレオレ	 CDN
Redundant	 
	 Web	 servers
Web	 サーバを冗長化
Redundant	 Web	 servers
NFS	 で	 WordPress	 を共有
Redundant	 Web	 servers
Master	 /	 Slave	 形式に
server	
  {	
  
	
  	
  listen	
  	
  	
  	
  	
  	
  80	
  default;	
  
	
  	
  server_name	
  _;	
  
	
  	
  root	
  	
  	
  	
  	
  	
  	
  	
  /path/to/wordpress;	
  
	
  	
  index	
  	
  	
  	
  	
  	
  	
  index.php	
  index.html	
  index.htm;	
  
!
	
  	
  set	
  $backend	
  'http://127.0.0.1:8080';	
  
	
  	
  if	
  ($request_method	
  =	
  POST)	
  {	
  
	
  	
  	
  	
  set	
  $backend	
  'http://{master	
  server	
  IP}:8080';	
  
	
  	
  }	
  
!
	
  	
  location	
  /	
  {	
  
	
  	
  	
  	
  proxy_pass	
  	
  	
  	
  	
  	
  	
  	
  	
  $backend;	
  
	
  	
  }	
  
}	
  
Redundant	 Web	 servers
POST	 の場合のバックエンド変更
Redundant	 Web	 servers
server	
  {	
  
	
  	
  listen	
  	
  	
  	
  	
  	
  80	
  default;	
  
	
  	
  server_name	
  _;	
  
	
  	
  root	
  	
  	
  	
  	
  	
  	
  	
  /path/to/wordpress;	
  
	
  	
  index	
  	
  	
  	
  	
  	
  	
  index.php	
  index.html	
  index.htm;	
  
!
	
  	
  set	
  $backend	
  'http://127.0.0.1:8080';	
  
	
  	
  if	
  ($request_method	
  =	
  POST)	
  {	
  
	
  	
  	
  	
  set	
  $backend	
  'http://{master	
  server	
  IP}:8080';	
  
	
  	
  }	
  
!
	
  	
  location	
  /	
  {	
  
	
  	
  	
  	
  proxy_pass	
  	
  	
  	
  	
  	
  	
  	
  	
  $backend;	
  
	
  	
  }	
  
}	
  
POST	 の場合のバックエンド変更
Redundant	 
	 DB	 servers
DB	 サーバを冗長化
Redundant	 DB	 servers
Master	 /	 Slave	 形式に
Redundant	 DB	 servers
WordPress	 plugin	 Hyper	 DB	 -	 

	 https://wordpress.org/plugins/hyperdb/
<?php	
  
//	
  Master	
  (	
  write	
  only	
  )	
  
$wpdb-­‐>add_database(array(	
  
	
  	
  	
  	
  	
  	
  	
  	
  'host'	
  	
  	
  	
  	
  =>	
  '192.168.0.1',	
  
	
  	
  	
  	
  	
  	
  	
  	
  'user'	
  	
  	
  	
  	
  =>	
  DB_USER,	
  
	
  	
  	
  	
  	
  	
  	
  	
  'password'	
  =>	
  DB_PASSWORD,	
  
	
  	
  	
  	
  	
  	
  	
  	
  'name'	
  	
  	
  	
  	
  =>	
  DB_NAME,	
  
	
  	
  	
  	
  	
  	
  	
  	
  'write'	
  	
  	
  	
  =>	
  1,	
  
	
  	
  	
  	
  	
  	
  	
  	
  'read'	
  	
  	
  	
  	
  =>	
  0,	
  
));	
  
	
  	
  
//	
  Slave	
  (	
  read	
  only	
  )	
  
$wpdb-­‐>add_database(array(	
  
HyperDB	 -	 db-config.php
Redundant	 DB	 servers
//	
  Slave	
  (	
  read	
  only	
  )	
  
$wpdb-­‐>add_database(array(	
  
	
  	
  	
  	
  	
  	
  	
  	
  'host'	
  	
  	
  	
  	
  =>	
  '192.168.0.2',	
  
	
  	
  	
  	
  	
  	
  	
  	
  'user'	
  	
  	
  	
  	
  =>	
  DB_USER,	
  
	
  	
  	
  	
  	
  	
  	
  	
  'password'	
  =>	
  DB_PASSWORD,	
  
	
  	
  	
  	
  	
  	
  	
  	
  'name'	
  	
  	
  	
  	
  =>	
  DB_NAME,	
  
	
  	
  	
  	
  	
  	
  	
  	
  'write'	
  	
  	
  	
  =>	
  0,	
  
	
  	
  	
  	
  	
  	
  	
  	
  'read'	
  	
  	
  	
  	
  =>	
  1,	
  
));
HyperDB	 -	 db-config.php
Redundant	 DB	 servers
Thanks	 !

Wckansai 2014

  • 1.
    Nginx, MySQL Replication… WordPress サーバの運用 (構築編) WordCamp Kansai 2014
  • 2.
  • 4.
  • 5.
    * WordPress 運用時の最小構成 * Web サーバを Apache から Nginx に
 * CDN の導入 * Web サーバを冗長化 * DB サーバを冗長化 Menu for Today
  • 6.
  • 7.
    Minimum Configuration *Apache ( mod_rewrite ) ! *php 5.2.4 or higher ! *MySQL 5.0 or higher
  • 8.
    <ifModule  mod_expires.c>   ExpiresActive  On   ExpiresDefault  "access  plus  1  seconds"   ExpiresByType  text/html  "access  plus  1  seconds"   ExpiresByType  image/gif  "access  plus  30  days"   ExpiresByType  image/jpeg  "access  plus  30  days"   ExpiresByType  image/png  "access  plus  30  days"   ExpiresByType  text/css  "access  plus  30  days"   ExpiresByType  text/javascript  "access  plus  30  days"   ExpiresByType  application/x-­‐javascript  "access  plus  30  days"   ExpiresByType  application/x-­‐shockwave-­‐flash  "access  plus  30  days"   </ifModule> 有効期限の設定 Minimum Configuration
  • 9.
    <link  rel="stylesheet"  type="text/css"  media=“all"    href="<?php  echo  get_stylesheet_uri();  ?>   ?ver=<?php  echo  filemtime(get_stylesheet_directory()  .  '/style.css');  ?>"  /> キャッシュのコントロール add_filter('stylesheet_uri',  function($stylesheet_uri){      $stylesheet  =  get_stylesheet_directory()  .  '/style.css';      $stylesheet_uri  .=  '?ver='  .  filemtime($stylesheet);      return  $stylesheet_uri;   }); または Minimum Configuration
  • 10.
    <IfModule  mod_deflate.c>   SetOutputFilter  DEFLATE   BrowserMatch  ^Mozilla/4  gzip-­‐only-­‐text/html   BrowserMatch  ^Mozilla/4.0[678]  no-­‐gzip   BrowserMatch  bMSI[E]  !no-­‐gzip  !gzip-­‐only-­‐text/html   SetEnvIfNoCase  Request_URI  .(?:gif|jpe?g|png|ico)$  no-­‐gzip  dont-­‐vary   SetEnvIfNoCase  Request_URI  _.utxt$  no-­‐gzip   #DeflateCompressionLevel  4   AddOutputFilterByType  DEFLATE  text/plain   AddOutputFilterByType  DEFLATE  text/html   AddOutputFilterByType  DEFLATE  text/xml   AddOutputFilterByType  DEFLATE  text/css   AddOutputFilterByType  DEFLATE  application/xml   AddOutputFilterByType  DEFLATE  application/x-­‐javascript   AddOutputFilterByType  DEFLATE  application/x-­‐httpd-­‐php   </IfModule> 圧縮転送の設定 Minimum Configuration
  • 11.
    #  BEGIN  WordPress   <IfModule  mod_rewrite.c>   RewriteEngine  On   RewriteBase  /   RewriteCond  %{REQUEST_FILENAME}  !-­‐f   RewriteCond  %{REQUEST_FILENAME}  !-­‐d   RewriteRule  .  /index.php  [L]   </IfModule>       #  END  WordPress   カスタムパーマリンクの設定 Minimum Configuration
  • 12.
    #  BEGIN  WordPress   <IfModule  mod_rewrite.c>   RewriteEngine  On   RewriteBase  /   RewriteCond  %{REQUEST_FILENAME}  !.(js|css|jpe?g|png|gif|ico)$   RewriteCond  %{REQUEST_FILENAME}  !-­‐f   RewriteCond  %{REQUEST_FILENAME}  !-­‐d   RewriteRule  .  /index.php  [L]   </IfModule>       #  END  WordPress   Minimum Configuration カスタムパーマリンクの設定
  • 13.
  • 14.
    /usr/share/mysql    my-­‐innodb-­‐heavy-­‐4G.cnf    InnoDB  memory  4G    my-­‐huge.cnf                          memory  1G  -­‐  2G    my-­‐large.cnf                        memory  -­‐  512MB    my-­‐medium.cnf                      memory  -­‐  128MB    my-­‐small.cnf                        memory  -­‐  64MB   MySQL のチューニング Minimum Configuration
  • 15.
    $  wget  -­‐O  -­‐  mysqltuner.pl  |  perl   
  >>    MySQLTuner  1.3.0  -­‐  Major  Hayden  <major  >>    Bug  reports,  feature  requests,  and  dow  >>    Run  with  '-­‐-­‐help'  for  additional  optio [!!]  Successfully  authenticated  with  no  pas [OK]  Currently  running  supported  MySQL  vers [OK]  Operating  on  64-­‐bit  architecture   ! -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  Storage  Engine  Statistics   Minimum Configuration MySQL のチューニング
  • 16.
    -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  Storage  Engine  Statistics  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐ [-­‐-­‐]  Status:  +ARCHIVE  +BLACKHOLE  +CSV  -­‐FEDERATED  +I [-­‐-­‐]  Data  in  InnoDB  tables:  496K  (Tables:  12)   [-­‐-­‐]  Data  in  PERFORMANCE_SCHEMA  tables:  0B  (Tables: [OK]  Total  fragmented  tables:  0 Minimum Configuration MySQL のチューニング
  • 17.
    -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  Security  Recommendations    -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐ [!!]  User  '@localhost'  has  no  password  set.   [!!]  User  '@v210-­‐172-­‐187-­‐188.z0d1.static.cnode.jp'   [!!]  User  '[email protected]'  has  no  password  set.   [!!]  User  'root@::1'  has  no  password  set.   [!!]  User  'root@localhost'  has  no  password  set.   [!!]  User  'root@v210-­‐172-­‐187-­‐188.z0d1.static.cnode. Minimum Configuration MySQL のチューニング
  • 18.
    -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  Performance  Metrics  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐ [-­‐-­‐]  Up  for:  5h  5m  4s  (111  q  [0.006  qps],  8  conn,  T [-­‐-­‐]  Reads  /  Writes:  96%  /  4%   [-­‐-­‐]  Total  buffers:  232.0M  global  +  1.5M  per  thread [OK]  Maximum  possible  memory  usage:  424.0M  (2%  of  i [OK]  Slow  queries:  0%  (0/111)   [OK]  Highest  usage  of  available  connections:  0%  (1/ [OK]  Key  buffer  size  /  total  MyISAM  indexes:  16.0M/ [OK]  Query  cache  efficiency:  41.3%  (38  cached  /  92   [OK]  Query  cache  prunes  per  day:  0   [OK]  Sorts  requiring  temporary  tables:  0%  (0  temp  s Minimum Configuration MySQL のチューニング
  • 19.
    -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐  Recommendations  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐ General  recommendations:          MySQL  started  within  last  24  hours  -­‐  recommenda        Enable  the  slow  query  log  to  troubleshoot  bad  q        When  making  adjustments,  make  tmp_table_size/ma        Reduce  your  SELECT  DISTINCT  queries  without  LIM Variables  to  adjust:          tmp_table_size  (>  64M)          max_heap_table_size  (>  64M)   ! Minimum Configuration MySQL のチューニング
  • 20.
  • 21.
    Nginx ? * OSS の軽量 HTTP サーバ ! ! * スレッドモデルでは無く、イベントループモデルを採用 ! ! * トラフィック量上位100万サイトでのシェア20%
  • 22.
    Nginx ? * OSS の軽量 HTTP サーバ ! ! * スレッドモデルでは無く、イベントループモデルを採用 ! ! * トラフィック量上位100万サイトでのシェア20%
  • 23.
    Nginx ? * OSS の軽量 HTTP サーバ ! ! * スレッドモデルでは無く、イベントループモデルを採用 ! ! * トラフィック量上位100万サイトでのシェア20%
  • 24.
  • 25.
    WordPres.com でも採用 $  wget  -­‐S  -­‐-­‐spider  http://wordpress.com   wordpress.com|66.155.11.243|:80  に接続しています...  接続しました。   HTTP  による接続要求を送信しました、応答を待っています...        HTTP/1.1  200  OK      Server:  nginx      Date:  Tue,  15  Apr  2014  05:58:07  GMT      Content-­‐Type:  text/html;  charset=utf-­‐8      Connection:  keep-­‐alive      Vary:  Accept-­‐Encoding      X-­‐Frame-­‐Options:  SAMEORIGIN      Vary:  Cookie      Set-­‐Cookie:  wordpress_homepage=existing;  expires=Wed,  30-­‐Apr-­‐2014   05:58:07  GMT   Nginx ?
  • 26.
    PHP は fast-CGI で Apache の mod_php のように Web サーバと同一プロセ スで php を動作させることはできません。! ! php-fpm などを使って別プロセスで動作させる必要があり ます。! Nginx ?
  • 27.
  • 28.
    nginx.conf の例 server  {      listen            80  default;      server_name  _;      root                /path/to/app;      index              index.php  index.html  index.htm;      charset          utf-­‐8;   !    location  ~  .php$  {          fastcgi_pass      unix:/var/run/php-­‐fpm.sock;          fastcgi_index    index.php;          fastcgi_param  SCRIPT_FILENAME   Nginx + php-fpm
  • 29.
    php-fpm.conf の例 [www]   listen  =  /var/run/php-­‐fpm.sock   listen.owner  =  nginx   listen.group  =  nginx   listen.mode  =  0666   ! user  =  nginx   group  =  nginx   ! pm  =  dynamic   pm.max_children  =  15   pm.start_servers  =  5   Nginx + php-fpm
  • 30.
    Nginx Performance Nginx Reverse Proxy Cache の紹介
  • 31.
  • 32.
    ab -n 100-c 10 (WordPress トップページ)! ! * Requests per second: 4.26 [#/sec]! ! * Time per request: 2346.174 [ms]! ! * Connection Times Total: 2309 [ms] Nginx Performance Apache, mod_php, WordPress
  • 33.
  • 34.
    ab -n 100-c 10 (WordPress トップページ)! ! * Requests per second: 5.79 [#/sec]! ! * Time per request: 1726.535 [ms]! ! * Connection Times Total: 1617 [ms] Nginx Performance Nginx, php-fpm, WordPress
  • 35.
  • 36.
    ab -n 100-c 10 (WordPress トップページ)! ! * Requests per second: 141.24 [#/sec]! ! * Time per request: 708.007 [ms]! ! * Connection Times Total: 636 [ms] Nginx Performance Nginx Reverse Proxy Cache, WordPress
  • 37.
  • 38.
    nginx.conf の例 http  {              :      proxy_cache_path    /var/cache/nginx/proxy_cache  levels=1:2                                          keys_zone=czone:32m  max_size=256m  inactive=1440m;      proxy_temp_path      /var/cache/nginx/proxy_temp;      proxy_cache_key      "$scheme://$host$request_uri";      proxy_set_header    Host                              $host;      proxy_set_header    Remote-­‐Addr                $remote_addr;      proxy_set_header    X-­‐Forwarded-­‐Host      $host;      proxy_set_header    X-­‐Forwarded-­‐Server  $host;      proxy_set_header    X-­‐Forwarded-­‐For        $proxy_add_x_forwarded_for;   !    upstream  backend  {          server  unix:/var/run/nginx-­‐backend.sock;      }              :   } Reverse Proxy Cache
  • 39.
    http  {              :      proxy_cache_path    /var/cache/nginx/proxy_cache  levels=1:2                                          keys_zone=czone:32m  max_size=256m  inactive=1440m;      proxy_temp_path      /var/cache/nginx/proxy_temp;      proxy_cache_key      "$scheme://$host$request_uri";      proxy_set_header    Host                              $host;      proxy_set_header    Remote-­‐Addr                $remote_addr;      proxy_set_header    X-­‐Forwarded-­‐Host      $host;      proxy_set_header    X-­‐Forwarded-­‐Server  $host;      proxy_set_header    X-­‐Forwarded-­‐For        $proxy_add_x_forwarded_for;   !    upstream  backend  {          server  unix:/var/run/nginx-­‐backend.sock;      }              :   } Reverse Proxy Cache nginx.conf の例
  • 40.
    http  {              :      proxy_cache_path    /var/cache/nginx/proxy_cache  levels=1:2                                          keys_zone=czone:32m  max_size=256m  inactive=1440m;      proxy_temp_path      /var/cache/nginx/proxy_temp;      proxy_cache_key      "$scheme://$host$request_uri";      proxy_set_header    Host                              $host;      proxy_set_header    Remote-­‐Addr                $remote_addr;      proxy_set_header    X-­‐Forwarded-­‐Host      $host;      proxy_set_header    X-­‐Forwarded-­‐Server  $host;      proxy_set_header    X-­‐Forwarded-­‐For        $proxy_add_x_forwarded_for;   !    upstream  backend  {          server  unix:/var/run/nginx-­‐backend.sock;      }              :   } Reverse Proxy Cache nginx.conf の例
  • 41.
    http  {              :      proxy_cache_path    /var/cache/nginx/proxy_cache  levels=1:2                                          keys_zone=czone:32m  max_size=256m  inactive=1440m;      proxy_temp_path      /var/cache/nginx/proxy_temp;      proxy_cache_key      "$scheme://$host$request_uri";      proxy_set_header    Host                              $host;      proxy_set_header    Remote-­‐Addr                $remote_addr;      proxy_set_header    X-­‐Forwarded-­‐Host      $host;      proxy_set_header    X-­‐Forwarded-­‐Server  $host;      proxy_set_header    X-­‐Forwarded-­‐For        $proxy_add_x_forwarded_for;   !    upstream  backend  {          server  unix:/var/run/nginx-­‐backend.sock;      }              :   } Reverse Proxy Cache nginx.conf の例
  • 42.
    server  {      listen            80  default;      server_name  _;      root                /path/to/wordpress;      index              index.php  index.html  index.htm;   !    location  ~*  .(js|css|jpe?g|gif|png|swf|wmv|flv|ico)$  {          expires  30d;      }   !    location  /  {          set  $do_not_cache  0;          if  ($request_method  =  POST)  {              set  $do_not_cache  1;          }          proxy_no_cache          $do_not_cache;          proxy_cache_bypass  $do_not_cache;          proxy_redirect          off;          proxy_cache                czone;          proxy_cache_key        "$scheme://$host$request_uri";          proxy_cache_valid    200  0m;          proxy_pass  http://backend;      }   }
  • 43.
    server  {      listen            80  default;      server_name  _;      root                /path/to/wordpress;      index              index.php  index.html  index.htm;   !    location  ~*  .(js|css|jpe?g|gif|png|swf|wmv|flv|ico)$  {          expires  30d;      }   !    location  /  {          set  $do_not_cache  0;          if  ($request_method  =  POST)  {              set  $do_not_cache  1;          }          proxy_no_cache          $do_not_cache;          proxy_cache_bypass  $do_not_cache;          proxy_redirect          off;          proxy_cache                czone;          proxy_cache_key        "$scheme://$host$request_uri";          proxy_cache_valid    200  0m;          proxy_pass  http://backend;      }   }
  • 44.
    server  {      listen            80  default;      server_name  _;      root                /path/to/wordpress;      index              index.php  index.html  index.htm;   !    location  ~*  .(js|css|jpe?g|gif|png|swf|wmv|flv|ico)$  {          expires  30d;      }   !    location  /  {          set  $do_not_cache  0;          if  ($request_method  =  POST)  {              set  $do_not_cache  1;          }          proxy_no_cache          $do_not_cache;          proxy_cache_bypass  $do_not_cache;          proxy_redirect          off;          proxy_cache                czone;          proxy_cache_key        "$scheme://$host$request_uri";          proxy_cache_valid    200  0m;          proxy_pass  http://backend;      }   }
  • 45.
    server  {      listen            unix:/var/run/nginx-­‐backend.sock;      server_name  _;      root                /path/to/wordpress;      index              index.php  index.html  index.htm;      charset          utf-­‐8;   !    gzip                            off;      gzip_vary                  off;   !    location  ~  .php$  {          fastcgi_pass      unix:/var/run/php-­‐fpm.sock;          fastcgi_index    index.php;          fastcgi_param    SCRIPT_FILENAME                                              $document_root$fastcgi_script_name;          include                fastcgi_params;          fastcgi_pass_header  "X-­‐Accel-­‐Redirect";          fastcgi_pass_header  "X-­‐Accel-­‐Buffering";          fastcgi_pass_header  "X-­‐Accel-­‐Charset";          fastcgi_pass_header  "X-­‐Accel-­‐Expires";          fastcgi_pass_header  "X-­‐Accel-­‐Limit-­‐Rate";      }   }
  • 46.
    server  {      listen            unix:/var/run/nginx-­‐backend.sock;      server_name  _;      root                /path/to/app;      index              index.php  index.html  index.htm;      charset          utf-­‐8;   !    gzip                            off;      gzip_vary                  off;   !    location  ~  .php$  {          fastcgi_pass      unix:/var/run/php-­‐fpm.sock;          fastcgi_index    index.php;          fastcgi_param    SCRIPT_FILENAME                                              $document_root$fastcgi_script_name;          include                fastcgi_params;          fastcgi_pass_header  "X-­‐Accel-­‐Redirect";          fastcgi_pass_header  "X-­‐Accel-­‐Buffering";          fastcgi_pass_header  "X-­‐Accel-­‐Charset";          fastcgi_pass_header  "X-­‐Accel-­‐Expires";          fastcgi_pass_header  "X-­‐Accel-­‐Limit-­‐Rate";      }   }
  • 47.
    cache 時間を php で制御 <?php   header('X-­‐Accel-­‐Expires:  '.  60  *  60  *  24);   ?> Reverse Proxy Cache
  • 48.
    Reverse Proxy Cache WP plugin Nginx Cache Controller - 
 https://wordpress.org/plugins/nginx-champuru/
  • 49.
    Reverse Proxy Cache WP plugin Nginx Mobile Theme - 
 https://wordpress.org/plugins/nginx-mobile-theme/
  • 50.
  • 51.
    CDN CDN ( Contents Delivery Network ) ?
  • 52.
    CDNAkamai - http://www.akamai.co.jp/enja/
  • 53.
    CDN CloudFront - 
 http://aws.amazon.com/jp/cloudfront/
  • 54.
    CDN CloudFlare - 
 https://www.cloudflare.com/
  • 55.
    CDN Jetpack Photon - 
 http://jetpack.me/support/photon/
  • 56.
  • 57.
    CDNWP plugin 絡新婦 - 
 http://wordpress.org/plugins/nephila-clavata/
  • 58.
  • 59.
  • 60.
    Redundant Web servers Web サーバを冗長化
  • 61.
    Redundant Web servers NFS で WordPress を共有
  • 62.
  • 63.
    server  {      listen            80  default;      server_name  _;      root                /path/to/wordpress;      index              index.php  index.html  index.htm;   !    set  $backend  'http://127.0.0.1:8080';      if  ($request_method  =  POST)  {          set  $backend  'http://{master  server  IP}:8080';      }   !    location  /  {          proxy_pass                  $backend;      }   }   Redundant Web servers POST の場合のバックエンド変更
  • 64.
    Redundant Web servers server  {      listen            80  default;      server_name  _;      root                /path/to/wordpress;      index              index.php  index.html  index.htm;   !    set  $backend  'http://127.0.0.1:8080';      if  ($request_method  =  POST)  {          set  $backend  'http://{master  server  IP}:8080';      }   !    location  /  {          proxy_pass                  $backend;      }   }   POST の場合のバックエンド変更
  • 65.
    Redundant DB servers DB サーバを冗長化
  • 66.
  • 67.
    Redundant DB servers WordPress plugin Hyper DB - 
 https://wordpress.org/plugins/hyperdb/
  • 68.
    <?php   //  Master  (  write  only  )   $wpdb-­‐>add_database(array(                  'host'          =>  '192.168.0.1',                  'user'          =>  DB_USER,                  'password'  =>  DB_PASSWORD,                  'name'          =>  DB_NAME,                  'write'        =>  1,                  'read'          =>  0,   ));       //  Slave  (  read  only  )   $wpdb-­‐>add_database(array(   HyperDB - db-config.php Redundant DB servers
  • 69.
    //  Slave  (  read  only  )   $wpdb-­‐>add_database(array(                  'host'          =>  '192.168.0.2',                  'user'          =>  DB_USER,                  'password'  =>  DB_PASSWORD,                  'name'          =>  DB_NAME,                  'write'        =>  0,                  'read'          =>  1,   )); HyperDB - db-config.php Redundant DB servers
  • 70.