Page Caching with Nginx

Nginx can also be used as a cache server for serving cache static content.In this tutorial we will learn how to do page caching with Nginx.

To start, install the latest stable Nginx avaliable at http://wiki.nginx.org/NginxInstall.
Next edit your nginx.conf and add the proxy_cache_path directive to define a named cache storage. These are independent of servers and locations, and can be reused inside each later on.
...
http {
...

proxy_cache_path /var/cache/nginx keys_zone=anonymous:10m;

proxy_temp_path /tmp/nginx;

}

  • proxy_cache_path and  proxy_temp_path — are used to set default cache folder settings and temporary folder

Next create the directory for the cache:
mkdir -p /var/cache/nginx

Once your are done with above mentioned steps,It time to make the changes in the nginx.conf.Add the following directives in the nginx conf for caching only get and head requests.



location / {
if ($request_method !~ ^(GET|HEAD)$) {
            set $nocache 1;
        }
proxy_pass    http://127.0.0.1:8080;
proxy_cache browse;
proxy_cache_key $request_uri;
proxy_cache_valid  1440m;
proxy_cache_use_stale updating;
proxy_ignore_headers "Set-Cookie";
proxy_cache_bypass  $nocache;
proxy_no_cache      $nocache;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header X-Cached $upstream_cache_status;
 log_format custom '$remote_addr - $remote_user [$time_local]  '
 '"$request" $status $body_bytes_sent '
 '"$http_referer" "$http_user_agent" nocache:$upstream_cache_status';
 access_log  /var/log/nginx/microcache.log custom;
}


I only included what you need to add to your virtualhost configuration that you are already using, some more configuration can exist there


  • proxy_pass sets the address of the proxied server and the URI to which location will be mapped


  • proxy_cache  you pass here the name of your cache server
  • proxy_cache_key this is important, do not forget to set this variable else content may be passed to other vhosts and that’s not good.
  • proxy_cache_valid set some validation on the cached files

  • proxy_cache_use_stale updating  tells Nginx when to serve a stale item from the proxy cache.


  • proxy_ignore_headers  Prohibits the processing of the header lines from the proxy server's response.

  • proxy_cache_bypass set the bypass headers that will invalidate the cache and refresh it
  • proxy_no_cache Specifies in what cases a response will not be cached

  • proxy_set_header This directive allows to redefine and to add some request header lines which will be transferred to the proxied server.

Make sure though you have created the cache folders before you reload the configuration.Once you are done with all the above steps,You caching has been setup successfully.To Test the Caching you can simple check the headers with curl.

A simple example with curl is given below:-

akhil@akhil-Dell-System-Vostro-3360:~# curl -I http://www.example.com/search/simple/CD
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 18 Mar 2014 07:40:07 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 0
Connection: keep-alive
Location: http://www.example.com/search/simple/CD
Vary: Accept-Encoding

X-Cached: MISS


The X-cached filed shows whether the page is served from cache or not.if it's a MISS then the page is not served from the cache and if it's a HIT then the page has been served from the Cache.Requesting the same page again will change the status of X-Cached.

akhil@akhil-Dell-System-Vostro-3360:~# curl -I http://www.example.com/search/simple/CD
HTTP/1.1 200 OK
Server: nginx
Date: Tue, 18 Mar 2014 07:40:07 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 0
Connection: keep-alive
Location: http://www.example.com/search/simple/CD
Vary: Accept-Encoding

X-Cached: HIT

And that’s all for today, hope it helps someone and will see you again.Don’t forget to comment below and subscribe!














author image

Written by

Experience running high-traffic web services, service configuration, monitoring,troubleshooting and change management.Expertise in Linux/Unix system administration, including configuration, troubleshooting, Python scripting language.

0 comments:

 

© 2013 Akhil's Blog. All rights resevered. Designed by Templateism

Back To Top