{"id":1563,"date":"2018-10-24T09:34:07","date_gmt":"2018-10-24T09:34:07","guid":{"rendered":"https:\/\/www.appservgrid.com\/paw92\/?p=1563"},"modified":"2018-10-25T02:14:28","modified_gmt":"2018-10-25T02:14:28","slug":"nginx-caching-for-wordpress-using-fastcgi_cache","status":"publish","type":"post","link":"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/24\/nginx-caching-for-wordpress-using-fastcgi_cache\/","title":{"rendered":"Nginx Caching for WordPress using fastcgi_cache"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/linuxadmin.io\/wp-content\/uploads\/2017\/05\/fastcgi_nginx_cache.png\" alt=\"Fastcgi_cache Nginx\" width=\"667\" height=\"159\" \/><\/p>\n<p>Caching PHP requests can dramatically reduce server resources per request and make the pages load time decrease substantially. In this tutorial we are going to utilize the fastcgi_cache functions in nginx to cache PHP requests making it.<\/p>\n<p>This tutorial assumes you have the following already completed on your server:<br \/>\nNginx installed, if you do not please follow \u2013 <a href=\"https:\/\/linuxadmin.io\/nginx-compile-source\/\">Nginx Compile From Source On CentOS<br \/>\n<\/a>The ngx_cache_purge module already installed \u2013 <a href=\"https:\/\/linuxadmin.io\/add-ngx_cache_purge-module-to-nginx\/\">How to install the ngx_cache_purge module in Ningx<\/a><br \/>\nFastCGI setup and running \u2013 <a href=\"https:\/\/linuxadmin.io\/php-fpm-php7-source\/\">PHP-FPM Installation<\/a><\/p>\n<p>It also assumes you already have a WordPress installation as this will just cover setting up the fastcgi_cache to work with WordPress.<\/p>\n<h3>Nginx fastcgi_cache Configuration<\/h3>\n<p>First make a directory in \/var\/run , this is where the fastcgi_cache will store the files in memory<\/p>\n<p>mkdir \/var\/run\/nginx-cache<\/p>\n<p>You will then need to edit the Nginx configuration<\/p>\n<p>nano \/etc\/nginx\/nginx.conf<\/p>\n<p>You will want to add the following lines in the http{} block before the server{} configuration<\/p>\n<p>fastcgi_cache_path \/var\/run\/nginx-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m;<br \/>\nfastcgi_cache_key &#8220;$scheme$request_method$host$request_uri&#8221;;<br \/>\nfastcgi_cache_use_stale error timeout invalid_header http_500;<br \/>\nfastcgi_ignore_headers Cache-Control Expires Set-Cookie;<\/p>\n<p>The above lines set the caching directory, cache levels and keys_zone. The fastcgi_cache_use_stale option will try to use cached files even if PHP-FPM has crashed or has been shutdown.<\/p>\n<p>You will then want to add the following to the server{} configuration:<\/p>\n<p>add_header X-Cache $upstream_cache_status;<\/p>\n<p>What this does is allows you to see if Nginx is caching a request later on<\/p>\n<p>if ($request_method = POST) {<br \/>\nset $skip_cache 1;<br \/>\n}<\/p>\n<p>if ($request_uri ~* &#8220;\/wp-admin\/|\/xmlrpc.php|wp-.*.php|index.php|\/feed\/|sitemap(_index)?.xml&#8221;) {<br \/>\nset $skip_cache 1;<br \/>\n}<\/p>\n<p>if ($http_cookie ~* &#8220;comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in&#8221;) {<br \/>\nset $skip_cache 1;<br \/>\n}<\/p>\n<p>The above statements allow you to bypass the cache when you are logged into the administrative interface etc.<\/p>\n<p>Add the following to location ~ .php$ {} block that sends the PHP requests to PHP-FPM<\/p>\n<p>fastcgi_cache_bypass $skip_cache;<br \/>\nfastcgi_no_cache $skip_cache;<br \/>\nfastcgi_cache WORDPRESS;<br \/>\nfastcgi_cache_valid 200 302 60m;<br \/>\nfastcgi_cache_valid 301 1h;<br \/>\nfastcgi_cache_valid any 1m;<\/p>\n<p>The above block fast_cache_valid entries specify which type of requests to cache and for how long.<\/p>\n<p>And finally add a purge folder to in the server{} configuration:<\/p>\n<p>location ~ \/purge(\/.*) {<br \/>\nallow 127.0.0.1;<br \/>\ndeny all;<br \/>\nfastcgi_cache_purge WORDPRESS &#8220;$scheme$request_method$host$1&#8221;;<br \/>\n}<\/p>\n<p>Once all of that has been completed, you can go ahead and restart nginx.<\/p>\n<p>\/etc\/init.d\/nginx restart<\/p>\n<h3>Nginx Helper Plugin Configuration<\/h3>\n<p>You will then want to cd to the wp-content\/plugins directory and get the <a href=\"https:\/\/wordpress.org\/plugins\/nginx-helper\/\">Nginx Helper plugin<\/a><\/p>\n<p>wget https:\/\/downloads.wordpress.org\/plugin\/nginx-helper.1.9.10.zip<\/p>\n<p>Uncompress the zip file<\/p>\n<p>unzip nginx-helper.1.9.10.zip<\/p>\n<p>Activate the plugin in WordPress and select the following options:<br \/>\nUnder Purging Options select:<\/p>\n<p>Enable Cache<\/p>\n<p>Under Caching Method select:<\/p>\n<p>Nginx FastCGI cache<\/p>\n<p>Under Purge Method select:<\/p>\n<p>Using a GET request to PURGE\/url(Default option)<\/p>\n<p>And click save to save the configuration.<\/p>\n<p>To test you should now try a curl to check the headers to ensure a HIT for the X-Cache header We set earlier.<\/p>\n<p># Curl -Is http:\/\/domain.com |grep X-Cache<br \/>\nX-Cache: MISS<\/p>\n<p>If this is the first request to that page since the cache has been active that is expected. A MISS means the page has not been cached yet.<\/p>\n<p>On subsequent requests you should see<\/p>\n<p># Curl -Is http:\/\/domain.com |grep X-Cache<br \/>\nX-Cache: HIT<\/p>\n<p>If you do get a X-Cache: BYPASS one of the rules we set earlier is causing the cache to be ignored. Try testing your site in one of the various speed test programs such as https:\/\/tools.pingdom.com you should notice a much improved load time over a non-cached WordPress install.<\/p>\n<p>May 29, 2017LinuxAdmin.io<\/p>\n<p><a href=\"https:\/\/linuxadmin.io\/nginx-caching-wordpress-using-fastcgi_cache\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Caching PHP requests can dramatically reduce server resources per request and make the pages load time decrease substantially. In this tutorial we are going to utilize the fastcgi_cache functions in nginx to cache PHP requests making it. This tutorial assumes you have the following already completed on your server: Nginx installed, if you do not &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.appservgrid.com\/paw92\/index.php\/2018\/10\/24\/nginx-caching-for-wordpress-using-fastcgi_cache\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Nginx Caching for WordPress using fastcgi_cache&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1563","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1563","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/comments?post=1563"}],"version-history":[{"count":1,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1563\/revisions"}],"predecessor-version":[{"id":1621,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/posts\/1563\/revisions\/1621"}],"wp:attachment":[{"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/media?parent=1563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/categories?post=1563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appservgrid.com\/paw92\/index.php\/wp-json\/wp\/v2\/tags?post=1563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}