Nginx fastcgi 缓存在不同位置块中

Nginx fastcgi 缓存在不同位置块中

我尝试使用不同的缓存键区域来缓存不同的位置,但不起作用。缓存仅适用于根“/”位置。

如果我关闭位置“/”的缓存,那么它仍然不适用于其他位置。

Http 块:

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";

服务器块:

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;


        }

答案1

URI/index.php由最后一个块处理location,因此使用microcache区域。

URI/2000/首先被重写为/index.php最后一个块,然后由其处理location,因此使用microcache区域。

要使用备用区域,您可以避免重写 URI,而是将 fastcgi_param SCRIPT_FILENAME 变量硬连线到$document_root/index.php,因为无论如何,所有与该模式匹配的 URI 都会被发送到index.php

例如:

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;
    ...
}

相关内容