In this article, we will explain how to enable fastcgi_cache caching in Nginx via Pagoda Panel to improve the access speed of your WordPress website and reduce the burden on the server. First, we will briefly introduce the concept and role of fastcgi_cache cache. Then, we will give a detailed guide on how to configure Nginx to enable fastcgi_cache cache in Pagoda panel to set up the plugin.
What is fastcgi_cache cache?
fastcgi_cache is a caching mechanism provided by Nginx to improve the performance of dynamic content. It is particularly suited to applications that deal with the FastCGI protocol, such as PHP (Wordpress). fastcgi_cache enables static web pages at the system level, greatly reducing server load, concurrency, and providing increased site loading speed.
What are the advantages of fastcgi_cache caching?
Many people will be curious, for Wordpress, fastcgi_cache cache and regular through Wp Rocket and other plug-ins to achieve the cache what is the difference? fastcgi_cache cache is thesystem levelCaching, handled only by Nginx (PHP is not involved), so it's fast and can take a lot of concurrency. The downside is that it's not as flexible, has fewer features, and is a bit of a barrier to use, so white people rarely hear about this cache.
ByWp RocketThe caching implemented by plugins such assoftware levelThe cache is handled by PHP (Nginx is not involved), so it is slower and takes up more server resources. Advantages are controlled by plug-ins, easy to use and understand, very flexible, feature-rich, the public's favorite.
We deal with servers all the time, dealing with high concurrency and lag issues. Most of the time, memory is exhausted or CPU is overloaded because PHP is running a huge task. fastcgi_cache cache does not need to run PHP, so it takes up much less resources, greatly reducing the burden on the server.
At the same time fastcgi_cache cache is handled by Nginx, so the computation link is much shorter than the cache implemented by plugins such as Wp Rocket, and therefore faster. fastcgi_cache cache is even faster than Litespeed cache cache in some environments!
Comparison of average response times for various caches↓ (shorter is better).
Caution.FastCGI_Cache on the server performance requirements are high, if it is 1H1G or other low configuration, and then how to optimize the effect is not obvious. If the website has more dynamic data, it is not recommended to use FastCGI_Cache cache.
Now most people use Pagoda Panel to build websites, which is convenient and time-saving. Here is how to open FastCGI_Cache cache in Pagoda Panel.
Note: Since FastCGI_Cache caches PHP requests, it may cause site exceptions. For example, read count invalidation, caching of commenter information, caching of login screen, etc. Additional code needs to be added to exclude this. Therefore, it is not recommended for newbies to use FastCGI_Cache caching.FastCGI_Cache caching requires the use of a specialized caching plugin.
How to enable fastcgi_cache caching in Nginx
The Pagoda panel is compiled with the Nginx ngx_cache_purge module by default, so you do not need to install it additionally.
Global Settings
1 Log in to Pagoda background, find Nginx in the software store, click the Settings button, and add the following contents in the "Configuration Modification":
fastcgi_cache_path /tmp/wpcache levels=1:2 keys_zone=WORDPRESS:250m inactive=1d max_size=1G;
fastcgi_temp_path /tmp/wpcache/temp;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
# ignore all nocache declarations, avoid not caching pseudo-static, etc.
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
The screenshot after adding is as follows.
Site Setup
Find the corresponding website in the list of websites in Pagoda background, click the "Settings" button, add the following code to the configuration file, and enter the IP (extranet) address of the web server in line 43.The code has to be adjusted as needed! It varies from site to site! Once again, I'm discouragedyoung white.
set $skip_cache 0;
#post accesses are not cached
if ($request_method = POST) {
set $skip_cache 1;
}
# dynamic queries are not cached
if ($query_string ! = "") {
set $skip_cache 1;
}
# backend and other specific pages are not cached (please add them for other requirements)
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-. *.php|/feed/|index.php|sitemap(_index)? .xml") {
set $skip_cache 1;
}
# does not show cache for logged in users, users who have commented
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $skip_cache 1;
}
# here please refer to your site before the configuration, especially the path of the sock, get it wrong 502! If your site uses PHP7.4, write -74.sock
location ~ [^/]\.php(/|$)
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi-74.sock;
fastcgi_index index.php;
include fastcgi.conf;
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
# new cache rules
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
add_header X-Cache "$upstream_cache_status From $host";
fastcgi_cache WORDPRESS;
add_header Cache-Control max-age=0;
add_header Nginx-Cache "$upstream_cache_status";
add_header Last-Modified $date_gmt;
add_header X-Frame-Options SAMEORIGIN; # allow only frames to be nested on this site
add_header X-Content-Type-Options nosniff; # disable sniffing of file types
add_header X-XSS-Protection "1; mode=block"; # XSS protection
etag on;
fastcgi_cache_valid 200 301 302 1d;
}
# cache cleanup configuration
location ~ /purge(/. *) {
allow 127.0.0.1;
allow "server extranet IP"; # quotes to be kept
deny all;
fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
}
The screenshot after adding is as follows.
After adding and saving, restart Nginx on it. The next step is to install the Setup Cache plugin ↓.
Install the WordPress Clean Cache plugin
FastCGI_Cache cannot automatically clean up the site cache in a timely and accurate manner, so you need to install the supporting cache plugin to automatically control the refreshing cache. Backend search, installationNginx Helperplugin, this plugin is built specifically for fastcgi_cache caching.
The plugin settings are referenced in the image below (image source. Zhang Ge Blog).
Choose Delete local server cache files for cleanup mode to delete cache files directly from the server and regenerate them.
My friend Nana built the site and found that the Nginx fastcgi_cache cache (404ms) is a little faster than the WP Super Cache + Memcached Object Cache cache (455ms).
Determining cache status
Open the frontend of the website in incognito mode of your browser (not logged in), press F12 to go to Developer Tools > Network > Tap on the URL > Tap on the "Header" to view the details.
HIT: Cache Success
MISS: Cache failure, suggest refreshing the page 2 times to see if it has become HIT.
BYPASS: Skip Cache
EXPIRED: Cache expired
Advanced Tutorials
indeedThe fastcgi_cache cache can also be used with the Wp Rocket cache plugin. Using it together is more effective than using theWp Rocket is about 27% higher than the standaloneThe fastcgi_cache cache is not much different. Just install therocket-nginx, just configure it appropriately.This method has a high threshold for use, so don't use it if you don't know how.
8 thoughts on “宝塔面板Nginx如何启用fastcgi_cache缓存? WordPress提速”
Thanks for the tutorial! I followed the process once, and found that after turning it on successfully, visiting the homepage will automatically jump to: /wp-admin/setup-config.php Show: File
wp-config.php
already exists. If you wish to reset any of the configuration items in this file, delete the file first. You can install it now. However, accessing the home page while the administrator is logged in will not jump.Is the wp-config.php file in the root directory (the folder whose name is the URL)? This problem is usually caused by the wrong location of the system files when you install wordpress. If the wp-config.php file is in the right place, try to clean up the cache and disable all the plug-ins to find out if there is a plug-in problem.
Confirmed it's not a browser cache thing, and I'm testing with a new site and all plugins turned off as well. I just tested on my side, as long as you delete the site's configuration file in the new code about fastcgi_cache cache, the home page is immediately accessible; add will jump to the tip
wp-config.php
It already exists. I've searched in English for a long time as well and didn't find an answer. I'm cracking up ...... 😭Is the wp-config.php file in the root directory (the folder with the name url)? Make sure first. Maybe the configuration code was copied wrong? Check it.
I found the reason, nginx was not compiled and installed.
crying without tears...
Hello, after your successful configuration, the following error appears in the "Tools" - "Site Health" in the wordpress backend, may I ask how to solve it?REST API encountered unexpected results Performance REST API is a way for WordPress and other applications to communicate with the server. a way for WordPress and other applications to communicate with the server. For example, the Block Editor page relies on the REST API to display and save your pages and posts. When testing the REST API returned unanticipated results: REST API Endpoint: my site URL/index.php/wp-json/wp/v2/types/post?context=editREST API Response: (404) Not Found
Hello, there are a number of reasons for errors with the REST API, not necessarily because the Fastcgi_cache cache. We recommend disabling plugins one by one, switching to the default theme to see if the problem is solved. If you are sure it is Fastcgi_cache caching issues please reply to this comment.
Many times the REST API does not work, if it does not affect the functionality of the site and so on, it is recommended to properly ignore this error.