• Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • Categories
    Category
    {{ postCtrl.tags }}
    • {{ category.tag_type }}

      • {{tag.tag_name}}
      • View more
  • News
  • Tutorials
  • Forums
  • Tags
  • Users
Tutorial News Comments FAQ Related Articles

Cache Control Headers in nginx Part 4

{{postValue.id}}

Cache-Control Headers in nginx - Part 4 (no-cache and must-revalidate headers )

Cache-Control Header types

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

No-cache Header
No-cache header is not similar too no-store wheres in no-store it will never store cache in public and private cache. In no-cache, the cache gets stored in both the caches (private and public), but every time when the client refreshes or requests it will revalidate with the application server whether the data is fresh or not. Next if when application server goes down completely at that time what no-cache directive will do? At that time no-cache directive will add age header along with it contains a label with a warning states that the document is not latest (older version) but in the Practical scenario it is not advisable to server document that time the must-revalidate header comes into the picture.

Next, open your configuration file

[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 
         }
}

Check the syntax and restart your nginx service as shown

[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

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

And then execute curl command as follows

[root@linuxhelp conf.d]# curl -I http://www.linuxhelp1.com/web1.html
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 20 Feb 2018 10:24:49 GMT
Content-Type: text/html
Content-Length: 31
Last-Modified: Tue, 20 Feb 2018 10:14:59 GMT
Connection: keep-alive
ETag: " 5a8bf523-1f" 
Accept-Ranges: bytes

Now provide cache control headers in your configuration file

[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 no-cache 
    add_header Cache-Control must-revalidate 
         }
}

Now Check the syntax and restart your nginx service.

[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

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

Execute the following Curl command

[root@linuxhelp conf.d]# curl -I http://www.linuxhelp1.com/web1.html
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Tue, 20 Feb 2018 10:26:28 GMT
Content-Type: text/html
Content-Length: 31
Last-Modified: Tue, 20 Feb 2018 10:14:59 GMT
Connection: keep-alive
ETag: " 5a8bf523-1f" 
Cache-Control: no-cache
Cache-Control: must-revalidate
Accept-Ranges: bytes

To perform verification open it in the web browser and also open access logs in one terminal

[root@linuxhelp conf.d]#  tailf /var/log/nginx/access.log

Navigate the following URL in the browser http://www.linuxhelp1.com/web1.html
browser_URL

Next switch to your access logs again and verify
access_log

Http status code is 200 for the first request towards http://www.linuxhelp1.com/web1.html When a client refreshes in his/her browser and if there is no modification in the original data on the application it returns 304 status code which states that “ no modifications”

Refreshing the browser
refresh_browser

Now if you verify access logs, you will see 304 http status code
304_status

Now modifying the file (web1.html)

[root@linuxhelp conf.d]# cd /usr/share/nginx/html

[root@linuxhelp html]# vim web1.html
< h1> 
Application 2 // modify changes
< /h1> 

Let' s Restart the nginx service

[root@linuxhelp html]# systemctl nginx restart

Now the content has been modified so you will be able to see access log with 200 status
Firrefreshrsh the in browser
refresh_browser
browser_refresh

If the client browser refreshes the data again which shows 304 not modified status code again.
304_modified

with this, the method to Cache-Control Headers in nginx - Part 4 (no-cache and must-revalidate headers ) comes to an end.

Tags:
ryan
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

Q

where i can find the part 3 of Cache Control Headers in nginx ?

A

You can find the part 3 of Cache Control Headers in nginx here
https://www.linuxhelpbkp.revyy.com/how-to-cache-control-headers-in-nginx-part-3/

Q

What is Cache-Control?

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

Whewre i can find the official official docs for caching in Ngnix

A

You can find the official official docs for caching in Ngnix here

https://www.nginx.com/blog/nginx-caching-guide/

Q

What are the Cache-Control Header types?

A

The Cache-Control Header types are
Cache-control : no-store
Cache-control: no-cache
Cache-control : max-age=0
Cache-control : s-maxage=0
Cache-control : must-revalidate
Pragma: no-cache

Q

How to check the status of syntax from terminal ?

A

To check the status of syntax from terminal use the following command

#nginx -t

Back To Top!
Rank
User
Points

Top Contributers

userNamenaveelansari
135850

Top Contributers

userNameayanbhatti
92510

Top Contributers

userNamehamzaahmed
32150

Top Contributers

1
userNamelinuxhelp
31040

Top Contributers

userNamemuhammadali
24500
Can you help Elijah ?
Remote Desktop Connection Has Stopped Working

When accessing my remote machine server using remote desktop on a windows machine I am getting this error

forum (1)

Networking
  • Routing
  • trunk
  • Netmask
  • Packet Capture
  • domain
  • HTTP Proxy
Server Setup
  • NFS
  • KVM
  • Memory
  • Sendmail
  • WebDAV
  • LXC
Shell Commands
  • Cloud commander
  • Command line archive tools
  • last command
  • Shell
  • terminal
  • Throttle
Desktop Application
  • Linux app
  • Pithos
  • Retrospect
  • Scribe
  • TortoiseHg
  • 4Images
Monitoring Tool
  • Monit
  • Apache Server Monitoring
  • EtherApe 
  • Arpwatch Tool
  • Auditd
  • Barman
Web Application
  • Nutch
  • Amazon VPC
  • FarmWarDeployer
  • Rukovoditel
  • Mirror site
  • Chef
Contact Us | Terms of Use| Privacy Policy| Disclaimer
© 2025 LinuxHelp.com All rights reserved. Linux™ is the registered trademark of Linus Torvalds. This site is not affiliated with linus torvalds in any way.