If you’re running a WordPress blog on a Nginx webserver, you should also enable FastCGI caching module. Nginx webserver is fast. When you combine Nginx and FastCGI caching module, you’ll greatly improve the performance of your web application, including a WordPress site.
FastCGI module has a Nginx directive for caching dynamic content that are served through PHP backend. When a web page is cached, repeated requests for the same page by web clients is quickly returned by the webserver because the page is coming from a cached location.
Instead of the webserver compiling all the dynamic data the makes up the page before returning it to the web clients on each request, a page is cached as a whole (all the data the makes up the page are stored together) and that the webserver doesn’t have to fetch dynamic data before returning that page to web clients.
This is an advantage of caching web pages.
This blog assumes that you already have working Nginx, PHP-FPM and WordPress setup. Check out our blog on setting up Nginx and WordPress.
Setup Nginx FastCGI Directive In Nginx.Conf file.
Most Linux distributions got Nginx main config file in /etc/nginx directory. In there you should see nginx.conf. This is the main configuration file for Nginx.
Open the file and add these FastCGI directives in the http block { …..}. The http block begins with http { and ends with }.
fastcgi_cache_path /var/cache/nginx/fastcgi_temp levels=1:2 keys_zone=CZONE:15m inactive=60m;fastcgi_cache_key "$scheme$request_method$host$request_uri";fastcgi_cache_use_stale error timeout invalid_header http_500;
Save the file. The highlighted location is where the cached content will be stored. The keys_zone is a unique name to identify the cache location.
Setup PHP Directives In Default.conf file.
When you’re done with nginx.conf file, go to the site definition file. This file is usually at /etc/nginx/conf.d/default.conf. Edit it and add these PHP location directives.
location ~ \.php$ {fastcgi_split_path_info ^(.+\.php)(/.+)$;fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;include fastcgi_params;fastcgi_intercept_errors on;fastcgi_ignore_client_abort off;fastcgi_connect_timeout 60;fastcgi_send_timeout 90;fastcgi_read_timeout 90;fastcgi_buffer_size 128k;fastcgi_buffers 4 256k;fastcgi_busy_buffers_size 256k;fastcgi_temp_file_write_size 256k;fastcgi_cache_bypass $no_cache;fastcgi_no_cache $no_cache;fastcgi_cache CZONE;fastcgi_cache_valid 200 302 1h;fastcgi_cache_valid 301 1h;fastcgi_cache_valid any 1h;fastcgi_cache_min_uses 2;}
Save the file.
The highlighted lines machines the
Do Not Cache Files For Logged In Users
These lines below prevent caching for logged-in users. Those who signed in to WordPress will always see live data and no cached content.
Add these lines in the default site configuration file in the Server block { ……. }. The server block is the one the begins with Server { and ends with }.
set $no_cache 0;if ($request_method = POST) {set $no_cache 1;}if ($query_string != "") {set $no_cache 1;}if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {set $no_cache 1;}if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {set $no_cache 1;}
Save the file and you’re almost done.
Setup PHP-FPM To Complete The Setup
Finally, open PHP-FPM config file and set the listen socket to what was added in Nginx config.
;listen = 127.0.0.1:9000listen = /var/run/php-fpm/php-fpm.sock
Change the listen directives to match what is above and save the file.
That’s it.
Reload Nginx and PHP-FPM the load the settings.
service nginx reload
Enjoy!
Frequently Asked Questions
How can I improve Wordpress performance with Nginx?
You can greatly improve Wordpress performance by enabling the FastCGI caching module in Nginx.
What is the advantage of using FastCGI caching module with Nginx?
FastCGI caching module in Nginx caches dynamic content served through PHP backend, reducing the need for the webserver to fetch dynamic data for each request.
Where can I find the Nginx main configuration file to add FastCGI directives?
You can find the Nginx main configuration file, nginx.conf, in the /etc/nginx directory. Add FastCGI directives in the http block of this file.
What is the purpose of the 'fastcgi_cache_path' directive in Nginx?
The 'fastcgi_cache_path' directive in Nginx defines the location where the cached content will be stored, improving web application performance.
How do I identify the cache location in Nginx FastCGI caching setup?
The 'keys_zone' parameter in the FastCGI caching setup is a unique name used to identify the cache location for improved performance.
Where should I add PHP directives for FastCGI caching in Nginx?
Add PHP directives for FastCGI caching in the site definition file, usually located at /etc/nginx/conf.d/default.conf.
What are the recommended levels for 'fastcgi_cache_path' in Nginx?
Set the levels parameter in 'fastcgi_cache_path' to 1:2 for efficient caching of dynamic content in Nginx.
How does FastCGI caching in Nginx help in serving repeated requests for web pages?
FastCGI caching in Nginx stores cached web pages as a whole, reducing the need for the webserver to compile dynamic data for each repeated request.