0

I have question about Nginx Cache

This my Nginx Config

fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=fpmcache:100m max_size=70g inactive=3d use_temp_path=off;

server {
        listen 80;
        server_name example.net;
        root /var/www/mydomain;
        index index.php;


        

        set $original_path $request_uri;

        if ($request_uri ~ "^([^?]*)(\?.*)?$") {
                set $original_path $1;
        }


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

        location ~* \.(css|js|gif|jpeg|jpg|png|ico)$ {
                expires max;
                log_not_found off;
                access_log off;
                add_header Pragma public;
                add_header Cache-Control "public, must-revalidate, proxy-revalidate";
        }

       

        location ~ \.php$ {

                fastcgi_hide_header "Set-Cookie";
                add_header X-Cache $upstream_cache_status-$ua_device;
                fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
                fastcgi_cache_key "$original_path";
                fastcgi_cache fpmcache;
                fastcgi_cache_valid 200 3d;
                fastcgi_cache_min_uses 1;
                fastcgi_cache_lock on;


                fastcgi_pass   unix:/var/run/php/php7.2-fpm.sock;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include        fastcgi_params;
        }

}

I try hit my url http://example.net/hello-world

When nginx process the url with this config,cache key is "/hello-world"

set $original_path $request_uri;

        if ($request_uri ~ "^([^?]*)(\?.*)?$") {
                set $original_path $1;
        }

Using ISP 1

Firt Try X-Cache is MISS Second Try X-Cache is HIT

Using ISP 2

First Try Is MISS

The question why when i hit using ISP 2 the cache is MISS ..? whereas previously using ISP 1 X-Cache was a hit.

I try debug $original_path variable to access.log is print /hello-world

1
  • What are your "ISPs"? Different servers? Commented Feb 3, 2022 at 14:10

1 Answer 1

0

try using cache-key like this:

fastcgi_cache_key $request_uri;

this might fix a wired issue with setting up your cache-key.

also, you didn't mention the Nginx version (it might be a good idea to update to the latest version)

I'm wondering why are you using regex to make $original_path?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.