Profile

How to enable fastcgi_cache caching in Pagoda Panel Nginx? WordPress speedup

Enable fastcgi_cache cache
Article List
Article List

FastCGI_cache is a caching module for Nginx that enables static web pages from the Nginx level, effectively improving the concurrency of websites, reducing PHP runtime and request response time, and greatly improving page load speed.

Fastcgi_cache is able to provide cached content directly at the nginx level without involving PHP or WordPress, accelerating the results more than theWp RocketIt's okay. In contrast the caching plugin still requires each request to be handled by PHP at the application level, consuming more CPU.

Some members of the group said that they installed and compared Nginx servers with fastcgi_cache caching enabled to be faster than Litespeed servers. However, the caching plugin can still come with many features, such as the ability to benchmark and control the content of the cache.

Comparison of average response times for various caches↓ (shorter is better).

Pasted 108

 

Of course, the premise is that your server itself has sufficient performance, if only 1H1G, and then how to optimize can not significantly improve the results. Now most people build a site using the Pagoda panel, convenient and hassle-free. The following shares the Pagoda panel to open FastCGI_Cache cache method.

Note: Since FastCGI_Cache caches PHP requests, it may cause site exceptions. For example, invalid reading count, cached commenter information, cached login screen, etc. Additional code needs to be added to fix this. Novice users are not recommended to use FastCGI_Cache caching. Enabling FastCGI_Cache caching requires the use of a specialized caching plugin, not something like Wp Rocket.

 

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.

Pasted 104

 

Site Setup

Find the corresponding website in the list of websites in the Pagoda backend, click the "Settings" button, and add the following code to the configuration file. The code should be adjusted as needed, enter the web server IP (external network) address in line 43.

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.

Pasted 105
After you add it, save it, restart Nginx, and the cache is added.

 

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).

fastcache

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.

Pasted 106

Pasted 107

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

End

Article out of date? Have a better opinion? Have a question? Please leave your comments below and we will follow up.

Recommended Articles.
Comments :

2 thoughts on “宝塔面板Nginx如何启用fastcgi_cache缓存? WordPress提速”

  1. 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

    1. 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.

发表评论

The email address will not be disclosed, and the URL needs to be removed from https://前缀 for insertion.

Chat
  • WeChat Service
迪亚莫建站
Quick login without registration

Enter your username and password to log in

Have no account? Forgot password?