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 / {

            rewrite ^([^.]*[^/])$ $1/ permanent;
            add_header X-Cache-Status $upstream_cache_status;

        location /doc/ {
                alias /usr/share/doc/;
                autoindex on;
                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.

  • I have added a simple hello world page in my document toot.my X-Cache status is HIT. but when i take mydomain.com/myapp it will always show MISS? And i can see files cached in my cache folder.Is it because the pages i am trying to cache is having cookies and sessions? – zamil Apr 1 '13 at 11:15
  • I missed this the first time, but your response has Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0 this will prevent caching. You need to modify your app to prevent sending this header when it's not necessary. – Jared Bond Apr 2 '13 at 0:07
  • I have tried to set my <meta http-equiv="Expires" content="public" /> like this in my header page(html page) but still the firebug shows Cache-Control no-store, no-cache, must-revalidate, post-check=0, pre-check=0. – zamil Apr 2 '13 at 6:11
  • You'll need to make sure the Cache-Control header is either not sent or has cache-positive values. If you have access to the PHP application's source code, you should first search it for instances of Cache-Control, Pragma and Expires and change accordingly. My suggestions would be to remove Pragma completely, set Cache-Control to max-age=432000 (5 days in seconds) and set Expires to 5 days in the future. If you want a different cache period, adjust max-age and Expires. Also, headers take precedence over meta tags, so focus on the headers. This doesn't seem like an nginx issue though. – Jared Bond Apr 3 '13 at 0:17

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
  • 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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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