The fastcgi_cache module enables Nginx-level page caching, effectively improving website concurrency, reducing PHP execution time and request response latency. This module serves cached content directly from Nginx without involving PHP or WordPress, delivering significant performance gains for sites without third-party advertisements. Most tutorials available online configure unified caching for both desktop and mobile clients. This guide explains how to implement separate cache pools for mobile and desktop traffic.
Prerequisites
The Baota panel includes the ngx_cache_purge module by default during Nginx compilation, so no additional installation is required.
Global Configuration
Log into the Baota admin panel, navigate to the software store, locate Nginx, and click the settings button. Add the following directives in the configuration modification section:
fastcgi_temp_path /var/cache/nginx/temp;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
# Detect mobile devices
map $http_user_agent $is_mobile {
default 0;
~*mobile 1;
}
# Separate cache pools for desktop and mobile
fastcgi_cache_path /var/cache/nginx/desktop levels=1:2 keys_zone=wp_cache_desktop:64m inactive=1d;
fastcgi_cache_path /var/cache/nginx/mobile levels=1:2 keys_zone=wp_cache_mobile:64m inactive=1d;
Site-Specific Configuration
In the Baota website list, select the target website and click settings. Add the following configuration to the site configuration file. Adjust the PHP socket path accordnig to your PHP version (line 56 adjustment):
set $skip_cache 0;
set $cache_pool wp_cache_desktop;
# Route to mobile cache pool
if ($is_mobile) {
set $cache_pool wp_cache_mobile;
}
# Skip caching for POST requests
if ($request_method = POST) {
set $skip_cache 1;
}
# Skip caching for query strings
if ($query_string != "") {
set $skip_cache 1;
}
# Exclude admin and system pages from caching
if ($request_uri ~* "/wp-admin|/wp-.*.php|index.php|sitemap(_index)?.xml|sitemap.txt|sitemap.xml|sitemap.html|tag.txt|sitemap([0-9]+)\.txt|sitemap([0-9]+)\.xml|tag([0-9]+)\.txt") {
set $skip_cache 1;
}
location ~ [^/]\.php(/|$)
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi-74.sock;
fastcgi_index index.php;
include fastcgi.conf;
include pathinfo.conf;
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
# Caching directives
fastcgi_cache_bypass $skip_cache;
fastcgi_no_cache $skip_cache;
add_header X-Cache "$upstream_cache_status From $host";
fastcgi_cache $cache_pool;
add_header Cache-Control max-age=0;
add_header Last-Modified $date_gmt;
add_header Nginx-Cache "$upstream_cache_status";
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_valid 200 301 302 30m;
}
After saving the configuration, restart Nginx to apply the changes.
Verifying Cache Operation
Open the website in an incognito browser window (ensuring no logged-in session), press F12 to access developer tools, navigate to the Network tab, click on the request URL, and inspect the headers.
- HIT: Cache successfully served
- MISS: Cache miss — refresh the page 2-3 times to check if status changes to HIT
- BYPASS: Request bypassed cache
- EXPIRED: Cached content has expired