Need to add expires header conditionally based on mime types


I need to add expires headers conditionally based on a certain mime
For example add expires 7d to text/css

How to do this ?

So far I found that $sent_http_content_type contains the mime type that
send in http response. But its not possible to put it in a if directive
do expires

if ($sent_http_content_type = “text/css”)
expires 7d;

will not work.

Also tried to create a map and try to hack around, but could not do it.

I cannot do on file based or specific directory location based because
of css/js content is generated from php files dynamically. And there are
multiple php files generating content.

I need a neat way to implement this, is it possible ?

Hi Silver,

this will fit your needs:

location ~* .(js|css|png|jpg|jpeg|gif|ico)$ {
expires 7d;

Am 13.06.2013 14:26, schrieb Silver Moon:


this is not sufficient.

in my application for example, js, css and images are generated
from php scripts. and there are multiple scripts to generate such

therefore i need mime based expire headers.

On Fri, Jun 14, 2013 at 12:23 AM, Simon Hnscheid <

Are they presented as .js, .css etc to the client browser?

If so, then this is sufficient.


On Fri, 2013-06-14 at 08:28 +0530, Silver Moon wrote:

    > }
    > there are multiple php files generating content.
    > [email protected]


nginx mailing list
[email protected]
nginx Info Page

Steve H. BSc(Hons) MNZCS

Skype: sholdowa

yes, i could send out headers from php itself
but mime based header generation in the webserver itself would make

so finally it is impossible to achieve that ?

From what you describe I do think the suggested nginx config is

However maybe you’re looking for something like this:

Not really sure if it suits you’re needs, but It seems this is what
you’re looking for.

You can also find more info on mime type here:

Hope this helps,


If you handle your js, css and images by php scripts you can use header
function to send out Expires and Cache-Control directives


<?php header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1 header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past ?>


putting $upstream_http_content_type or $sent_http_content_type or
$content_type inside “if” does not work.
i wish someone could test and provide a working example.

none of the samples shown in the stackoverflow post work.

i tested enough and found that only $sent_http_content_type contains
http response Content-type
and that can be tested like this

add_header TEST $sent_http_content_type

however putting that inside an if and trying to add further headers
that if will not work
i opened a ticket in trac and they said that it does not work like that

however no one confirms if it is strictly possible or impossible.

If you use the Lua embedded module you can do that AFAICT.

Le 14 juin 2013 05:42, “Silver Moon” [email protected] a crit :