简体   繁体   中英

Nginx FastCGI Cache Key Not working when using original URL

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM