I am using nginx as a reverse proxy in front of application server. The application responds with an X-Accel-Redirect header telling nginx which static file to serve. What I'd like to be able to do is have nginx cache some of these upstream responses so it can serve the correct file without hitting the app server.

Unfortunately, this doesn't work: nginx seems to refuse to cache any response with an X-Accel-Redirect header. If I don't use X-Accel-Redirect and get the app server to return the file itself then caching works perfectly. However this isn't very efficient as the app server then has to read the file off disk and send it to nginx, which then writes the file back to disk (in its cache) and sends it to the client.

Here is a stripped down version of my nginx config:

proxy_cache_path /tmp/nginx-cache keys_zone=testzone:10m;

server {
    location / {
        proxy_cache testzone;
        proxy_pass http://localhost:8000/;
    }

    location /static-files/ {
        internal;
        alias /var/static-files/;
    }
}

Does anyone know if what I'm attempting to do is possible? My suspicion is that when nginx spots the X-Accel-Redirect header it immediately jumps to processing the supplied URI and skips the normal caching logic, but it would be nice to have this confirmed.

link|improve this question
feedback

1 Answer

I suggest you rewrite your server and location block:

server {
    location / {
        proxy_cache testzone;
        proxy_pass http://localhost:8000;
    }

    location ~* \.(ico|js|jpg|png|gif|jpeg|mp3|wav|swf|mov|doc|pdf|flv|css)$ {
       expires max;
       proxy_cache testzone;
       proxy_pass http://localhost:8000;
    }
}

also look into proxy_temp_path http://wiki.nginx.org/HttpProxyModule#proxy_temp_path. Although already enabled by default (I think), I like to look at this folder to see if requests for static content are being stored.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.