1

I have a dynamic content controller in CodeIgniter that pulls images from GridFS. The server is running nginx and I am trying to set the cache control headers in my nginx config to cache the images served by this dynamic content controller for 7 days. I have the config set correctly in my nginx config, but I am getting 404 headers from nginx because the files do not physically exist on the server.

My cache control directive is as follows:

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires 7d;
    log_not_found off;
}

log_not_found helps keep nginx from logging the 404 error, but the headers that are sent to the browser are still 404 errors. I tried setting the headers manually via php's "header" function, but because nginx is using php-fpm, it was doing some weird stuff.

Can anyone point me in the correct direction on how to get my cache control headers set up properly for this situation? Thanks everyone =)

UPDATE:

I changed my nginx conf with a special location for all my static files and my dynamic controller.

location ~ ^/(dres|js|css|art)/ {
    access_log off;
    expires 7d;
    add_header Cache-Control public;
    try_files $uri $uri/ /index.php?$args;
}

Nginx is setting the correct expires headers on the static files, but I cannot for the life of me get fastcgi and nginx to output the expires headers for the dynamically output images. I must be missing something in my fastcgi config to allow expiration headers when serving php files.

  • The uri for my dynamic content is in the following format: http://example.com/controller/my_image_{imageid}.jpg There is a rewrite that forces everything to go through the main index.php file for the site. If I change that directive to the path of my controller before its rewritten to the index, instead of matching the extension of nonexistent files, would that prevent the 404 error? – user1106959 Dec 20 '11 at 16:19
  • here is my full conf: http://pastebin.com/sJ1vrkwF and yes, anything thats non-static is running through php. Thanks for your help =) – user1106959 Dec 20 '11 at 18:11

2 Answers2

0

Aren't you supposed to set fastcgi_cache for that?

Nat
  • 3,499
  • 18
  • 21
  • I am not trying to cache the files server side, I'm simply trying to set the expires header and tell the client side browser to cache the images... – user1106959 Dec 20 '11 at 18:31
0

Solved for the most part. Realized that using php's "header" function was working, there were other issues which were making me think it was not. I just added this to my dynamic image controller:

// seconds, minutes, hours, days
$expires = (60*60*24*7);
header("Pragma: public");
header("Cache-Control: maxage=".$expires);
header('Expires: ' . gmdate('D, d M Y H:i:s', time()+$expires) . ' GMT');       

Now at least the expiration is working like I want for the dynamic images. I haven't figured out how to specify expiration for static files without getting a 404 on these dynamic images, but this is better for now.