More Related Content Similar to Nginx MySQL Replication WordPress Server Configuration Similar to Nginx MySQL Replication WordPress Server Configuration (20) More from Wataru OKAMOTO (11) Nginx MySQL Replication WordPress Server Configuration5. * WordPress 運用時の最小構成
* Web サーバを Apache から Nginx に
* CDN の導入
* Web サーバを冗長化
* DB サーバを冗長化
Menu for Today
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
カスタムパーマリンクの設定
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
'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 のチューニング
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 のチューニング
21. Nginx ?
* OSS の軽量 HTTP サーバ
!
!
* スレッドモデルでは無く、イベントループモデルを採用
!
!
* トラフィック量上位100万サイトでのシェア20%
22. Nginx ?
* OSS の軽量 HTTP サーバ
!
!
* スレッドモデルでは無く、イベントループモデルを採用
!
!
* トラフィック量上位100万サイトでのシェア20%
23. Nginx ?
* OSS の軽量 HTTP サーバ
!
!
* スレッドモデルでは無く、イベントループモデルを採用
!
!
* トラフィック量上位100万サイトでのシェア20%
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 ?
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
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
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
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
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
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 の場合のバックエンド変更
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