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

cachenginx

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?

Best Answer

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.