ããã«ã¡ã¯ãCSãã¼ã ã®åæ¬ã§ãã
ä»åã¯Nginxããã¼ããã©ã³ãµã¼ã¨ãã¦å©ç¨ãã¦è² è·åæ£ãè¡ããã¨ã§Wordpressãé«éåãããã¨æãã¾ãã
ãã¾ã¾ã§ã®è¨äº
Nginx + WordPress
Nginx + WordPress ãGzip Precompressionãã¢ã¸ã¥ã¼ã«ç¯
Nginx + WordPress proxy cacheç¯
â»EC2ã®ç°å¢ãNginx以å¤ã®MySQLãPHPãPHP-FPMã®è¨å®ãªã©ã¯ãã¾ã¾ã§ã¨åæ§ã§ãã
ç®æ¬¡
- ä»åã®æ§æ
- ãã¼ããã©ã³ãµã¼ã®è¨å®
- è¨å®ãã¡ã¤ã«ï¼ãã¼ããã©ã³ãµã¼ï¼default.confï¼
- è¨å®ãã¡ã¤ã«ï¼Webãµã¼ãã¼ï¼default.confï¼
- æ¯è¼
1. ä»åã®æ§æ
ä»åã®æ§æã¯ä»¥ä¸ã®å³ã®ãããªã¤ã¡ã¼ã¸ã§ãã
ããã³ãã¨ã³ãã«Nginxããã¼ããã©ã³ãµã¼ã¨ãã¦ç½®ãã¦ãããã¯ã¨ã³ãã«3å°ã®Webãµã¼ãã¼ãç½®ãã¦ãã¾ãã
2. ãã¼ããã©ã³ãµã¼ã®è¨å®
Nginxããã¼ããã©ã³ãµã¼ã¨ãã¦ä½¿ãå ´åãç¹ã«ã¢ã¸ã¥ã¼ã«ã追å ããå¿ è¦ã¯ããã¾ããã
è¨å®ãã·ã³ãã«ã§ãããããããã¨ã¦ãç°¡åã§ãã
ããã³ãã¨ã³ãã®Nginxã®upstreamãããã¯ã®è¨å®ã§ãããã¯ã¨ã³ãã®Webãµã¼ãã¼ã以ä¸ã®ããã«æå®ãã¾ãã
ãã®è¨å®ã®ã¿ã§ãã¼ããã©ã³ãµã¼ã®ä¸ã«ç½®ãWebãµã¼ãã¼ã®æ°ãç°¡åã«å¢æ¸ã§ãã¾ãã
â»æ¯ãåãæ¹æ³ã¯ã©ã¦ã³ãããã³ã§ãã
upstream backend { server 54.XXX.XXX.41; server 54.XXX.XXX.42; server 54.XXX.XXX.43; }
ããã¯ã¨ã³ãã®ãµã¼ãã¼ã«ã¹ããã¯ã®éããããå ´åãªã©ã¯ã以ä¸ã®ããã«weightãè¨å®ãããã°ãæ¯ãåãããµã¼ãã¼ã®éã¿ä»ãï¼ãªã¯ã¨ã¹ããæ¯ãåããå²åï¼ã®è¨å®ãã§ãã¾ãã
ä¾ãã°ã以ä¸ã®è¨å®ããããªã£ãå ´åãã54.XXX.XXX.41ãã«ã¯12ãªã¯ã¨ã¹ããã54.XXX.XXX.42ãã«ã¯13ãªã¯ã¨ã¹ããweight=2ãè¨å®ããã54.XXX.XXX.43ãã«ã¯25ãªã¯ã¨ã¹ããæ¯ãåããããã¤ã¡ã¼ã¸ã§ãã
upstream backend { server 54.XXX.XXX.41; server 54.XXX.XXX.42; server 54.XXX.XXX.43 weight=2; }
åãIPã¢ãã¬ã¹ããã®ã¢ã¯ã»ã¹ãåããµã¼ãã¼ã«æ¯ãåãããå ´åã¯ãip_hashã追å ãã¾ãã
upstream backend { ip_hash; server 54.XXX.XXX.41; server 54.XXX.XXX.42; server 54.XXX.XXX.43; }
â»ãã¡ãã®è¨å®ã¯éã¿ä»ãã®è¨å®ã¨çµã¿åããããã¨ã¯ã§ãã¾ããã
3. è¨å®ãã¡ã¤ã«ï¼ãã¼ããã©ã³ãµã¼ï¼default.confï¼
ããã³ãã¨ã³ãã®ãã¼ããã©ã³ãµã¼ã®Nginxã®è¨å®ã§ãã
/etc/nginx/conf.d/default.conf
server { listen 80; server_name _; access_log /var/log/nginx/access_80.log main; location = /favicon.ico { log_not_found off; } location /wordpress/wp-admin { proxy_pass http://backend; } location /wordpress/wp-login.php { proxy_pass http://backend; } location = /wordpress { rewrite ^(.+)$ /wordpress/index.php; } location = /wordpress/wp-admin { rewrite ^(.+)$ /wordpress/wp-admin/index.php; } proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Host $host; proxy_set_header X-Forwarded-Server $host; proxy_set_header X-Real-IP $remote_addr; location / { proxy_pass http://backend; } }
4. è¨å®ãã¡ã¤ã«ï¼Webãµã¼ãã¼ï¼default.confï¼
ããã¯ã¨ã³ãã®Webãµã¼ãã¼ã®Nginxã®è¨å®ã§ãã
/etc/nginx/conf.d/default.conf
server { listen 80; server_name _; location / { root /usr/share/nginx/html; index index.php index.html index.htm; try_files $uri $uri/ /index.php?$args; } location ~ .php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } }
5. æ¯è¼
ä»åã®ãã¼ããã©ã³ãµã¼1å°ï¼Webãµã¼ãã¼3å°ã§è² è·åæ£ãããå ´åã¨ååã¾ã§ã®proxy cacheã®ç°å¢ã®é度ãæ¯è¼ãã¦ã¿ããã¨æãã¾ãã
â»ä»åã®ç°å¢ã§ã¯proxy cacheã®è¨å®ããã¦ãã¾ããã
ãã¼ã«ã«ã®ç°å¢ããabã³ãã³ãã§ãã³ãã¼ãã¼ã¯ã®æ¸¬å®ãããçµæã§ãã
â»ãµã¼ãã¼ã¯ä¸¡æ¹ã¨ãEC2ã§Zoneã¯ap-southeast-1bãTypeã¯t1.microã¨ãªã£ã¦ãã¾ãã
ãã¼ããã©ã³ãµã¼
ãã¹ã¦ã®ãªã¯ã¨ã¹ããå®äºããã®ã«ããã£ãæéã¯ç´2.6ç§ã§ããã
Time taken for tests: 2.668697 seconds
ab -n 50 -c 5 http://ec2-XXX-XXX-XXX-130.ap-southeast-1.compute.amazonaws.com/wordpress/?p=1 Benchmarking ec2-54-251-32-130.ap-southeast-1.compute.amazonaws.com (be patient).....done Server Software: nginx/1.0.15 Server Hostname: ec2-XXX-XXX-XXX-130.ap-southeast-1.compute.amazonaws.com Server Port: 80 Document Path: /wordpress/?p=1 Document Length: 6919 bytes Concurrency Level: 5 Time taken for tests: 2.668697 seconds Complete requests: 50 Failed requests: 0 Write errors: 0 Total transferred: 358900 bytes HTML transferred: 345950 bytes Requests per second: 18.74 [#/sec] (mean) Time per request: 266.870 [ms] (mean) Time per request: 53.374 [ms] (mean, across all concurrent requests) Transfer rate: 131.15 [Kbytes/sec] received
proxy cache
ãã¹ã¦ã®ãªã¯ã¨ã¹ããå®äºããã®ã«ããã£ãæéã¯ç´1.2ç§ã§ããã
Time taken for tests: 1.284296 seconds
ab -n 50 -c 5 http://ec2-XXX-XXX-XXX-124.ap-southeast-1.compute.amazonaws.com/wordpress/?p=1 Benchmarking ec2-XXX-XXX-XXX-124.ap-southeast-1.compute.amazonaws.com (be patient).....done Server Software: nginx/1.0.15 Server Hostname: ec2-XXX-XXX-XXX-124.ap-southeast-1.compute.amazonaws.com Server Port: 80 Document Path: /wordpress/?p=1 Document Length: 6703 bytes Concurrency Level: 5 Time taken for tests: 1.276036 seconds Complete requests: 50 Failed requests: 0 Write errors: 0 Total transferred: 348200 bytes HTML transferred: 335150 bytes Requests per second: 39.18 [#/sec] (mean) Time per request: 127.604 [ms] (mean) Time per request: 25.521 [ms] (mean, across all concurrent requests) Transfer rate: 266.45 [Kbytes/sec] received
ãã¯ãproxy cacheã®ç°å¢ã®æ¹ãéãã§ãããè² è·åæ£ããç°å¢ã§ã¯2ç§å°ã¨ãªãã5ç§ä»¥ä¸ããã£ã¦ããä½ã対å¿ããããªã£ã¦ããªã1å°æ§æã®ç°å¢ããã¯é«éåãã¾ãã
proxy cacheã®ç°å¢ã¨ã®æ¯è¼ã§ãã¨éããã³ã¹ãå ±ã«åãæªãã§ãããé常ã¯proxy cacheã¨è² è·åæ£ã®ä¸¡æ¹ãçµã¿åããã¦å©ç¨ãããã¨ã«ãªãã¨æãã¾ãã
ä½ãããNginxã®ãã¼ããã©ã³ãµã¼ã®è¨å®ã¯ä¸è¨ã®ããã«ã·ã³ãã«ã§ç°¡åãªã¨ãããããã§ããã
ãã®ããã«Nginxã使ãã¨ç°¡åã«ãµã¤ããé«éåã§ãããã¨ããããã¾ããããã ããã®ã¾ã¾ã®æ§æã§ãã¨éç¨ä¸åé¡ã¨ãªãç¹ãããã¾ãã
WordPressã®ç®¡çç»é¢ããç»åãã¢ãããã¼ãããå ´åã®ãã¼ã¿ã®ä¿åå ãã©ãã«ããããSPOFï¼åä¸é害ç¹ï¼ã¨ãªã£ã¦ããç®æï¼DBã¨ãã¼ããã©ã³ãµã¼ï¼ãã©ããããããªã©ã§ãã
ãã®è§£æ±ºæ¹æ³ãå æ¥ã®ãã©ã¹ã«ã¤æ§ã¨ã®åå¼·ä¼ã§èª¬æããã¦ããã ãã¾ããã
ãåæã«AWSã¯ã©ã¦ããã¶ã¤ã³ãã¿ã¼ã³ ãã£ã³ãã¼ã³ãµã¤ãç¯ã
次åã¯ãä¸è¨ã®ã¹ã©ã¤ãã«è£è¶³èª¬æãå ãã¦ãAWSã®ãµã¼ãã¹ã使ã£ãWordPressã®ãµã¤ãã®ã«ã¤ã¼ã³ã®æµãã説æãããã¨æãã¾ãã