Nginx – Cache-control for permanent 301 redirects nginx

cachenginxredirect

I was wondering if there is a way to control lifetime of the redirects in Nginx?

We would liek to cache 301 redirects in CDN for specific amount of time, let say 20 minutes and the CDN is controlled by the standard caching headers. By default there is no Cache-control or Expires directives with the Nginx redirect. That could cause the redirect to be cached for a really long time. By having specific redirect lifetime the system could have a chance to correct itself, knowing that even "permanent" redirect change from time to time..

The other thing is that those redirects are included from the Server block, which according the nginx specification should be evaluated before locations.

I tried to add add_header Cache-Control "max-age=1200, public"; to the bottom of the redirects file, but the problem is that Cache-control gets added twice – first comes let say from the backend script and the other one added by the add_header directive..

In Apache there is the environment variable trick to control headers for rewrites:

RewriteRule /taxonomy/term/(\d+)/feed /taxonomy/term/$1 [R=301,E=expire:1] Header always set Cache-Control "store, max-age=1200" env=expire

But I'm not sure how to accomplish this in Nginx.

Best Answer

Have you tried Cache-Control flags in your nginx configuration ?

Sample configuration:

upstream yourappserver{
  server 0.0.0.0:6677;
}


proxy_cache_path  /tmp/cache levels=1:2 keys_zone=my-test-cache:8m max_size=5000m inactive=300m;

server {
    listen 80;
    server_name your.domain.tld;
    root /path/to/the/document/root/;

    access_log  /var/log/nginx/access.log;

    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_cache my-test-cache;
      proxy_cache_valid  200 404  1m;
      proxy_cache_valid 302 20m;

      proxy_cache_use_stale   error timeout invalid_header updating;
      proxy_redirect off;

      if (-f $request_filename/index.html) {
        rewrite (.*) $1/index.html break;
      }
      if (-f $request_filename.html) {
        rewrite (.*) $1.html break;
      }
      if (!-f $request_filename) {
        proxy_pass http://yourappserver;
        break;
      }
    }

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

I think you are looking for this particular configuration snippet

proxy_cache_valid 302 20m;
Related Topic