Nginx cashe validation is not working properly?

June 3, 2016 216 views

I have added cache to Nginx. But changed static files (css,images) are not updated untill I press F5. That is wrong
Maybe you can advice me what to do.

Here is part of server config

server {
listen 80;
etag on;
if_modified_since exact;
location ~* ^.+\.(jpg|jpeg|gif|css|png|js|ico|pdf)$
root /storage/www;
expires 30d;
add_header Cache-Control 'public';

fastcgi_pass unix://var/run/php-fpm/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /storage/www$fastcgi_script_name;
fastcgi_pass_header Last-Modified;
include fastcgi_params;
fastcgi_read_timeout 240s;
fastcgi_send_timeout 240s;
fastcgi_intercept_errors on;

Here is header of CSS
<^>HTTP/1.1 200 OK
Server: nginx/1.6.2
Date: Fri, 03 Jun 2016 22:46:02 GMT
Content-Type: text/css
Content-Length: 16150
Last-Modified: Fri, 03 Jun 2016 22:06:24 GMT
Connection: close
ETag: "5751ff60-3f16"
Expires: Sun, 03 Jul 2016 22:46:02 GMT
Cache-Control: max-age=2592000
Cache-Control: public
Accept-Ranges: bytes<^>

1 Answer

Hi @staszhilnikov,

What you're describing is the expected behavior because of the way that the cache module works. When a file is sent from the web server it's given the header (as shown in your update) that the file is valid for 30 days. The easiest solution is to set a shorter window, or to not use the cache at all. That said, if you require the cache for performance reasons you'll probably have to balance between cache duration, and update time (perhaps a day? or 12 hours?). Please keep in mind that some large networks use 'edge cache' services which would mean that even if a specific client hasn't accessed your site directly, their network may have, and the cache may be in effect.

Have another answer? Share your knowledge.