My pages are not getting served from cache.But nginx is actually caching the files

i have set up an nginx server in ubuntu as a reverse proxy cache server.My application code resides in /var/www/myapp folder.

following are the configuration i have given in

server {
        listen   80; ## listen for ipv4; this line is default and implied
        root /var/www/;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name localhost;

        location / {

            proxy_pass         http://127.0.0.1:8080/;
            rewrite ^([^.]*[^/])$ $1/ permanent;
            add_header X-Cache-Status $upstream_cache_status;
        }

        location /doc/ {
                alias /usr/share/doc/;
                autoindex on;
                allow 127.0.0.1;
                deny all;
        }
    }

is the content of my nginx/sites-available/default file

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
        worker_connections 1024 ;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

         server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # Logging Settings
        ##

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

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

         gzip_vary on;
          gzip_proxied any;
         gzip_comp_level 6;
         gzip_buffers 16 8k;
         gzip_http_version 1.1;
         gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;


        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;

        proxy_cache_path  /var/www/cache levels=1:2 keys_zone=my-cache:8m max_size=1000m inactive=600m;
        proxy_temp_path /var/www/cache/tmp;
        proxy_cache my-cache;
        proxy_cache_valid  200 302  60m;
        proxy_cache_valid  404      1m;

}

is the content of my nginx/nginx.conf file

Nginx is caching the files under /var/www/cache directory

But when i check the header response of my page http://mydomain.com/myapp in firefox using firebug it shows

Cache-Control   no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection  keep-alive
Content-Encoding    gzip
Content-Length  3817
Content-Type    text/html; charset=utf-8
Date    Fri, 29 Mar 2013 10:19:23 GMT
Expires Thu, 19 Nov 1981 08:52:00 GMT
Pragma  no-cache
Server  nginx/1.1.19
Vary    Accept-Encoding
X-Cache-Status  MISS
X-Powered-By    PHP/5.3.10-1ubuntu3.6

X-Cache-Status is MISS. Why it is not served from the cache?


Edit: I missed this the first time, but your app is sending this header Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0 which will prevent caching. You should modify your app to not send this header if it's not necessary.


I've tried your settings and on the first request, I get a MISS, which is expected since it's not yet in the cache. On subsequent requests, I get a HIT.

I'm using version 1.2.6, while you're using 1.1.9. In the release notes, there seem to be a few bug fixes for caching between your version and mine. Perhaps your configuration is okay but your version is buggy?

You could also try logging to see what nginx is saying on the server side:

log_format cache_status '[$time_local] "$request"  $upstream_cache_status';
access_log logs/cache.log cache_status;

Along with the other $upstream_ variables, you might be able to get more information about what's going wrong via logging.


From: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_valid

Syntax: proxy_cache_valid [code ...] time;

...

Parameters of caching can also be set directly in the response header. This has higher priority than setting of caching time using the directive.

  • The “X-Accel-Expires” header field sets caching time of a response in seconds. The zero value disables caching for a response. If the value starts with the @ prefix, it sets an absolute time in seconds since Epoch, up to which the response may be cached.
  • If the header does not include the “X-Accel-Expires” field, parameters of caching may be set in the header fields “Expires” or
    “Cache-Control”.
  • If the header includes the “Set-Cookie” field, such a response will not be cached.
  • If the header includes the “Vary” field with the special value “*”, such a response will not be cached (1.7.7). If the header includes
    the “Vary” field with another value, such a response will be cached
    taking into account the corresponding request header fields (1.7.7).

Processing of one or more of these response header fields can be disabled using the proxy_ignore_headers directive.

Most web apps set Set-Cookie header, so a response will not be cached. To fix that, use this directive:

proxy_ignore_headers Set-Cookie;