Nginx: conditional "expires" directive

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 :wink:

Posted at Nginx Forum:

On 16 Ago 2011 12h14 WEST, [email protected] 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:

http://wiki.nginx.org/HttpMapModule

map $args $caching_expires {
default 30d;
~no_caching=1 epoch;
}

location /whatever {
expires $caching_expires;
}

I haven’t tried it myself.

— appa

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:

On 16 Ago 2011 13h25 WEST, [email protected] 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

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

It seems that expires directive does not support varaibles.

Posted at Nginx Forum:

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

Posted at Nginx Forum:

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

Posted at Nginx Forum:

On 16 Ago 2011 16h15 WEST, [email protected] 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
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

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:

On 16 Ago 2011 17h13 WEST, [email protected] 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

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

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:

On 16 Ago 2011 16h59 WEST, [email protected] wrote:

On 16 Ago 2011 16h15 WEST, [email protected] 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 :frowning: It
doesn’t work.

— appa

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

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

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

Posted at Nginx Forum:

On 16 Ago 2011 19h00 WEST, [email protected] 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

On 16 Ago 2011 19h18 WEST, [email protected] 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

On 16 Ago 2011 19h23 WEST, [email protected] 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