Nginx FastCGI Page Cache not working

My issue: I have configured my VPS as instructed by the tutorial at Install Nginx, PHP 8.4, WP-CLI, MySQL for WordPress on Ubuntu 24.04 . However, FastCGI Page Cache seems to not be working.

How I encountered the problem: When viewing page headers in my Google Chrome, the Fastcgi-Cache page header was not showing.

Solutions I’ve tried: I’ve tried moving configuration directives between the sites-available file and nginx.conf.

Version of NGINX or NGINX adjacent software (e.g. NGINX Gateway Fabric): 1.28.1

Deployment environment: Ubuntu 24.04 LTS

Nginx.conf:

user itwithme-ictmee;
worker_processes auto;
pid /run/nginx.pid;
error_log /var/log/nginx/error.log;
include /etc/nginx/modules-enabled/*.conf;

events {
	worker_connections 1024;
	multi_accept on;
}

http {

	##
	# Basic Settings
	##
	
	keepalive_timeout 15;
	sendfile on;
	tcp_nopush on;
	types_hash_max_size 2048;
	server_tokens off;
	client_max_body_size 64m;

	# server_names_hash_bucket_size 64;
	# server_name_in_redirect off;

	include /etc/nginx/mime.types;
	default_type application/octet-stream;

	##
	# SSL Settings
	##

	ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
	ssl_prefer_server_ciphers on;

	##
	# Logging Settings
	##

	access_log /var/log/nginx/access.log;

	##
	# Gzip Settings
	##

	gzip on;

	# gzip_vary on;
	gzip_proxied any;
	gzip_comp_level 5;
	# gzip_buffers 16 8k;
	# gzip_http_version 1.1;
	gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # Cache Settings
        ##

        fastcgi_cache_key "$scheme$request_method$http_host$request_uri";
        add_header Fastcgi-Cache $upstream_cache_status;

	##
	# Brotli Settings
	##

	brotli on;
	brotli_comp_level 5;
	brotli_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

	##
	# Virtual Host Configs
	##

	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;

	server {
    listen 80 default_server;
    listen [::]:80 default_server;
	listen 443 ssl default_server deferred;
	listen [::]:443 ssl default_server deferred;
	listen 443 quic reuseport default_server;
	listen [::]:443 quic reuseport default_server;

    server_name _;

	ssl_certificate /etc/nginx/ssl/default/cert.pem;
	ssl_certificate_key /etc/nginx/ssl/default/privkey.pem;

    return 444;
	}

}

Sites-available file:

fastcgi_cache_path /home/itwithme-ictmee/test.josvlaar.com/cache levels=1:2 keys_zone=test.josvlaar.com:100m inactive=60m;

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    listen 443 quic;
    listen [::]:443 quic;
    http2 on;

    server_name test.josvlaar.com;

    ssl_certificate /etc/letsencrypt/live/test.josvlaar.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/test.josvlaar.com/privkey.pem;

    access_log /home/itwithme-ictmee/test.josvlaar.com/logs/access.log;
    error_log /home/itwithme-ictmee/test.josvlaar.com/logs/error.log;

    root /home/itwithme-ictmee/test.josvlaar.com/public/;
    index index.php;

    set $skip_cache 0;

    # POST requests should always go to PHP
    if ($request_method = POST) {
        set $skip_cache 1;
    }

    # URLs containing query strings should always go to PHP
    if ($query_string != "") {
        set $skip_cache 1;
    }

    # Don't cache uris containing the following segments
    if ($request_uri ~* "/wp-admin/|/wp-json/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml|/cart/|/checkout/|/my-account/") {
        set $skip_cache 1;
    }

    # Don't use the cache for logged in users or recent commenters
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in|woocommerce_items_in_cart") {
        set $skip_cache 1;
    }

    add_header Alt-Svc 'h3=":443"; ma=86400';

    location / {
        try_files $uri $uri/ /index.php?$args;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php8.4-fpm.sock;
        fastcgi_index index.php;
        include fastcgi.conf;

        fastcgi_cache test.josvlaar.com;
        fastcgi_cache_bypass $skip_cache;
        fastcgi_no_cache $skip_cache;
        fastcgi_cache_valid 60m;
    }
}

server {
    listen 80;
    listen [::]:80;

    server_name test.josvlaar.com;

    return 301 https://test.josvlaar.com$request_uri;
}

I have solved the issue by moving add_header Fastcgi-Cache $upstream_cache_status; from nginx.conf to the sites-available file (just above the first add_header Alt-Svc ‘h3=“:443”; ma=86400’; line).

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.