Search code examples
httpnginxhttp-cachinglast-modified

How to generate 'last-modified' http header for a resource?


I currently read this about 'last-modified' HTTP header. Though I have read number of sources, I'm still confused how it is generated for a particular resource.

Is it solely depends on the time stamp when the resource has changed in the db. i.e. as PUT requests are processed for the db. on the same resource?

Or are there any generalized or nearly generalized logic to tackle the problem?

And the generation of last-modified time, is it solely a responsibility of the actual application? or are there any other ways?


Solution

  • As I mentioned in my answer about ETags, the responsibility to provide the header always lies with the resource provider.

    And just as with ETags, there may be ways that the web server or framework can automate the creation of the header. By far the most common case is that web servers serving static files can use the files' modification time to set the Last-Modified header.

    It's harder to automate with dynamic applications, since there's generally not a standard source of that information. To take an example I'm familiar with, Django allows you to specify a function that will be called to get the Last-Modified date. It's up to you to put the application-specific logic into that function, but the framework will populate the header and do the comparison to return Not Modified responses.

    My impression is that most dynamic applications don't bother with the Last-Modified header. If you have to provide custom logic to figure out the caching headers, you might as well just provide the ETag, which is generally preferred. Also, with ETags you can often avoid doing any application-specific logic at all if the framework will hash the response for you.