0

I am new to caching on nginx. I have been trying to set caching on server but X-Cache-Status is giving a MISS even after adding configurations for css/js/and images. This is leading to high load time and slow speed even on low traffic. Any idea what I am missing ?

Following is the response Header :

Cache-Control   
max-age=86400
Connection  
keep-alive
Content-Encoding    
gzip
Content-Type    
text/css
Date    
Wed, 18 Jan 2017 16:00:34 GMT
Expires 
Thu, 19 Jan 2017 16:00:34 GMT
Last-Modified   
Mon, 19 Oct 2015 09:26:18 GMT
Server  
nginx/1.4.6 (Ubuntu)
Transfer-Encoding   
chunked
Vary    
Accept-Encoding
X-Cache-Status  
MISS

I am using https where I am redirecting http over to https.Here is the sites-enabled file I have setup.

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=TOMCAT:50m max_size=100m;
server {
   listen   80;
   server_name *.xyz.com;
   default_type text/html; 
   return 307 https://$host$request_uri;
   root   /var/lib/tomcat7/webapps;
   index  index.html index.jsp;
      location / {
    set $no_cache "";
        if ($request_method !~ ^(GET|HEAD)$) {
            set $no_cache "1";
        }
        if ($no_cache = "1") {
            add_header Set-Cookie "_mcnc=1; Max-Age=2; Path=/";
            add_header X-Microcachable "0";
        }
        if ($http_cookie ~* "_mcnc") {
            set $no_cache "1";
        }
    if ($request_uri ~* ".(jpg|jpeg|gif|gz|zip|flv|rar|wmv|avi|css|swf|png|htc|ico|mpeg|mpg|txt|mp3|mov|js)(\?v=[0-9.]+)?$") {
    expires 1d;
    access_log off;
    break;
    }
      proxy_no_cache $no_cache;
        proxy_cache_bypass $no_cache;
        proxy_cache TOMCAT;
        proxy_cache_key $scheme$host$request_method$request_uri;
        proxy_cache_valid 200 302 1s;
        proxy_cache_valid 301 1s;
        proxy_cache_valid any 1s;
        proxy_cache_use_stale updating;
      sendfile off;
      proxy_pass         http://127.0.0.1:8080/;
      proxy_redirect     default;

      proxy_set_header   Host             $host;
      proxy_set_header   X-Real-IP        $remote_addr;
      proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
      proxy_max_temp_file_size 0;
    add_header X-Cache-Status $upstream_cache_status;
      #this is the maximum upload size
      client_max_body_size       10m;
      client_body_buffer_size    128k;

      proxy_connect_timeout      90;
      proxy_send_timeout         90;
      proxy_read_timeout         90;

      proxy_buffer_size          4k;
      proxy_buffers              4 32k;
      proxy_busy_buffers_size    64k;
      proxy_temp_file_write_size 64k;
   }
}

server {
        server_name *.xyz.com;
        listen 443;

        ssl on;
        ssl_session_timeout 5m;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        #make sure you already have this certificate pair!
    ssl_certificate /etc/ssl/certs/xyz.crt;
        ssl_certificate_key /etc/ssl/certs/xyz.key;
        ssl_session_cache shared:SSL:10m;

        location / {
            set $no_cache "";
        if ($request_method !~ ^(GET|HEAD)$) {
            set $no_cache "1"; 
        }   
        if ($no_cache = "1") {
            add_header Set-Cookie "_mcnc=1; Max-Age=2; Path=/";
            add_header X-Microcachable "0"; 
        }   
        if ($http_cookie ~* "_mcnc") {
            set $no_cache "1";
        } 
    if ($request_uri ~* ".(jpg|jpeg|gif|gz|zip|flv|rar|wmv|avi|css|swf|png|htc|ico|mpeg|mpg|txt|mp3|mov|js)(\?v=[0-9.]+)?$") {
    expires 1d;
    access_log off;
    break;
    }  
      proxy_no_cache $no_cache;
        proxy_cache_bypass $no_cache;
        proxy_cache TOMCAT;
        proxy_cache_key $scheme$host$request_method$request_uri;
        proxy_cache_valid 200 302 1s;
        proxy_cache_valid 301 1s;
        proxy_cache_valid any 1s;
        proxy_cache_use_stale updating;

            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto https;
    add_header X-Cache-Status $upstream_cache_status;
            proxy_redirect off;
            proxy_connect_timeout      240;
            proxy_send_timeout         240;
            proxy_read_timeout         240;
            # note, there is not SSL here! plain HTTP is used
       proxy_pass http://127.0.0.1:8081;
        }
     }

Any suggestions ?

0

simply add this. This sets the expire date to maximum date (something like 2037 as I remember) and the cache control to 10 years.

location ~* \.(css|gif|ico|jpeg|jpg|js|png|woff|woff2|ttf|ttc|otf|eot)$ {
        expires max;
        log_not_found off;
    }
  • Hi Liyanage, I tried that but site didnt render any of the extensions just the simple html without any resources – prit kalra Jan 19 '17 at 14:26
  • upload a couple of image files with the given extension such as myimage.png. make sure to upload it in the root web directory. /var/www/html. and then try to access it via the web browser, and then check again its header in chrome (press f12-> network ->press f5 -> click the image -> and then check its header) – Don Dilanga Jan 19 '17 at 14:27

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.