How to Cache-Control Headers in Nginx Part 3

Cache-Control Headers in Nginx Part - 3

Caching is an important protocol as per as HTTP and browsing. The basic principle of content caching is to offload repetitive work from the upstream servers. There are specific directives for caching mechanisms and these directives need to be followed by caching servers either be it in the browser or a dedicated cache server. The cache policies are specified by the browsers and dedicated cache servers. The cache mechanism works with the help of cache-control header.


In our previous tutorial, we have covered expire header and some basics of the cache-control header. If you have missed it, you can check it at the following link:

How To Cache-Control In Nginx - Part 1

How To Cache-Contril In Nginx - Part 2

Cache &ndash Control Header types


&bull Cache-control : no-store
&bull Cache-control : no-cache
&bull Cache-control : max-age=0
&bull Cache-control : s-maxage=0
&bull Cache-control : must-revalidate
&bull Pragma : no-cache

In this tutorial, we are going to cover s- maxage

s-maxage

s-maxage is specifically for intermediate cache server which means the public cache will store the cache for the seconds mentioned in that directive. For example, if a webserver sends its configuration with both the max-age and s-maxage directive in it, you can see both the headers in it.

s-maxage is applicable only for intermediate cache (public cache).

* Private Cache => Client browser Cache
* Public Cache => A dedicated server which is maintained only for storing Cache

Cache- Controlling

Let us do a simple Virtual Host configuration with maxage header. Open your configuration file.

[root@linuxhelp ~]# cd /etc/nginx/conf.d/
[root@linuxhelp conf.d]# vim vir.conf

Make sure you make the following modifications.

server {
   server_name www.linuxhelp1.com 
   location / {
    root /usr/share/nginx/html 
    index index.html index.htm 
   add_header Cache-Control max-age=120 
         }
}
Save and exit it

You shall now restart your nginx service by making use of

[root@linuxhelp html]# systemctl restart nginx


Now execute curl command and also check it in the browser

[root@linuxhelp html]# curl -I http://www.linuxhelp1.com/web1.html
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Wed, 07 Feb 2018 12:34:19 GMT
Content-Type: text/html
Content-Length: 20
Last-Modified: Wed, 07 Feb 2018 12:33:50 GMT
Connection: keep-alive
ETag: " 5a7af22e-14" 
Cache-Control: max-age=120
Accept-Ranges: bytes

We can make the modifications now, so assign s-max age in the configuration file. Open the file.

[root@linuxhelp ~]# cd /etc/nginx/conf.d/
[root@linuxhelp conf.d]# vim vir.conf

And make the following changes.

server {
   server_name www.linuxhelp1.com 
   location / {
    root /usr/share/nginx/html 
    index index.html index.htm 
#   add_header Cache-Control max-age=120 
  add_header Cache-Control s-maxage=200 
         }
}

Once it is done, execute curl command

[root@linuxhelp conf.d]# curl -I http://www.linuxhelp1.com
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Thu, 15 Feb 2018 12:01:08 GMT
Content-Type: text/html
Content-Length: 42
Last-Modified: Thu, 15 Feb 2018 11:59:30 GMT
Connection: keep-alive
ETag: " 5a857622-2a" 
Cache-Control: s-maxage=200
Accept-Ranges: bytes

You will notice that Cache-Control is configured.

You can now restart the nginx service.

[root@linuxhelp conf.d]# systemctl restart nginx

Next, assign both the headers inside the configuration file. So, open the configuration file by making use of the following commands. And make the below configuration.

[root@linuxhelp ~]# cd /etc/nginx/conf.d/
[root@linuxhelp conf.d]# vim vir.conf
server {
   server_name www.linuxhelp1.com 
   location / {
    root /usr/share/nginx/html 
    index index.html index.htm 
   add_header Cache-Control max-age=120 
    add_header Cache-Control s-maxage=200 
         }
}

Check for the syntax now.

[root@linuxhelp conf.d]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Again, restart the nginx service.

[root@linuxhelp conf.d]# systemctl restart nginx

You should now execute the curl command again.

[root@linuxhelp conf.d]# curl -I http://www.linuxhelp1.com

You will notice the Cache-Control configuration.

HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Thu, 15 Feb 2018 12:02:02 GMT
Content-Type: text/html
Content-Length: 42
Last-Modified: Thu, 15 Feb 2018 11:59:30 GMT
Connection: keep-alive
ETag: " 5a857622-2a" 
Cache-Control: max-age=120
Cache-Control: s-maxage=200
Accept-Ranges: bytes

This is how both the Cache-control headers been used in the nginx configuration file.

The major difference is only when max-age is specified both the private and public cache gets the cache value assigned in the max-age directive.

After assigning both the values if you check in the browser only max-age gets displayed

This is because it shows only (private) that is client’ s browser cache. With this, the tutorial comes to an end.

FAQ
Q
how to check nginx configuration is correct or not ?
A
use the following command to check nginx configuration

# nginx -t
Q
What is Cache-Control in nginx?
A
Cache-Control is an HTTP cache header comprised of a set of directives that allow you define when / how a response should be cached and for how long. HTTP caching occurs when a browser stores.
Q
I have a REST API behind an nginx proxy. Proxying works fine, however I am unable to cache any responses. Any one help me?
A
This must be specified with proxy_cache_valid directive. proxy_cache one; proxy_cache_key $host$uri$is_args$args; proxy_cache_valid 200 10m;
Q
Set “Expires” and “Cache-Control” headers on Nginx?
A
You have an incorrect setting in your php.ini file: The session.cache_limiter value is set to nocache in the default php.ini file and needs to be changed.
Q
how to Delivering Cached Content When the Origin is Down?
A
Please follow the steps as below location / { # ... proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504; }