• 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

How to Cache-Control in Nginx

{{postValue.id}}

To Cache-Control in Nginx

Caching is   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 needs to be followed by caching servers either be it in 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.

 

To Cache

First, open your configuration file by making use of the following command. 

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

 

Add the following configuration in the file. 

server {
   server_name www.linuxhelp1.com 
   location / {
    root /usr/share/nginx/html 
    index index.html index.htm 
         }
    location ~.(jpg) {
    root /usr/share/nginx/html 
     }
}

Save and exit the configuration file once it is completed. 

 

Now, you need to check for the syntax.

[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

 

You should now restart your nginx service as follows. 

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

 

And then, you need to execute curl command.   You will notice that there is no information related to Cache-control in the output. 

[root@linuxhelp conf.d]# curl -I http://www.linuxhelp1.com/index1.jpg
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Mon, 05 Feb 2018 12:29:17 GMT
Content-Type: image/jpeg
Content-Length: 12871
Last-Modified: Mon, 05 Feb 2018 11:59:55 GMT
Connection: keep-alive
ETag: " 5a78473b-3247" 
Accept-Ranges: bytes

 

So, you need to open your configuration file again. 

[root@linuxhelp conf.d]# vim vir.conf

 

Now add expire header 48h which by default adds cache-control header also for that add the following. 

server {
   server_name www.linuxhelp1.com 
   location / {
    root /usr/share/nginx/html 
    index index.html index.htm 
         }
    location ~.(jpg) {
    root /usr/share/nginx/html 
    expires 48h 
     }
}

Save and exit the configuration file

 

Once again restart your nginx service. 

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

 

And execute the curl command to check for Cache-Control. 

[root@linuxhelp conf.d]# curl -I http://www.linuxhelp1.com/index1.jpg
HTTP/1.1 200 OK
Server: nginx/1.12.2
Date: Mon, 05 Feb 2018 12:31:19 GMT
Content-Type: image/jpeg
Content-Length: 12871
Last-Modified: Mon, 05 Feb 2018 11:59:55 GMT
Connection: keep-alive
ETag: " 5a78473b-3247" 
Expires: Wed, 07 Feb 2018 12:31:19 GMT
Cache-Control: max-age=172800
Accept-Ranges: bytes


You will now note that the Cache-Control is visible. 

Tags:
noah
Author: 

Comments ( 0 )

No comments available

Add a comment
{{postCtrl.cmtErrMsg}}

Frequently asked questions ( 5 )

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

where to get the official docs for caching on nginx?

A

Please refer the link as follows to get the official docs

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

Q

how to check nginx configuration is correct or not ?

A

use the following command to check nginx configuration

# nginx -t

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

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 Ganesh Konka ?
Zentya 6.1 http proxy configuration

please send link for creating zentyal 6.1 for http proxy and firewall as gateway.

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.