Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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