# Does the expires directive add the head if the Expire head not exist?

**URL:** <https://www.ruby-forum.com/t/does-the-expires-directive-add-the-head-if-the-expire-head-not-exist/145945>\
**Category:** NGINX\
**Created:** [August 15, 2008, 11:51am UTC](https://www.ruby-forum.com/t/does-the-expires-directive-add-the-head-if-the-expire-head-not-exist/145945 "2008-08-15T11:51:07Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Delta\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f9ae1b/32.png) [@Delta\_Y](https://www.ruby-forum.com/u/Delta_Y)\
**Post date:** [August 15, 2008, 11:51am UTC](https://www.ruby-forum.com/t/does-the-expires-directive-add-the-head-if-the-expire-head-not-exist/145945/1 "2008-08-15T11:51:07Z")

</div>

I want to use “expires” directive to decrease the sever load.  
My question is if there is no Expires head from the backend server ,  
does  
nginx add such head?

BR,  
DeltaY

---

<div class="post-metadata">

**Author:** ![Delta\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f9ae1b/32.png) [@Delta\_Y](https://www.ruby-forum.com/u/Delta_Y)\
**Post date:** [August 17, 2008, 1:35pm UTC](https://www.ruby-forum.com/t/does-the-expires-directive-add-the-head-if-the-expire-head-not-exist/145945/2 "2008-08-17T13:35:40Z")

</div>

I use

location / {  
proxy\_redirect off;  
proxy\_redirect default;

```
        #if ( $request_uri ~* ^.+.(css|jpg|jpeg|gif)$ ) {
        if ( $content_type ~* css ) {
           expires 1h;

        }

        proxy_pass http://my_upstream;

    }

```

but it does not work.

when I use $request\_uri the browser get error response. It seems the  
nginx  
box reset the tcp connection.  
when I use $content\_type no Expires head returned by nginx.

2008/8/15 Igor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Delta\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f9ae1b/32.png) [@Delta\_Y](https://www.ruby-forum.com/u/Delta_Y)\
**Post date:** [August 15, 2008, 12:19pm UTC](https://www.ruby-forum.com/t/does-the-expires-directive-add-the-head-if-the-expire-head-not-exist/145945/3 "2008-08-15T12:19:12Z")

</div>

On Fri, Aug 15, 2008 at 05:42:06PM +0800, Delta Y. wrote:

> I want to use “expires” directive to decrease the sever load.  
> My question is if there is no Expires head from the backend server , does  
> nginx add such head?

nginx’s expires overrides backend headers.

---

<div class="post-metadata">

**Author:** ![Delta\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f9ae1b/32.png) [@Delta\_Y](https://www.ruby-forum.com/u/Delta_Y)\
**Post date:** [August 18, 2008, 12:42am UTC](https://www.ruby-forum.com/t/does-the-expires-directive-add-the-head-if-the-expire-head-not-exist/145945/4 "2008-08-18T00:42:53Z")

</div>

Hello!

On Sun, Aug 17, 2008 at 07:27:38PM +0800, Delta Y. wrote:

> I use
> 
> location / {  
> proxy\_redirect off;  
> proxy\_redirect default;

JFYI:

“proxy\_redirect default” is useless here since redirects was  
switched off by “proxy\_redirect off”.

> ```
> #if ( $request_uri ~* ^.+.(css|jpg|jpeg|gif)$ ) {
> 
> ```

JFYI:

Since last dot in regex isn’t escaped, this expression will match  
something like “/some-notes-about-css” as well as “/style.css”.

> ```
> if ( $content_type ~* css ) {
> 
> ```

Variable $content\_type contains Content-Type header value from  
_request_. It’s unlikely that request will contain css in it’s  
Content-Type.

See [http://wiki.codemongers.com/NginxHttpCoreModule#var\_content\_type](http://wiki.codemongers.com/NginxHttpCoreModule#var_content_type)

> when I use $request\_uri the browser get error response. It seems the nginx  
> box reset the tcp connection.

I’ve just checked and with $request\_uri it works as expected. I.e.  
nginx adds Expires and Cache-Control headers to response if .

Try tcpdump / error log / error log at debug level to figure out  
what’s actually happening.

> when I use $content\_type no Expires head returned by nginx.

Also as expected, see above.

Maxim D.

---

<div class="post-metadata">

**Author:** ![Delta\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f9ae1b/32.png) [@Delta\_Y](https://www.ruby-forum.com/u/Delta_Y)\
**Post date:** [August 18, 2008, 4:52am UTC](https://www.ruby-forum.com/t/does-the-expires-directive-add-the-head-if-the-expire-head-not-exist/145945/5 "2008-08-18T04:52:10Z")

</div>

I think it’s because ncache2.0 module.

I built nginx 0.7.10 with ncache2.0 but i didn’t turn it on in my  
conf.

When I use if ( $request\_uri ~\* ^.+.(css|jpg|jpeg|gif)$ ) {  
#if ( $content\_type ~ css ) {  
expires max;

```
  }

```

I the error log is :

2008/08/18 10:11:16 [notice] 5589#0: using the “epoll” event method  
2008/08/18 10:11:16 [notice] 5589#0: nginx/0.7.10  
2008/08/18 10:11:16 [notice] 5589#0: built by gcc 4.1.2 20061115  
(prerelease) (Debian 4.1.1-21)  
2008/08/18 10:11:16 [notice] 5589#0: OS: Linux 2.6.22.18-co-0.7.3  
2008/08/18 10:11:16 [notice] 5589#0: getrlimit(RLIMIT\_NOFILE): 1024:1024  
2008/08/18 10:11:16 [notice] 5595#0: start worker processes  
2008/08/18 10:11:16 [notice] 5595#0: start worker process 5596  
2008/08/18 10:12:26 [notice] 5595#0: signal 17 (SIGCHLD) received  
2008/08/18 10:12:26 [alert] 5595#0: worker process 5596 exited on signal  
11  
2008/08/18 10:12:26 [notice] 5595#0: start worker process 5602  
2008/08/18 10:12:32 [notice] 5595#0: signal 17 (SIGCHLD) received  
2008/08/18 10:12:32 [alert] 5595#0: worker process 5602 exited on signal  
11  
2008/08/18 10:12:32 [notice] 5595#0: start worker process 5608  
2008/08/18 10:13:20 [notice] 5595#0: signal 17 (SIGCHLD) received  
2008/08/18 10:13:20 [alert] 5595#0: worker process 5608 exited on signal  
11  
2008/08/18 10:13:20 [notice] 5595#0: start worker process 5625

But when I build without ncache2.0 , the same conf works as expected.  
I will report the bug to the author of ncache.

2008/8/18 Delta Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![Delta\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f9ae1b/32.png) [@Delta\_Y](https://www.ruby-forum.com/u/Delta_Y)\
**Post date:** [August 18, 2008, 4:01am UTC](https://www.ruby-forum.com/t/does-the-expires-directive-add-the-head-if-the-expire-head-not-exist/145945/6 "2008-08-18T04:01:54Z")

</div>

2008/8/18 Maxim D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> JFYI:
> 
> “proxy\_redirect default” is useless here since redirects was switched off by “proxy\_redirect off”.
> 
> > ```
> > #if ( $request_uri ~* ^.+.(css|jpg|jpeg|gif)$ ) {
> > 
> > ```

I had thought proxy\_redirect off first clear all previous proxy  
redirect settings, then the second proxy\_redirect default will  
define a clean default settings. Maybe I 'm wrong.

> JFYI:
> 
> Since last dot in regex isn’t escaped, this expression will match something like “/some-notes-about-css” as well as “/style.css”.

Aha, I only copied it from others configuration for my test, but  
thank you anyway, I will tweak it in production environment later.

> > ```
> > if ( $content_type ~* css ) {
> > 
> > ```
> 
> Variable $content\_type contains Content-Type header value from _request_. It’s unlikely that request will contain css in it’s Content-Type.
> 
> See [http://wiki.codemongers.com/NginxHttpCoreModule#var\_content\_type](http://wiki.codemongers.com/NginxHttpCoreModule#var_content_type)

I think Content-Type value can be text/css .

> > when I use $request\_uri the browser get error response. It seems the nginx  
> > box reset the tcp connection.
> 
> I’ve just checked and with $request\_uri it works as expected. I.e. nginx adds Expires and Cache-Control headers to response if .
> 
> Try tcpdump / error log / error log at debug level to figure out what’s actually happening.  
> I will try again and share with others what mistake I had made, 🙂

---

<div class="post-metadata">

**Author:** ![Delta\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f9ae1b/32.png) [@Delta\_Y](https://www.ruby-forum.com/u/Delta_Y)\
**Post date:** [August 18, 2008, 7:28am UTC](https://www.ruby-forum.com/t/does-the-expires-directive-add-the-head-if-the-expire-head-not-exist/145945/7 "2008-08-18T07:28:15Z")

</div>

On Mon, Aug 18, 2008 at 12:37:40PM +0800, Delta Y. wrote:

> So the only question remained is : Is it possible to set Expire  
> header according to the response Content-Type in nginx?

No. Use the following configuraiton:

location / {  
proxy\_pass [http://upstream](http://upstream);  
proxy\_redirect default;  
}

location \*~ .(css|jpg|jpeg|gif)$ {  
proxy\_pass [http://upstream](http://upstream);  
expires max;  
}

---

<div class="post-metadata">

**Author:** ![Delta\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f9ae1b/32.png) [@Delta\_Y](https://www.ruby-forum.com/u/Delta_Y)\
**Post date:** [August 18, 2008, 9:27am UTC](https://www.ruby-forum.com/t/does-the-expires-directive-add-the-head-if-the-expire-head-not-exist/145945/8 "2008-08-18T09:27:03Z")

</div>

Hi Igor,  
I use nginx as a pure reverse proxy.  
I think set the Expires header according to the response Content-Type  
is more resonable than the request uri in this scenario.

Do you have any plan to add the directive like

proxy\_set\_expires text/css 30d

Thanks.  
DeltaY

2008/8/18, Igor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![Delta\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f9ae1b/32.png) [@Delta\_Y](https://www.ruby-forum.com/u/Delta_Y)\
**Post date:** [August 19, 2008, 10:39am UTC](https://www.ruby-forum.com/t/does-the-expires-directive-add-the-head-if-the-expire-head-not-exist/145945/9 "2008-08-19T10:39:45Z")

</div>

On Mon, Aug 18, 2008 at 03:18:23PM +0800, Delta Y. wrote:

> I use nginx as a pure reverse proxy.  
> I think set the Expires header according to the response Content-Type  
> is more resonable than the request uri in this scenario.
> 
> Do you have any plan to add the directive like
> 
> proxy\_set\_expires text/css 30d

Yes, it may be added, but in form “expires text/css image/gif 30d”.

---

<div class="post-metadata">

**Author:** ![Delta\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f9ae1b/32.png) [@Delta\_Y](https://www.ruby-forum.com/u/Delta_Y)\
**Post date:** [August 20, 2008, 11:54am UTC](https://www.ruby-forum.com/t/does-the-expires-directive-add-the-head-if-the-expire-head-not-exist/145945/10 "2008-08-20T11:54:22Z")

</div>

That’s great, thanks !

I’m waiting to test this feature.

2008/8/19 Igor S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![Delta\_Y](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f9ae1b/32.png) [@Delta\_Y](https://www.ruby-forum.com/u/Delta_Y)\
**Post date:** [August 18, 2008, 6:44am UTC](https://www.ruby-forum.com/t/does-the-expires-directive-add-the-head-if-the-expire-head-not-exist/145945/11 "2008-08-18T06:44:24Z")

</div>

So the only question remained is : Is it possible to set Expire  
header according to the response Content-Type in nginx?

2008/8/18 Delta Y. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
