Configuring Nginx fastcgi_cache for Separate Desktop and Mobile WordPress Caching on BaoTa

Nginx's fastcgi_cache module facilitates static page caching directly at the server level, bypassing PHP execution to drastically improve response times and concurrency. BaoTa panel ships with the ngx_cache_purge module pre-compiled, allowing immediate configuration without additional installations. To properly serve cached content, desktop and mobile user agents must be isolated into separate cache zones.

Global Nginx Configuration

Log into the BaoTa dashboard, navigate to the Software store, select Nginx, and click Settings. Under the Configuration Modification tab, append the following directives:

fastcgi_temp_path /var/nginx_cache/tmp;
fastcgi_cache_key "$scheme$request_method$server_name$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

map $http_user_agent $device_type {
    default          desktop;
    ~*mobile|android|iphone  mobile;
}

fastcgi_cache_path /var/nginx_cache/desktop levels=1:2 keys_zone=desktop_zone:128m inactive=24h;
fastcgi_cache_path /var/nginx_cache/mobile levels=1:2 keys_zone=mobile_zone:128m inactive=24h;

Site-Specific Configuration

Locate your target website in the BaoTa website list, click Settings, and access the Configuration file. Insert the following logic. Ensure the PHP socket path matches your current environment to prevent 502 errors.

set $do_not_cache 0;
set $active_zone desktop_zone;

if ($device_type = mobile) {
    set $active_zone mobile_zone;
}

if ($request_method = POST) {
    set $do_not_cache 1;
}

if ($query_string != "") {
    set $do_not_cache 1;
}

if ($request_uri ~* "/wp-admin|/wp-.*\.php|/index\.php|/sitemap") {
    set $do_not_cache 1;
}

location ~ [^/]\.php(/|$) {
    try_files $uri =404;
    fastcgi_pass unix:/tmp/php-cgi-80.sock;
    fastcgi_index index.php;
    include fastcgi.conf;
    include pathinfo.conf;

    add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
    fastcgi_cache_bypass $do_not_cache;
    fastcgi_no_cache $do_not_cache;
    add_header X-Cache-Status $upstream_cache_status;
    fastcgi_cache $active_zone;
    add_header Cache-Control max-age=0;
    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$server_name$request_uri";
    fastcgi_cache_valid 200 301 302 60m;
}

Save the modifications and restart the Nginx service to apply the caching rules.

Verifying Cache Status

Open the frontend of the website in an incognito browser window. Access the Developer Tools (F12), navigate to the Network tab, select the document request, and inspect the Response Headers. Look for the X-Cache-Status header.

  • HIT: Content successfully served from the cache.
  • MISS: Cache miss; refresh the page to see if it transitions to HIT.
  • BYPASS: Cache intentionally bypassed.
  • EXPIRED: Cache entry has expired.

Tags: nginx BaoTa WordPress fastcgi_cache Caching

Posted on Wed, 08 Jul 2026 17:45:13 +0000 by djopie