1

I am trying to cache different location with different cache keyzone but it is not working. Cache is only working for the root "/" location.

If I turn off the cache for location "/" then still it does not work for other location.

Http block:

fastcgi_cache_path /webcache/nginx levels=1:2 keys_zone=microcache:100m max_size=1000m inactive=45m use_temp_path=off;
fastcgi_cache_path /webcache/extreme levels=1:2 keys_zone=extreme:100m max_size=10400m inactive=99999m use_temp_path=off;
fastcgi_cache_key "$scheme$request_method$host$request_uri";

Server block:

location ~* "^/(20[0-1][0-8]/)" {              
   try_files $uri $uri/ /index.php?$args;
    location ~ \.php$ {
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/opt/remi/php56/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        include /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors on;
        fastcgi_cache_key "$scheme$request_method$host$request_uri";
        fastcgi_no_cache $no_cache;
        fastcgi_cache_bypass $no_cache;
        fastcgi_cache extreme;
        fastcgi_cache_min_uses 1;
        fastcgi_cache_methods GET HEAD;
        fastcgi_cache_lock on;
        fastcgi_cache_lock_age 5s;
        fastcgi_cache_lock_timeout 5s;
        fastcgi_cache_valid 200 302 301 8760h;
        fastcgi_cache_valid 500 502 10s;
        fastcgi_cache_valid 403 404 10s;
        fastcgi_cache_use_stale updating error timeout invalid_header http_500;
        fastcgi_cache_background_update on;
        fastcgi_pass_header Set-Cookie;
        fastcgi_pass_header Cookie;

}

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


     location ~ \.php$ {
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/opt/remi/php56/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        include /etc/nginx/fastcgi_params;
        fastcgi_intercept_errors on;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_no_cache $no_cache;
    fastcgi_cache_bypass $no_cache;
    fastcgi_cache microcache;
    fastcgi_cache_min_uses 1;
    fastcgi_cache_methods GET HEAD;
    fastcgi_cache_lock on;
    fastcgi_cache_lock_age 5s;
    fastcgi_cache_lock_timeout 5s;
    fastcgi_cache_valid 200 302 301 1m;
    fastcgi_cache_valid 500 502 10s;
    fastcgi_cache_valid 403 404 10s;
    fastcgi_cache_use_stale updating error timeout invalid_header http_500;
    fastcgi_cache_background_update on;
    fastcgi_pass_header Set-Cookie;
    fastcgi_pass_header Cookie;
    fastcgi_ignore_headers Cache-Control Expires Set-Cookie;


        }
  • Do all of these /2xxx/... URIs get handled by /index.php? – Richard Smith Nov 9 at 7:45
  • Sorry for late response. Yes everything is handled by index.php – Ted Khi Nov 9 at 10:31
  • Is there any way supported by nginx to have different cache zone for different locations? I want this to work so much but I failed to find a solution. For /2xxxx/ I tried adding double slash in index.php "try_files $uri $uri/ //index.php?$args;" and then created another location for .php like this "location ~ //index\.php$ {" but then page does not load. – Ted Khi Nov 9 at 10:38
0

The URI /index.php is processed by the last location block, so uses the microcache zone.

The URI /2000/ is first rewritten to /index.php and then processed by the last location block, so uses the microcache zone.

To use an alternative zone, you could avoid rewriting the URI, and instead hardwire the fastcgi_param SCRIPT_FILENAME variable to $document_root/index.php, as all URIs matching that pattern are sent to index.php anyway.

For example:

location ~* ^/20[0-1][0-8]/ {
    fastcgi_pass unix:/var/opt/remi/php56/run/php-fpm/php-fpm.sock;

    include /etc/nginx/fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root/index.php;
    ...
    fastcgi_cache extreme;
    ...
}
location / {
    try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
    try_files $uri = 404;
    fastcgi_pass unix:/var/opt/remi/php56/run/php-fpm/php-fpm.sock;

    include /etc/nginx/fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    ...
    fastcgi_cache microcache;
    ...
}
  • Thanks Richard. I am really sorry again, I am away from computer now. I will try this and will let you know. – Ted Khi Nov 9 at 11:57
  • Hey it works =D I had to remove quotes "^/20[0-1][0-8]/" and then it started working. Strange but it works now. – Ted Khi Nov 10 at 11:47
  • hey Richard, sorry for asking one more question. As I can see my page is showing X-Cache HIT status. But still all my images, CSS, JS on that page is still being requested from backend. How can I cache whole page with all its content? – Ted Khi Nov 10 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.