Expires .. is there a way to set an expires header for all of jpg, gif, css, js?

Hi,

Expires … is there a way to set an expires header for all of jpg, gif,
css, js ?

I have been reading through the docs and have tried a few things such
as:

    location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html)$ {
        access_log        off;
        expires           30d;
    }

This made me think what I am asking must be possible, but the above
makes every
page on my site 404.

        location ~* ^.+\.(jpg|jpeg|gif)$ {
            root         /spool/www;
            access_log   off;
            expires      30d;
        }

This would work, but I don’t have a fixed root for images. I’d like to
do it
throughout an entire site - or would I simply change the root in the
above
example to my document root ?

Thanks

Igor has the plan to add expire by response content-type.
Let’s wait it. :slight_smile:

2008/9/6 David [email protected]:

I think there’s just a simple typo in your regex. Try,

location ~* .(jpg|jpeg|gif|css|png|js|ico|html)$

I’ve been using the expires 30d; and I think it works ok.
Chris :slight_smile:

Thanks for your replies.

Chris I gave that a shot, but am testing the site with Firebug & YSlow
(dunno if
you’ve used them but it’s a Yahoo thing that isolates slowdowns on page
loads
and makes recommendations.

When I use:

    location ~* \.(jpg|jpeg|gif|css|png|js|ico|html)$ {
        access_log        off;
        expires           2d;
    }

The expires header is not working.

This is the regex that i use and for me it worked when testing with
YSlow
location ~* ^.+.(jpg|jpeg|gif|png)$ {
access_log off;
expires 30d;
}

Infact when I use that regex my images 404. It’s weird.

Hi Rob,

Does your regex apply the expires header to images in directories deeper
than
the root dir ? The reason I ask is that that isn’t working for me.

Thanks

On Saturday 06 September 2008, David wrote:

Infact when I use that regex my images 404. It’s weird.

error.log will show why

You’re absolutely right.

When using:

location ~* ^.+.(jpg|jpeg|gif|png)$ {
access_log off;
expires 30d;
}

It ignores my:

    location / {
            index   index.php index.html index.htm;
            root    /home/user/public_html;
    }

and tries to load the image from /usr/local/nginx/html

Hi Rob,

I’ve managed to get this working now:

    Location ~* ^.+\.(jpg|jpeg|gif|png)$ {
        access_log        off;
        expires           30d;
    }

I needed to specify the root or it would default to nginx’s default
docroot.

Your regex works well, but only works for images without underscores,
hyphens,
or anything infact that isn’t just a continuous text string. Would it be
easy
enough to modify in order to cope with this ?

It should work with underscores, hyphen’s etc. as its grabbing all
char’s
from the .+ that should match all char’s that is not a line break.
Also about ignoring root. I set my root in the base of the server
config. IE
server {
listen 80;
server_name domain.org www.domain.org;

            access_log      /var/log/nginx/domain.org.access_log 

main;
error_log /var/log/nginx/domain.org.error_log
info;

            root /var/www/domain.org/htdocs;

            location ~ .*\.php$ {
                    fastcgi_pass   127.0.0.1:9000;
                    fastcgi_index   index.php;
            }

            location ~* ^.+\.(jpg|jpeg|gif|png)$ {
                    access_log   off;
                    expires      30d;
            }
    }

I try to keep everything at the lowest part that i need it. I use the
index
directive in the main http section as most of my sites will use those.
And
if i need custom then i will adjust in each of my server sections. Same
thing with my server sections. i do a root in the toplevel of the server
directive so everything works no matter what i do and then IF i need a
location to go somewhere else i put that inside the location. I think
this
is easier and solves alot of issues of having to remember to put them in
multiple location’s etc etc. Only overwrite it if you need it.

it’s probably due to location parsing order. it can be confusing

this works like a charm:

    server {
            listen 80;
            server_name michaelshadle.com;
            index index.php index.html;
            root /home/mike/web/michaelshadle.com;
             location ~* ^.+\.(jpg|jpeg|gif|css|png|js|ico|html)$ {
                              expires max;
            }
            error_page 404 = /wordpress/index.php?q=$request_uri;
            location ~ \.php$ {
                    fastcgi_pass 127.0.0.1:11000;
            }
    }

you don’t actually -need- a location / either, it seems. unless your
config requires it due to some other stuff. but i don’t have any of
those in mine and it works fine.