I have a PHP script that generates a thumbnail based on an existing path (more info). The basic idea is this:
Given the document root /web/
, an image lives in /web/images/foo.jpg
. When a predefined thumbnail filter named thumb
is requested in the url /web/thumb/foo.jpg
, the PHP script picks it up, and generates a thumbnail on that location, so that it will be served directly via Nginx on the next request.
Now this works with the following (simplified) configuration:
server {
location / {
try_files $uri @rewriteapp;
}
}
Where @rewriteapp
contains the rewrite rule for the application.
The problem is that I want to add an Expires
header to the cached thumbnails. But when I add something like this
location ~* \.(jpe?g|gif|png)$ {
expires 1y;
}
It only works with generated thumbnails, but it returns a 404 on the request that should trigger the @rewriteapp
rule.
I tried adding that last block before or after the first location block, I also tried to include the same try_files
statement inside my location w/ expires block. But none of this works.
How can I add the header to my images?
UPDATE:
The accepted answer below contains a more detailed explanation why this failed. In my case I solved it by replacing the second location block with this:
location /thumb/ {
try_files $uri @rewriteapp;
expires 1y; access_log off; log_not_found off;
}