1

I'm using nginx as a reverse proxy and for caching. Currently the cache for location "/test" will be renewed every 2 hours: "proxy_cache_valid 200 302 301 304 2h;"

Is it possible to only renew cache if a file has changed?

Thank you!

varlog
  • 23
  • 2
  • 6

2 Answers2

2

This should be handled generically for you.

If a file is cached in a browser, and the browser requests that file again, and the file has been purged from the server cache, the server will still look at the if-modified-since header in the request.

The server will check that datetime value, and if it determines no change as occurred, the server will send a 304 response, which tells the browser it's OK to load the cached version, and the actual object is not server and downloaded again.

If the value is change, the object will be server and the cache copy updated.

https://www.keycdn.com/support/if-modified-since-http-header

IMHO, caching is something you should really controlwith Cache-Control headers, rather than server configuration.

The server configuration options are there as a failsafe for when Cache-Control doesn't exist or is illogically configured.

Garreth McDaid
  • 3,399
  • 26
  • 41
  • Thank you, but I prefare caching on server-side - and not at browser-level. – varlog Jan 20 '20 at 13:51
  • You can't prevent browser-caching unless you configure your application to send a very specific set of `Cache-Control` directives that override the default configuration that exists in all standard browsers to improve user experience. Server side caching exists to control what happens when objects cached by your browser go stale. They don't exist to provide "server or browser" menu of options. – Garreth McDaid Jan 20 '20 at 15:02
0

I assume that we are speaking about generated content that is not already a static file on your web server (Static files on your web server are usually fast enough).

The main question is how you let Nginx know that cached content has been changed.

Revalidation sounds good but has the impact, that mostly the process of checking for updates is as expensive as generating it. So thats no real option.

Best practice is to Purge content in case it has been updated (e.g. has been changed in your CMS). Purge is the process to invalidate cached objects. Nginx supports Purge only in the NginxPlus version. However you can do almost the same by reloading the cached object.

Here is a simple how-to:

Nginx config

...

# default location
location / {
    try_files                   $uri @php;
}

# PHP handler
location @php {
    try_files                   $uri /index.php;

    access_log                  /var/log/nginx/access.log;

    # include fastcgi_params
    include                     fastcgi_params;

    # how to connect to PHP-FPM
    fastcgi_pass                php-fpm;

    # enable caching
    fastcgi_cache               phpfpm;

    # don't look for existing cache objects with HTTP header X-Purge
    fastcgi_cache_bypass        $http_x_purge;
}

...

Curl to purge one document

curl https://example.com/site1/overview.jpeg -s -H "X-Purge: true"

Jens Bradler
  • 6,133
  • 2
  • 16
  • 13