Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In nginx is there a way to conditionally serve a file with different expires headers depending on when the file was last modified? For example, if foo was modified within the last week then I want it to expire in a few hours, otherwise I want it to expire in a few days.

share|improve this question
up vote 0 down vote accepted

Browsers are probably already supposed to have the functionality you describe, so, there's really not that much point implementing it within nginx.

Standard nginx has the expires directive, which lets you use several kinds of static no-comparison offset-style expressions, but not the dynamic ones you describe.

I don't think any of the standard modules that are compiled by default into nginx can do what you request here — dynamic expires, with a varying offset depending on how long ago the file was modified.

However, there is an ngx_http_perl_module, which is an official experimental perl module directly from the makers of nginx, which lets you use the resourceful perl language directly from within the nginx configuration file.

Note that this http_perl_module is not compiled by default for obvious reasons (obviously requires perl etc), and is considered highly experimental — use with care.

share|improve this answer
The Lua module should be built in, and that would work as well. – Michael Hampton Dec 1 '13 at 3:52
Not yet in nginx, but it's already available in the kernel. So, yes, you could probably tie your in-kernel lua-based kernel module in NetBSD with the bpf(4) interface, and look for packets requesting certain files, find their mtime in the vfs(9) layer, and patch the Expires HTTP response header in the outgoing packets as you please. Care to volunteer a bounty for the work needed to implement such a solution? – cnst Dec 1 '13 at 4:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.