Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I've got something like the following:

server {
    server_name my.com;

    location /media/ {
       alias /home/my/media/;
       expires 1h;
    }

    location / {
        include fastcgi_params;
    }
}

I'd like to add a second /media/ location block that catches just css and js files and sets expires to max.

Ideally I'd like to also process the existing /media/ block. i.e. I just want to override expires but also process any directives in the main block.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Using a nested location will allow you to do what you want: Also, when the location matches the suffix of your alias directive, it's more efficient to just use root:

location /media/ {
  root /home/my;
  expires 1h;

  location ~ \.(css|js)$ {
    expires max;
  }
}

If the real config doesn't have the matching /media/, you can change that back to an alias. This approach also has the advantage of not trying to match the regex for every request, only for those that start with /media/.

share|improve this answer
location ~ ^/media/.+\.(css|js)$ {
    expires max;
}

Please note that you cannot make it parse two locations at the same time. Nginx will only ever use one location. Even if you rewrite a request it will apply the new location and disregard the old one.

share|improve this answer
2  
You need to anchor this location using ^ and $ –  kolbyjack May 14 '11 at 11:40

Your Answer

 
discard

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.