Nginx

nginx fastcgi記憶體 - 多個不同的過期時間

  • March 7, 2020

我需要設置 NGINX,以便不同的位置有不同的記憶體過期時間,有些會禁用記憶體。這是我目前的設置:

fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=CACHE:200m inactive=100m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
server {
   ...
   set $no_cache 1;
   ...
   if ($request_uri ~* "/some/cached/route") {
       set $no_cache 0;
   }
   ...
   location ~ \.php$ {
       fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
       fastcgi_cache CACHE;
       fastcgi_cache_valid 200 30m;       
       fastcgi_cache_bypass $no_cache;
       fastcgi_no_cache $no_cache;
       fastcgi_split_path_info ^(.+\.php)(/.+)$;
       fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
       fastcgi_index index.php;
       include fastcgi_params;
   }
   ...
}

使用此設置,我可以控制何時記憶體響應,但無法控制記憶體的有效時間。

如何使記憶體有效時間變數?

最近我又遇到了這個限制。

為了更清楚地重申這個問題:在不同的時間長度內記憶體多個不同的 PHP 響應。

我想出了這個技巧:

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

location ~ /resource/.* {
   try_files $uri $uri/ //index.php?$query_string;
}

location ~ /longresource/.* {
   try_files $uri $uri/ ///index.php?$query_string;
}

location ~ //index\.php$ {
   fastcgi_cache resources_cache;
   fastcgi_cache_valid 200 60m;
   fastcgi_cache_methods GET;

   include snippets/fastcgi-php.conf;
   fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

location ~ ///index\.php$ {
   fastcgi_cache resources_cache;
   fastcgi_cache_valid 200 180m;
   fastcgi_cache_methods GET;

   include snippets/fastcgi-php.conf;
   fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

location ~ \.php$ {
   include snippets/fastcgi-php.conf;
   fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}

此程式碼片段來自一個 laravel 項目,其中所有內容都通過 index.php 文件。

之所以有效,是因為//index.phpand /index.php(以及帶有任意數量的前斜杠的相同位置)解析為相同的文件,但解析為不同的 nginx 位置塊。

這是一個非常糟糕的 hack生產測試,而且根本不是DRY,但它可以工作,你可以避免使用if.

原始回复

使用不同的記憶體

fastcgi_cache_path /dev/shm/c1 levels=1:2 keys_zone=C1:50m inactive=1440m;
fastcgi_cache_path /dev/shm/c2 levels=1:2 keys_zone=C2:50m inactive=500m;
fastcgi_cache_path /dev/shm/c3 levels=1:2 keys_zone=C2:50m inactive=10m;

# absolute path
location = /path/to/resource { 
 fastcgi_cache C1; # etc
}

# regular expression
location ~* wp-admin { 
 fastcgi_cache C2; # etc
}

# catch-all
location ~ \.php$ {
 fastcgi_cache C3; # etc
}

請注意,您必須了解Nginx 處理請求的順序才能做到這一點,並且您可能必須使用正則表達式匹配 - 我發現這個正則表達式網站對測試非常有用。不要使用“如果”。如果是邪惡的

更新。這個想法來自我在我的網站上執行的 Nginx 配置文件。你可以在我的nginx 教程第一部分下載我的配置。

更新 1

根據要求,這是一個更完整的配置。這是從上面頁面上的連結中刪除的,因為它很長。這只顯示了主要的 PHP 塊,而不是我做的其他事情,例如 wp-admin、靜態圖像熱連結保護以及從 http 到 https 的轉發等

# Caching. Putting the cache into /dev/shm keeps it in RAM, limited to 10MB, for one day.
# You can move to disk if you like, or extend the caching time
fastcgi_cache_path /dev/shm/hr_nginxcache levels=1:2 keys_zone=HR_CACHE:50m inactive=1440m; #RAM

upstream php {
  server 127.0.0.1:9001;
}


