Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

WordPress + NGINX Best Practices with EasyEngine

5,426 views

Published on

Whether for speed, security or scalability, a WordPress site can be improved using NGINX.

View full webinar on-demand at: http://nginx.com/resources/webinars/taste-nginx-conf-wordpress-nginx-best-practices-easyengine/

Published in: Technology

WordPress + NGINX Best Practices with EasyEngine

  1. 1. WordPress-NGINX Best Practices (With EasyEngine) by Rahul Bansal (rtCamp)
  2. 2. WordPress-NGINX - Best practices
  3. 3. The Rule! “If you can do it in NGINX, just do it in NGINX!”
  4. 4. #1. nginx fastcgi-cache
  5. 5. Purpose ● Best solution for serving content to non-logged in users ● Should be used instead full-page-cache from WordPress plugin
  6. 6. Global Code Fragment fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m; fastcgi_cache_key "$scheme$request_method$host$request_uri"; fastcgi_cache_use_stale error updating timeout invalid_header http_500;
  7. 7. Site config server { location ~ .php$ { try_files $uri =404; include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_cache WORDPRESS; fastcgi_cache_valid 60m; fastcgi_cache_bypass $skip_cache; fastcgi_no_cache $skip_cache;}}
  8. 8. Cache conditions set $skip_cache 0; if ($request_method = POST) { set $skip_cache 1; } if ($query_string != "") { set $skip_cache 1; } if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp- .*.php|index.php") { set $skip_cache 1; } if ($http_cookie ~* "comment_author|wordpress_[a-f0- 9]+|wordpress_logged_in") { set $skip_cache 1; }
  9. 9. Cache conditions if ($request_uri ~* "/store/|/random/|/wp-admin/|...") { set $skip_cache 1; } if ($http_cookie ~* "comment_author||yummy_cookie|...") { set $skip_cache 1; } if ($remote_addr ~* "1.2.3.4") { set $skip_cache 1; }
  10. 10. Caching search result pages if ($query_string != "") { set $skip_cache 1; } # for http://example.com/?s=hello if ($arg_s != "") { set $skip_cache 0; }
  11. 11. Caching multiple versions of a page # Cookie Example fastcgi_cache_key "$scheme$request_method$host$request_uri$cookie_country"; # User-Agent Example set $mobile no; if ($http_user_agent ~* "iphone|android|blackberry|netfront") { set $mobile yes;} fastcgi_cache_key "$scheme$request_method$host$request_uri$mobile";
  12. 12. #2. NGINX substitution module CDN & SSL Warning fix
  13. 13. CDN (Origin Pull) Original URL => example.com/wp-content/uploads/hello.jpg CDN URL => cdn.example.com/wp-content/ uploads/hello.jpg sub_filter "example.com/wp-content/uploads/" "cdn.example.com/wp-content/uploads/"; sub_filter_last_modified on; sub_filter_once off;
  14. 14. SSL - mixed content warning fix sub_filter "http://example.com/wp-content/" "https://example.com/wp-content/"; sub_filter_last_modified on; sub_filter_once off;
  15. 15. #3. pagespeed module Reduce load time
  16. 16. pagespeed module ● css/js minify & combine. ● search-replace URL for CDN ● on the fly image (lossless) compression ● lazy loading for images
  17. 17. site specific filters # CSS pagespeed EnableFilters combine_css,rewrite_css; # JS pagespeed EnableFilters combine_javascript,rewrite_javascript; # Images pagespeed EnableFilters lazyload_images; pagespeed EnableFilters rewrite_images; pagespeed EnableFilters convert_jpeg_to_progressive
  18. 18. pagespeed global config # Turning the module on and off pagespeed on; # Configuring PageSpeed Filters pagespeed RewriteLevel PassThrough; # Needs to exist and be writable by nginx. Use tmpfs for best performance. pagespeed FileCachePath /var/ngx_pagespeed_cache;
  19. 19. #4. Upstream Module Load Balancer/Failover
  20. 20. load balance mode upstream backend { ip_hash; server 1.1.1.1; server 2.2.2.2; server 2.2.2.2; }
  21. 21. failover mode upstream backend { server 1.1.1.1 max_fails=3 fail_timeout=30s; server 2.2.2.2 backup; }
  22. 22. Front-end site config location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; #proxy_cache }
  23. 23. Backend NGINX Config #gloabal config set_real_ip_from 9.9.9.9; real_ip_header X-Forwarded-For;
  24. 24. What if load balancer fails? ● Configure backend server with NGINX in a way that it can run as standalone server ● Use DNS-based failover to switch traffic from load balancer to a backend server
  25. 25. #5. Upstream mod with HHVM Speed for logged in users
  26. 26. HHVM with PHP-FPM fallback upstream php { server 127.0.0.1:8000; #hhvm server 127.0.0.1:9000 backup; #php-fpm }
  27. 27. More Tricks The list is endless!
  28. 28. SSL and spdy #enable SSL cache ssl_session_cache shared:SSL:20m; ssl_session_timeout 10m; #enable spdy server { listen 443 ssl spdy; }
  29. 29. URL Redirection #For Single URL location = /old/path { return 301 http://foo.com/new/url; } #For Complete Section location ~ /old/(.*) { return 301 http://foo.com/new/$1; }
  30. 30. Security with Limit Request Module #global limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; #server location = /wp-login.php { limit_req zone=one burst=1 nodelay; include fastcgi_params; fastcgi_pass 127.0.0.1:9000; }
  31. 31. Memcache/Redis - Distributed Cache ● For wordpress site is spread across multiple servers ● Only extra work is, WordPress/PHP side codes are needed to “fill” cache ● For Memcache, NGINX has a core and few third party modules ● For redis, NGINX has third party modules
  32. 32. Using EasyEngine
  33. 33. What is EasyEngine? ● A script to setup and manage multiple wordpress sites with NGINX web server ● Always up to date with best practices, including most of the stuff we discussed so far
  34. 34. Getting Started #install easy-engine wget -qO ee rt.cx/ee && sudo bash ee #install nginx, php, mysql, postfix ee stack install #install WordPress on example.com ee site create example.com --wpfc
  35. 35. WordPress Multisite Handling #multisite with subdirectory ee site create example.com --wpfc --wpsubdir #multisite with subdomain ee site create example.com --wpfc --wpsubdom
  36. 36. EasyEngine Demo
  37. 37. EasyEngine Resources Home : http://rtcamp.com/easyengine Github : http://github.com/rtCamp/easyengine Forum : http://community.rtcamp.com/category/easyengine/
  38. 38. Q&A mailto: rahul.bansal@rtcamp.com or better, join the discussion on http://community.rtcamp.com/

×