# Setting expires header to multiple locations

**URL:** <https://www.ruby-forum.com/t/setting-expires-header-to-multiple-locations/226544>\
**Category:** NGINX\
**Created:** [December 14, 2012, 12:32pm UTC](https://www.ruby-forum.com/t/setting-expires-header-to-multiple-locations/226544 "2012-12-14T12:32:06Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![howard\_chen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bbce88/32.png) [@howard\_chen](https://www.ruby-forum.com/u/howard_chen)\
**Post date:** [December 14, 2012, 12:32pm UTC](https://www.ruby-forum.com/t/setting-expires-header-to-multiple-locations/226544/1 "2012-12-14T12:32:06Z")

</div>

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;  
}  
}

=====================================

Are there any better way to write it?

Thanks.

---

<div class="post-metadata">

**Author:** ![howard\_chen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bbce88/32.png) [@howard\_chen](https://www.ruby-forum.com/u/howard_chen)\
**Post date:** [December 14, 2012, 12:39pm UTC](https://www.ruby-forum.com/t/setting-expires-header-to-multiple-locations/226544/2 "2012-12-14T12:39:42Z")

</div>

On Fri, Dec 14, 2012 at 6:31 PM, howard chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> expires 1y;
> location ~* ^/.*\.(?:css|js|jpg|jpeg|gif|png)$ {
> expires 1y;
> }
> 
> ```
> 
> }
> 
> =====================================
> 
> Are there any better way to write it?

put in separate file and include it in each blocks?

---

<div class="post-metadata">

**Author:** ![howard\_chen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bbce88/32.png) [@howard\_chen](https://www.ruby-forum.com/u/howard_chen)\
**Post date:** [December 14, 2012, 1:00pm UTC](https://www.ruby-forum.com/t/setting-expires-header-to-multiple-locations/226544/3 "2012-12-14T13:00:44Z")

</div>

> ```
> expires 1y;
> 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;  
}

–appa

---

<div class="post-metadata">

**Author:** ![howard\_chen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bbce88/32.png) [@howard\_chen](https://www.ruby-forum.com/u/howard_chen)\
**Post date:** [December 14, 2012, 1:38pm UTC](https://www.ruby-forum.com/t/setting-expires-header-to-multiple-locations/226544/4 "2012-12-14T13:38:06Z")

</div>

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

> > ```
> > expires 1y;
> > 
> > ```
> > 
> > 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 S.

> **[F5 NGINX: Built for cloud-native apps. Ready for AI traffic.](https://www.f5.com/products/nginx)**
>
> Deliver modern applications with NGINX. Explore high-performance load balancing, API gateways, and web server solutions trusted by top global enterprises.

---

<div class="post-metadata">

**Author:** ![howard\_chen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bbce88/32.png) [@howard\_chen](https://www.ruby-forum.com/u/howard_chen)\
**Post date:** [December 14, 2012, 3:44pm UTC](https://www.ruby-forum.com/t/setting-expires-header-to-multiple-locations/226544/5 "2012-12-14T15:44:17Z")

</div>

Hi Ignor

On Fri, Dec 14, 2012 at 8:37 PM, Igor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> expires 1y;
> 
> ```
> 
> }  
> }

My issue is in nginx, url can only be matched to only ONE location,  
unlike  
in Apache we have something like \* ExpiresByType\*, seems duplicate  
multiple locations is a must in nginx.

e.g.

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

```
# more unique config for /a, cannot be combined

```

}

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

```
# more unique config for /b, cannot be combined

```

}

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

```
# more unique config for /c, cannot be combined

```

}

Any better way?

---

<div class="post-metadata">

**Author:** ![howard\_chen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/bbce88/32.png) [@howard\_chen](https://www.ruby-forum.com/u/howard_chen)\
**Post date:** [December 14, 2012, 3:39pm UTC](https://www.ruby-forum.com/t/setting-expires-header-to-multiple-locations/226544/6 "2012-12-14T15:39:01Z")

</div>

Hi

On Fri, Dec 14, 2012 at 7:59 PM, Antonio P.P. Almeida  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)wrote:

> 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;  
> }

Thanks.

My example just in a simplified form and there are more config inside  
each  
a, b, c…So I must need separate blocks for them..
