# Nginx: conditional "expires" directive

**URL:** <https://www.ruby-forum.com/t/nginx-conditional-expires-directive/210746>\
**Category:** NGINX\
**Created:** [August 16, 2011, 1:15pm UTC](https://www.ruby-forum.com/t/nginx-conditional-expires-directive/210746 "2011-08-16T13:15:36Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [August 16, 2011, 1:15pm UTC](https://www.ruby-forum.com/t/nginx-conditional-expires-directive/210746/1 "2011-08-16T13:15:36Z")

</div>

I wanted to create if block for expires directive, but it’s not allowed  
in if context.

Like that:

if($args ~ “no\_caching=1”) {  
expires epoch;  
}  
Is it somehow possible to create this in any other way?

I need nginx to change expire header to epoch when simple $\_GET param  
no\_cache=1 is in present.

For visual checking purposes.

Thanks 😉

Posted at Nginx Forum:

> **[Nginx: conditional "expires" directive](https://forum.nginx.org/read.php?2,213841,213841#msg-213841)**
>
> I wanted to create if block for expires directive, but it's not allowed in if context. Like that: if($args ~ "no\_caching=1") { expires epoch; } Is it somehow possible to create this in any other way? I need nginx to c

---

<div class="post-metadata">

**Author:** ![somebi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a587f6/32.png) [@somebi](https://www.ruby-forum.com/u/somebi)\
**Post date:** [August 16, 2011, 1:36pm UTC](https://www.ruby-forum.com/t/nginx-conditional-expires-directive/210746/2 "2011-08-16T13:36:39Z")

</div>

On 16 Ago 2011 12h14 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I need nginx to change expire header to epoch when simple $\_GET  
> param no\_cache=1 is in present.

You can try using the map directive:

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

map $args $caching\_expires {  
default 30d;  
~no\_caching=1 epoch;  
}

location /whatever {  
expires $caching\_expires;  
}

I haven’t tried it myself.

— appa

---

<div class="post-metadata">

**Author:** ![somebi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a587f6/32.png) [@somebi](https://www.ruby-forum.com/u/somebi)\
**Post date:** [August 16, 2011, 2:16pm UTC](https://www.ruby-forum.com/t/nginx-conditional-expires-directive/210746/3 "2011-08-16T14:16:55Z")

</div>

I like your solution. But i’m getting error:

nginx: [emerg] “expires” directive invalid value in  
/usr/local/nginx/conf/nginx.conf:131  
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

When using expires $caching\_expires;

Posted at Nginx Forum:

> **[Re: Nginx: conditional "expires" directive](https://forum.nginx.org/read.php?2,213841,213844#msg-213844)**
>
> I wanted to create if block for expires directive, but it's not allowed in if context. Like that: if($args ~ "no\_caching=1") { expires epoch; } Is it somehow possible to create this in any other way? I need nginx to c

---

<div class="post-metadata">

**Author:** ![somebi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a587f6/32.png) [@somebi](https://www.ruby-forum.com/u/somebi)\
**Post date:** [August 16, 2011, 3:21pm UTC](https://www.ruby-forum.com/t/nginx-conditional-expires-directive/210746/4 "2011-08-16T15:21:19Z")

</div>

On 16 Ago 2011 13h25 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> It seems that expires directive does not support varaibles.

Then try this:

location / {

if ($arg\_no\_caching) {  
return 302 /no-caching;  
}

expires 30d;  
}

location /no-caching {  
expires epoch;  
}

— appa

---

<div class="post-metadata">

**Author:** ![somebi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a587f6/32.png) [@somebi](https://www.ruby-forum.com/u/somebi)\
**Post date:** [August 16, 2011, 5:11pm UTC](https://www.ruby-forum.com/t/nginx-conditional-expires-directive/210746/5 "2011-08-16T17:11:53Z")

</div>

Today Aug 16, 2011 at 08:25 somebi wrote:

> It seems that expires directive does not support varaibles.

You can try Cache-Control header instead:

server {  
…  
set $ccontrol “max-age=3600”;  
if($arg\_no\_cache) { set $ccontrol “no-cache”;}  
add\_header Cache-Control $ccontrol;  
…  
}

–  
WNGS-RIPE

---

<div class="post-metadata">

**Author:** ![somebi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a587f6/32.png) [@somebi](https://www.ruby-forum.com/u/somebi)\
**Post date:** [August 16, 2011, 2:25pm UTC](https://www.ruby-forum.com/t/nginx-conditional-expires-directive/210746/6 "2011-08-16T14:25:36Z")

</div>

It seems that expires directive does not support varaibles.

Posted at Nginx Forum:

> **[Re: Nginx: conditional "expires" directive](https://forum.nginx.org/read.php?2,213841,213845#msg-213845)**
>
> I wanted to create if block for expires directive, but it's not allowed in if context. Like that: if($args ~ "no\_caching=1") { expires epoch; } Is it somehow possible to create this in any other way? I need nginx to c

---

<div class="post-metadata">

**Author:** ![somebi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a587f6/32.png) [@somebi](https://www.ruby-forum.com/u/somebi)\
**Post date:** [August 16, 2011, 5:16pm UTC](https://www.ruby-forum.com/t/nginx-conditional-expires-directive/210746/7 "2011-08-16T17:16:12Z")

</div>

Nope still getting 304. Maybe something with rewrite and inside  
redirect?

Posted at Nginx Forum:

> **[Re: Nginx: conditional "expires" directive](https://forum.nginx.org/read.php?2,213841,213856#msg-213856)**
>
> I wanted to create if block for expires directive, but it's not allowed in if context. Like that: if($args ~ "no\_caching=1") { expires epoch; } Is it somehow possible to create this in any other way? I need nginx to c

---

<div class="post-metadata">

**Author:** ![somebi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a587f6/32.png) [@somebi](https://www.ruby-forum.com/u/somebi)\
**Post date:** [August 16, 2011, 5:16pm UTC](https://www.ruby-forum.com/t/nginx-conditional-expires-directive/210746/8 "2011-08-16T17:16:49Z")

</div>

Oleksandr i’ll try your solution. Just a moment.

Posted at Nginx Forum:

> **[Re: Nginx: conditional "expires" directive](https://forum.nginx.org/read.php?2,213841,213858#msg-213858)**
>
> I wanted to create if block for expires directive, but it's not allowed in if context. Like that: if($args ~ "no\_caching=1") { expires epoch; } Is it somehow possible to create this in any other way? I need nginx to c

---

<div class="post-metadata">

**Author:** ![somebi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a587f6/32.png) [@somebi](https://www.ruby-forum.com/u/somebi)\
**Post date:** [August 16, 2011, 6:02pm UTC](https://www.ruby-forum.com/t/nginx-conditional-expires-directive/210746/9 "2011-08-16T18:02:29Z")

</div>

On 16 Ago 2011 16h15 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Nope still getting 304. Maybe something with rewrite and inside  
> redirect?

It works here:

location /test {  
if ($arg\_no\_caching) {  
return 302 /test/t1;  
}

location = /test/t1 {  
expires epoch;  
}

expires 1d;  
}

Gives:

curl -I logs.damiao/test  
HTTP/1.1 200 OK  
Server: nginx  
Date: Tue, 16 Aug 2011 15:53:30 GMT  
Content-Type: application/octet-stream  
Content-Length: 0  
Connection: keep-alive  
Keep-Alive: timeout=10  
Expires: Wed, 17 Aug 2011 15:53:30 GMT  
Cache-Control: max-age=86400 \<—

curl -I logs.damiao/test?no\_caching=1  
HTTP/1.1 302 Moved Temporarily ←   
Server: nginx  
Date: Tue, 16 Aug 2011 15:53:53 GMT  
Content-Type: text/html  
Content-Length: 154  
Location: [http://logs.damiao/test/t1](http://logs.damiao/test/t1)  
Connection: keep-alive  
Keep-Alive: timeout=10  
Expires: Wed, 17 Aug 2011 15:53:53 GMT  
Cache-Control: max-age=86400 \<—

There’s something else in your config or backend that is interfering  
with the desired behavior, I guess.

— appa

---

<div class="post-metadata">

**Author:** ![somebi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a587f6/32.png) [@somebi](https://www.ruby-forum.com/u/somebi)\
**Post date:** [August 16, 2011, 6:13pm UTC](https://www.ruby-forum.com/t/nginx-conditional-expires-directive/210746/10 "2011-08-16T18:13:35Z")

</div>

Getting two max-age headers in result. One with 1 year and second with  
3600. ?no\_cache=1 gives the same result.  
if ($arg\_no\_cache) { set $ccontrol “no-cache”;} is ignored do not know  
why  
if ($args ~ “no\_cache=1”) { set $ccontrol “no-cache”;} ignored as well

Posted at Nginx Forum:

> **[Re: Nginx: conditional "expires" directive](https://forum.nginx.org/read.php?2,213841,213863#msg-213863)**
>
> I wanted to create if block for expires directive, but it's not allowed in if context. Like that: if($args ~ "no\_caching=1") { expires epoch; } Is it somehow possible to create this in any other way? I need nginx to c

---

<div class="post-metadata">

**Author:** ![somebi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a587f6/32.png) [@somebi](https://www.ruby-forum.com/u/somebi)\
**Post date:** [August 16, 2011, 6:41pm UTC](https://www.ruby-forum.com/t/nginx-conditional-expires-directive/210746/11 "2011-08-16T18:41:59Z")

</div>

On 16 Ago 2011 17h13 WEST, removed\_email\_address@domain.invalid wrote:

Oleksandr suggestion works.

curl -I logs.damiao/test?no\_caching=1  
HTTP/1.1 200 OK  
Server: nginx  
Date: Tue, 16 Aug 2011 16:35:08 GMT  
Content-Type: application/octet-stream  
Content-Length: 0  
Connection: keep-alive  
Keep-Alive: timeout=10  
Cache-Control: no-cache

curl -I logs.damiao/test  
HTTP/1.1 200 OK  
Server: nginx  
Date: Tue, 16 Aug 2011 16:35:14 GMT  
Content-Type: application/octet-stream  
Content-Length: 0  
Connection: keep-alive  
Keep-Alive: timeout=10  
Cache-Control: max-age=3600

— appa

---

<div class="post-metadata">

**Author:** ![somebi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a587f6/32.png) [@somebi](https://www.ruby-forum.com/u/somebi)\
**Post date:** [August 16, 2011, 6:56pm UTC](https://www.ruby-forum.com/t/nginx-conditional-expires-directive/210746/12 "2011-08-16T18:56:48Z")

</div>

Today Aug 16, 2011 at 12:13 somebi wrote:

> Getting two max-age headers in result. One with 1 year and second with  
> 3600. ?no\_cache=1 gives the same result.  
> if ($arg\_no\_cache) { set $ccontrol “no-cache”;} is ignored do not know

It’s because you specify expires directive too.  
Comment out expires and set “max-age=31536000” for 1 year.

–  
WNGS-RIPE

---

<div class="post-metadata">

**Author:** ![somebi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a587f6/32.png) [@somebi](https://www.ruby-forum.com/u/somebi)\
**Post date:** [August 16, 2011, 7:04pm UTC](https://www.ruby-forum.com/t/nginx-conditional-expires-directive/210746/13 "2011-08-16T19:04:12Z")

</div>

I’m getting:

Cache-Control:  
Connection:keep-alive  
Date:Tue, 16 Aug 2011 16:53:23 GMT  
Last-Modified:Tue, 09 Aug 2011 05:32:58 GMT  
Server:nginx

And 304 header

Posted at Nginx Forum:

> **[Re: Nginx: conditional "expires" directive](https://forum.nginx.org/read.php?2,213841,213867#msg-213867)**
>
> I wanted to create if block for expires directive, but it's not allowed in if context. Like that: if($args ~ "no\_caching=1") { expires epoch; } Is it somehow possible to create this in any other way? I need nginx to c

---

<div class="post-metadata">

**Author:** ![somebi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a587f6/32.png) [@somebi](https://www.ruby-forum.com/u/somebi)\
**Post date:** [August 16, 2011, 6:05pm UTC](https://www.ruby-forum.com/t/nginx-conditional-expires-directive/210746/14 "2011-08-16T18:05:34Z")

</div>

On 16 Ago 2011 16h59 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 16 Ago 2011 16h15 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Nope still getting 304. Maybe something with rewrite and inside  
> > redirect?
> 
> It works here:

Oops. Doing several things at the same time brings bad results ☹ It  
doesn’t work.

— appa

---

<div class="post-metadata">

**Author:** ![somebi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a587f6/32.png) [@somebi](https://www.ruby-forum.com/u/somebi)\
**Post date:** [August 16, 2011, 7:11pm UTC](https://www.ruby-forum.com/t/nginx-conditional-expires-directive/210746/15 "2011-08-16T19:11:36Z")

</div>

Today Aug 16, 2011 at 13:03 somebi wrote:

> I’m getting:
> 
> Cache-Control:  
> Connection:keep-alive  
> Date:Tue, 16 Aug 2011 16:53:23 GMT  
> Last-Modified:Tue, 09 Aug 2011 05:32:58 GMT  
> Server:nginx

Please show full config.

–  
WNGS-RIPE

---

<div class="post-metadata">

**Author:** ![somebi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a587f6/32.png) [@somebi](https://www.ruby-forum.com/u/somebi)\
**Post date:** [August 16, 2011, 8:00pm UTC](https://www.ruby-forum.com/t/nginx-conditional-expires-directive/210746/16 "2011-08-16T20:00:22Z")

</div>

worker\_processes 2;  
worker\_rlimit\_nofile 2048;  
worker\_priority -5;

events {  
multi\_accept on;  
worker\_connections 2048;  
}

http {  
include mime.types;  
default\_type application/octet-stream;  
autoindex off;  
server\_tokens off;  
client\_max\_body\_size 3m;  
access\_log off;  
sendfile on;  
keepalive\_timeout 2m;

server {  
server\_name [example.com](http://example.com);  
listen 80;  
root html;  
index index.html;

```
location / {
  rewrite ^\/.+$ 

```

http://www.example.com$request\_uri? permanent;  
break;  
}  
}

```
server {
server_name www.example.com;
    listen 80;
root html;
index index.html;

location ~*

```

^.+.(jpg|jpeg|gif|gz|zip|flv|rar|wmv|avi|css|swf|png|htc|ico|mpeg|mpg|txt|mp3|mov|js)$  
{  
break;  
root /opt/lampp/htdocs/randevu/public;  
#set $ccontrol “max-age=3600”;  
#if ($arg\_no\_cache) { set $ccontrol “no-cache max-age=0”;}  
#add\_header Cache-Control $ccontrol;  
expires 1y;  
}

```
location ~* ^\/$ {
  proxy_pass http://127.0.0.1:8888;
        proxy_redirect off;
  proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
  break;
}

    location / {

  proxy_buffers 8 64k;
  proxy_connect_timeout 2m;
  proxy_read_timeout 2m;
  proxy_send_timeout 2m;

  proxy_pass http://127.0.0.1:8080;
        proxy_redirect off;

  proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
    }

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root html;
    }

    location ~ /\.ht {
        deny all;
    }
}

```

}

Posted at Nginx Forum:

> **[Re: Nginx: conditional "expires" directive](https://forum.nginx.org/read.php?2,213841,213872#msg-213872)**
>
> I wanted to create if block for expires directive, but it's not allowed in if context. Like that: if($args ~ "no\_caching=1") { expires epoch; } Is it somehow possible to create this in any other way? I need nginx to c

---

<div class="post-metadata">

**Author:** ![somebi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a587f6/32.png) [@somebi](https://www.ruby-forum.com/u/somebi)\
**Post date:** [August 16, 2011, 8:18pm UTC](https://www.ruby-forum.com/t/nginx-conditional-expires-directive/210746/17 "2011-08-16T20:18:53Z")

</div>

Thanks. Now it’s working. I guess my mistake was in additional quotes.

Posted at Nginx Forum:

> **[Re: Nginx: conditional "expires" directive](https://forum.nginx.org/read.php?2,213841,213874#msg-213874)**
>
> I wanted to create if block for expires directive, but it's not allowed in if context. Like that: if($args ~ "no\_caching=1") { expires epoch; } Is it somehow possible to create this in any other way? I need nginx to c

---

<div class="post-metadata">

**Author:** ![somebi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a587f6/32.png) [@somebi](https://www.ruby-forum.com/u/somebi)\
**Post date:** [August 16, 2011, 8:10pm UTC](https://www.ruby-forum.com/t/nginx-conditional-expires-directive/210746/18 "2011-08-16T20:10:52Z")

</div>

On 16 Ago 2011 19h00 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

\> ^.+\.(jpg|jpeg|gif|gz|zip|flv|rar|wmv|avi|css|swf|png|htc|ico|mpeg|mpg|txt|mp3|mov|js)$ \> break; \> root /opt/lampp/htdocs/randevu/public; \> #set $ccontrol "max-age=3600"; \> #if ($arg\_no\_cache) { set $ccontrol "no-cache max-age=0";} \> #add\_header Cache-Control $ccontrol;

> expires 1y;

location ~\*  
^.+.(?:jpe?g|gif|gz|zip|flv|rar|wmv|avi|css|swf|png|htc|ico|mpeg|mpg|txt|mp3|mov|js)$  
{  
root /opt/lampp/htdocs/randevu/public;

set $cache\_control max-age=31536000; # 1 year

if ($arg\_no\_caching) {  
set $cache\_control no-cache;  
}

add\_header Cache-Control $cache\_control;  
}

— appa

---

<div class="post-metadata">

**Author:** ![somebi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a587f6/32.png) [@somebi](https://www.ruby-forum.com/u/somebi)\
**Post date:** [August 16, 2011, 8:33pm UTC](https://www.ruby-forum.com/t/nginx-conditional-expires-directive/210746/19 "2011-08-16T20:33:40Z")

</div>

On 16 Ago 2011 19h18 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Thanks. Now it’s working. I guess my mistake was in additional  
> quotes.

No. Your mistake was the expires 1y directive. The expires directive  
“automagically” sets a Cache-Control header.

You can do it without variables, like this:

location ~\*  
^.+.(?:jpe?g|gif|gz|zip|flv|rar|wmv|avi|css|swf|png|htc|ico|mpeg|mpg|txt|mp3|mov|js)$  
{  
root /opt/lampp/htdocs/randevu/public;

if ($arg\_no\_caching) {  
add\_header Cache-Control no-cache;  
break;  
}

add\_header Cache-Control max-age=31536000; # 1 year  
}

— appa

---

<div class="post-metadata">

**Author:** ![somebi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a587f6/32.png) [@somebi](https://www.ruby-forum.com/u/somebi)\
**Post date:** [August 16, 2011, 8:42pm UTC](https://www.ruby-forum.com/t/nginx-conditional-expires-directive/210746/20 "2011-08-16T20:42:56Z")

</div>

On 16 Ago 2011 19h23 WEST, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> No, your mistake was a “break” which stops processing before any  
> “if” would run. Also it is better to use “map” to set value instead  
> of “set/if”.

Indeed, there were two errors. I overlooked the break right at the  
top. There’s also the expires 1y directive.

— appa

[Next page](https://www.ruby-forum.com/t/nginx-conditional-expires-directive/210746.md?page=2)
