2015-09-09 27 views
7

我運行nginx服務器+ PHP webservices API。我使用nginx的fastcgi_cache來緩存所有的GET請求,當某些資源更新時,我清除一個或多個相關的緩存資源。清除nginx緩存文件並不總是有效

我正在使用的方法是爲我要清除的每個資源計算nginx緩存文件名,然後刪除該文件。大多數情況下,這很有效。

但是,我發現有時,即使刪除緩存文件後,nginx仍會從緩存中返回數據。

這不是選擇正確的緩存文件,刪除了一個問題 - 我的測試的一部分,我已經刪除了整個緩存目錄,nginx的仍返回HIT響應

是任何人都知道的爲什麼這可能會發生?是否有可能涉及另一個緩存?例如,操作系統是否將緩存文件的緩存版本返回給nginx,是否nginx不知道它已被刪除?

我在CentOS運行此,以及與此nginx的配置(減去不相關部分):

user nginx; 

# Let nginx figure out the best value 
worker_processes auto; 

events { 
    worker_connections 10240; 
    multi_accept  on; 
    use     epoll; 
} 

# Maximum number of open files should be at least worker_connections * 2 
worker_rlimit_nofile 40960; 

# Enable regex JIT compiler 
pcre_jit on; 

http { 

    # TCP optimisation 
    sendfile  on; 
    tcp_nodelay  on; 
    tcp_nopush  on; 

    # Configure keep alive 
    keepalive_requests 1000; 
    keepalive_timeout 120s 120s; 

    # Configure SPDY 
    spdy_headers_comp 2; 

    # Configure global PHP cache 
    fastcgi_cache_path /var/nginx/cache levels=1:2 keys_zone=xxx:100m inactive=24h; 

    # Enable open file caching 
    open_file_cache max=10000 inactive=120s; 
    open_file_cache_valid 120s; 
    open_file_cache_min_uses 5; 
    open_file_cache_errors off; 

    server { 
     server_name xxx; 
     listen 8080; 

     # Send all dynamic content requests to the main app handler 
     if (!-f $document_root$uri) { 

      rewrite ^/(.+) /index.php/$1   last; 
      rewrite ^/  /index.php   last; 
     } 

     # Proxy PHP requests to php-fpm 
     location ~ [^/]\.php(/|$) { 

      # Enable caching 
      fastcgi_cache xxx; 

      # Only cache GET and HEAD responses 
      fastcgi_cache_methods GET HEAD; 

      # Caching is off by default, an can only be enabled using Cache-Control response headers 
      fastcgi_cache_valid 0; 

      # Allow only one identical request to be forwarded (others will get a stale response) 
      fastcgi_cache_lock on; 

      # Define conditions for which stale content will be returned 
      fastcgi_cache_use_stale error timeout invalid_header updating http_500 http_503; 

      # Define cache key to uniquely identify cached objects 
      fastcgi_cache_key "$scheme$request_method$host$request_uri"; 

      # Add a header to response to indicate cache results 
      add_header X-Cache $upstream_cache_status; 

      # Configure standard server parameters 
      fastcgi_split_path_info ^(.+\.php)(/.+)$; 
      include fastcgi_params; 

      # php-fpm config 
      fastcgi_param SCRIPT_URL  $fastcgi_path_info; 
      fastcgi_param PATH_INFO   $fastcgi_path_info; 
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; 
      fastcgi_param REQUEST_SCHEME $scheme; 
      fastcgi_param REMOTE_USER  $remote_user; 

      # Read buffer sizes 
      fastcgi_buffer_size 128k; 
      fastcgi_buffers 256 16k; 
      fastcgi_busy_buffers_size 256k; 
      fastcgi_temp_file_write_size 256k; 

      # Keep connection open to enable keep-alive 
      fastcgi_keep_conn on; 

      # Proxy to PHP 
      fastcgi_pass unix:/var/run/php-fpm/fpm.sock; 
     } 
    } 
} 

現在我看這個,可以在open_file_cache參數是影響緩存文件?

任何想法?

+0

來想起來了,這其實是一個非常好的問題。:-) – cnst

回答

6

不,操作系統不緩存文件。

但是,這可能發生的原因是文件實際上並未完全刪除,直到鏈接數和打開文件的進程數都降爲零。

unlink(2) manual page,哪個文件使用的工具,如rmsystem call,內容如下:

取消鏈接()函數刪除由它的目錄路徑命名的鏈接和遞減的鏈接數文件被鏈接引用。如果該減量將文件的鏈接數減少到零,並且沒有進程打開文件,則與該文件關聯的所有資源都將被收回。如果一個或多個進程在最後一個鏈接被刪除時打開該文件,鏈接將被刪除,但文件的刪除將被延遲,直到所有對該文件的引用都被關閉。

根據系統的不同,您實際上仍可以完全恢復這些打開的文件,而不會丟失任何數據,例如請參見https://unix.stackexchange.com/questions/61820/how-can-i-access-a-deleted-open-file-on-linux-output-of-a-running-crontab-task

因此,實際上,open_file_cache將有效地阻止您的刪除在其高速緩存中仍具有相關文件描述符的進程中發揮任何作用。如果刪除後的緊急清除對您來說非常重要,您可能需要使用較短的open_file_cache_valid

0

確保您的瀏覽器沒有緩存頁面。嘗試在瀏覽器控制檯下選擇禁用緩存選項,並在測試服務器緩存時保持控制檯處於打開狀態

enter image description here