# http production headphone reviews server
server {
 server_name www.example.com;
 listen 443 ssl http2;

 ssl_certificate /var/lib/acme/certs/***CERT_DIRECTORY/fullchain;
 ssl_certificate_key /var/lib/acme/certs/***CERT_DIRECTORY/privkey;

 # Set up preferred protocols and ciphers. TLS1.2 is required for HTTP/2
 ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
 ssl_prefer_server_ciphers on;
 ssl_ciphers ECDH+AESGCM:ECDH+AES256:ECDH+AES128:DH+3DES:!ADH:!AECDH:!MD5;

 root /var/www/***folder;

 # First line is a cached access log, second logs immediately
 access_log  /var/log/nginx/hr.access.log main buffer=128k flush=60 if=$log_ua;

 # Rules to work out when cache should/shouldn't be used
 set $skip_cache 0;

 # POST requests and urls with a query string should always go to PHP
 if ($request_method = POST) {
     set $skip_cache 1;
 }   
 if ($query_string != "") {
   set $skip_cache 1;
 }   
 # Don't cache uris containing the following segments. 'admin' is for one of my websites, it's not required
 # for everyone. I've removed index.php as I want pages cached.
 #if ($request_uri ~* "/wp-admin/|/admin-*|/purge*|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
 if ($request_uri ~* "/wp-admin/|/admin-*|/purge*|/xmlrpc.php|wp-.*.php|/feed/|sitemap(_index)?.xml") {
   set $skip_cache 1;
 }   
 # Don't use the cache for logged in users or recent commenters
 #  if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in|code|PHPSESSID") {
 if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wordpress_logged_in|code") {
   set $skip_cache 1;
 }

 # If we skip the cache it's likely customised for one user. Set the caching headers to match.
 # http://www.mobify.com/blog/beginners-guide-to-http-cache-headers/
 if ($skip_cache = 1) {
   set $cacheControl "private, max-age=0, s-maxage=0, no-cache, no-store";
 }
 if ($skip_cache = 0) {
   set $cacheControl "public, max-age=86400, s-maxage=86400";
 }

 # Default location to serve
 location / {
   # If the file can't be found try adding a slash on the end - it might be
   # a directory the client is looking for. Then try the Wordpress blog URL
   # this might send a few requests to PHP that don't need to go that way
   try_files $uri $uri/ /blog/index.php?$args;
   more_clear_headers Server; more_clear_headers "Pragma"; more_clear_headers "Expires";
   # add_header Z_LOCATION "hr_root"; add_header URI $uri; # DEBUG
 }

 # Send HipHop and PHP requests to HHVM
 location ~ \.(hh|php)$ {
   fastcgi_keep_conn on;
   fastcgi_intercept_errors on;
   fastcgi_pass   php;
   include        fastcgi_params;
   fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;

   # Use the cache defined above. Cache 200 (success) status's, for 24 hours, and cache
   # specific other status's for an hour. This helps mitigate DDOS attacks.
   # Only cache GET and HEAD requests
   fastcgi_cache HR_CACHE;
   fastcgi_cache_valid 200 1440m;
   fastcgi_cache_valid 403 404 405 410 414 301 302 307 60m;
   add_header X-Cache $upstream_cache_status;

   fastcgi_cache_methods GET HEAD; 
   fastcgi_cache_bypass $skip_cache;
   fastcgi_no_cache $skip_cache;

   # Set the cache control headers we prepared earlier. Remove the old unnecessary Pragma and hide
   # the server version. Clearing existing headers seems necessary
   more_clear_headers "Cache-Control";
   add_header Cache-Control $cacheControl;
   more_clear_headers "Pragma"; more_clear_headers Server; more_clear_headers "Expires";

 }
}

更新2,將以上兩個部分放在一起

fastcgi_cache_path /dev/shm/c1 levels=1:2 keys_zone=C1:50m inactive=1440m;
fastcgi_cache_path /dev/shm/c2 levels=1:2 keys_zone=C2:50m inactive=500m;
fastcgi_cache_path /dev/shm/c3 levels=1:2 keys_zone=C2:50m inactive=10m;

# regular expression for directory app1
location ~* ^\/.*app1\/.*.php$ { 
 fastcgi_cache C1; # etc
}

# regular expression for directory app2
location ~ ^\/.*app2\/.*.php$ {
 fastcgi_cache C2; # etc
}

# regular expression for directory app3
location ~ ^\/.*app3\/.*.php$ {
 fastcgi_cache C3; # etc
}

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