Linux

Nginx fastcgi記憶體在不同的位置塊

  • November 9, 2019

我正在嘗試使用不同的記憶體鍵區記憶體不同的位置,但它不起作用。記憶體僅適用於根“/”位置。

如果我關閉位置“/”的記憶體,那麼它仍然不適用於其他位置。

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;


       }

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

引用自:https://serverfault.com/questions/991177