Score:0

Nginx FastCGI cache is EXPIRED when it should not

cn flag

Nginx sets the x-fastcgi-cache header to EXPIRED a few hours after the page was first cached, whereas the cache validity is 1 week.

Nginx Config:

fastcgi_cache_path /usr/share/nginx/fastcgi_cache levels=1:2 keys_zone=phpcache:500m max_size=30g inactive=1w use_temp_path=off;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
fastcgi_cache_valid 1w;

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

...

    set $skip_cache 0;
    set $bypass_reason "NONE";
    set $woocommerce "OK";
 
    # POST requests and URLs with a query string should always go to PHP
    if ($request_method = POST) {
        set $skip_cache 0;
        set $bypass_reason "REQUEST_METHOD";
    }
    
    if ($request_uri ~* ("/wp-admin.*|/cart.*|/panier.*|/commander.*|/checkout.*|/account.*|/myaccount.*|/addond.*|/store.*|/shop.*|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-ocations.php|sitemap(_index)?.xml|a-z0-9_-]+-sitemap([0-9]+)?.xml)") { 
        set $skip_cache 1;
        set $bypass_reason "REQUEST_URI_1";
    }
 
    if ( $cookie_yith_ywraq_items_in_raq != "0" ) {
        set $woocommerce "NOK";
    }
    if ( $cookie_yith_ywraq_items_in_raq = "" ) {
        set $woocommerce "OK";
    }
    if ( $woocommerce = "NOK" ) {
        set $skip_cache 1;
        set $bypass_reason "WOOCOMMERCE";
    }

    if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
        set $skip_cache 1;
        set $bypass_reason "REQUEST_URI_2";
    }   
 
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in|panier|commander") {
        set $skip_cache 1;
        set $bypass_reason "HTTP_COOKIE";
    }

    location ~ \.php$ {
        fastcgi_cache_bypass $skip_cache;
        fastcgi_no_cache $skip_cache;
        fastcgi_cache phpcache;
        fastcgi_cache_valid 200 301 302 60m;
        fastcgi_cache_use_stale error timeout updating invalid_header http_500 http_503;
        fastcgi_cache_min_uses 1;
        fastcgi_cache_lock on;
        add_header X-FastCGI-Cache $upstream_cache_status;
        add_header X-FastCGI-Cache-Bypass-Reason $bypass_reason;

        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }
...

I don't understand why even with inactive=1w and fastcgi_cache_valid 1w I have an expired cache status.

Any ideas on what I am missing here?

Thanks

Forever Cuber avatar
cd flag
I may be mistaken, but it looks like you are setting the default expiration time to 1 week, but then setting it again to only 60 minutes for all php requests using cache: phpcache. You may be trying to enforce some other expiration with this second line set at 60m, but I believe that may be the source of your issue.
Fluktegrute avatar
cn flag
@Csnap, you're right! I needed the `fastcgi_cache_valid` in the `location ~ \.php$` block at some point but it is useless now. It probably is the source of the issue. I think you may post your comment as an answer. Thanks for pointing it out!
Score:0
cd flag

I may be mistaken, but it looks like you are setting the default expiration time to 1 week, but then setting it again to only 60 minutes for all php requests using cache: phpcache.

You may be trying to enforce some other expiration with this second line set at 60m, but I believe that may be the source of your issue.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.