Setting expires header to multiple locations

Igor Sysoev igor at sysoev.ru
Fri Dec 14 12:37:23 UTC 2012


On Dec 14, 2012, at 15:59 , Antonio P.P. Almeida wrote:

>> Currently I need to use tricks since I have multiple locations in my site
>> config.
>> 
>> e.g.
>> 
>> =====================================
>> 
>> 
>> location ~* /a {
>>    location ~* ^/.*\.(?:css|js|jpg|jpeg|gif|png)$ {
>>        expires 1y;
>>    }
>> }
>> 
>> location ~* /b {
>>    location ~* ^/.*\.(?:css|js|jpg|jpeg|gif|png)$ {
>>        expires 1y;
>>    }
>> }
>> 
>> location ~* /c {
>>    location ~* ^/.*\.(?:css|js|jpg|jpeg|gif|png)$ {
>>        expires 1y;
>>    }
>> }
> 
> Going against Igor, Maxim, Valentin and Ruslan in order to be more DRY you
> could use a regex based location (which has its own quirks):
> 
> location ~* ^/(?:a|b|c)/.*\.(?:css|gif|js|jpe?g|png)$ {
>    expires 1y;
> }

This valid only if "~* /b" was intended for "~* ^/b".

As to me, I prefer to isolate regex locations (if I have to use them at all)
inside usual locations:

location /c {
   location ~* \.(?:css|js|jpg|jpeg|gif|png)$ {
       expires 1y;
   }
}

Of course this requires more time to type, but allows me to spend much
less time when I need to modify configuration in future.


--
Igor Sysoev
http://nginx.com/support.html



More information about the nginx mailing list