Nginx cache configuration example

Nginx cache configuration example

When developing and debugging a web application, you will often encounter the trouble of having to clear the browser cache or force a refresh to test it. Here are some settings for Apache no-cache configuration and nginx no-cache configuration. There are two common cache settings, both of which are set using add_header: Cache-Control and Pragma.

nginx:
location ~ .*\.(css|js|swf|php|htm|html )$ {
add_header Cache-Control no-store;add_header Pragma no-cache;
}

For static content that is not frequently modified on the site (such as images, JS, and CSS), you can set an expiration time on the server to control browser cache, thereby effectively reducing bandwidth traffic and reducing server pressure.

Take Nginx server as an example:

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
#The expiration time is 30 days.
#Image files are rarely updated, so you can set a larger value when they expire.
#If you update frequently, you can set it smaller.
expires 30d;
}
location ~ .*\.(js|css)$ {
expires 10d;
}

[ Background ]: Expires is a header field in the Web server response message. When responding to an http request, it tells the browser that the browser can directly retrieve data from the browser cache before the expiration time without requesting again.

Related information

1. Cache-control strategy

Cache-Control and Expires have the same function, both of which indicate the validity period of the current resource and control whether the browser directly retrieves data from the browser cache or resends a request to the server to retrieve data. It’s just that Cache-Control has more options and more detailed settings. If set at the same time, its priority is higher than Expires.

HTTP protocol header Cache-Control :

The value can be public, private, no-cache, no-store, no-transform, must-revalidate, proxy-revalidate, max-age

The meanings of the instructions in each message are as follows:

  • Public indicates that the response can be cached by any cache.
  • Private indicates that the entire or partial response message for a single user cannot be processed by the shared cache. This allows the server to describe only part of the response message to the current user, without having this response message be valid for other users' requests.
  • no-cache indicates that the request or response message cannot be cached
  • no-store is used to prevent important information from being accidentally released. Sending it in a request message will cause both the request and response messages to not use the cache.
  • max-age indicates that the client can accept responses with an age no greater than the specified time (in seconds).
  • min-fresh indicates that the client can receive responses whose response time is less than the current time plus the specified time.
  • max-stale indicates that the client can receive response messages beyond the timeout period. If you specify a max-stale message value, the client can receive response messages that exceed the specified timeout period.

Last-Modified/If-Modified-Since

  • Last-Modified/If-Modified-Since must be used in conjunction with Cache-Control.
  • Last-Modified: Indicates the last modification time of this response resource. When responding to a request, the web server tells the browser when the resource was last modified.
  • If-Modified-Since: When a resource expires (using the max-age flag of Cache-Control), and it is found that the resource has a Last-Modified declaration, the next request to the web server will include the If-Modified-Since header, indicating the request time. After receiving the request, the web server finds the If-Modified-Since header and compares it with the last modification time of the requested resource. If the last modification time is relatively recent, it means that the resource has been modified again, and the entire resource content (written in the response message body) is responded with HTTP 200; if the last modification time is relatively old, it means that the resource has not been modified, and the response is HTTP 304 (no body required, saving browsing time), telling the browser to continue using the saved cache.

What it ultimately achieves is equivalent to setting up these three types of HTML caching technologies:

<meta http-equiv="pragma" content="no-cache"/>
<meta http-equiv="Cache-Control" content="no-cache, must-revalidate"/> 
<meta http-equiv="expires" content="0"/>

This is the end of this article about the detailed case of Nginx cache settings. For more relevant Nginx cache settings, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to hide the version number and web page cache time in Nginx
  • Detailed explanation of setting resource cache in nginx
  • nginx proxy_cache batch cache clearing script introduction
  • How to set up static files on the nginx cache server
  • How to handle Nginx and browser cache

<<:  Detailed explanation of TIMESTAMPDIFF case in MySQL

>>:  vue dynamic component

Recommend

How to hide elements on the Web and their advantages and disadvantages

Example source code: https://codepen.io/shadeed/p...

Summary of important mysql log files

Author: Ding Yi Source: https://chengxuzhixin.com...

Linux five-step build kernel tree

Table of contents 0. The kernel tree that comes w...

Detailed explanation of JavaScript Proxy object

Table of contents 1. What is Proxy? 2. How to use...

Example of converting timestamp to Date in MySQL

Preface I encountered a situation at work: In the...

How to lock a virtual console session on Linux

When you are working on a shared system, you prob...

Detailed explanation of how to write mysql not equal to null and equal to null

1. Table structure 2. Table data 3. The query tea...

Javascript common higher-order functions details

Table of contents 1. Common higher-order function...

Alibaba Cloud Ubuntu 16.04 builds IPSec service

Introduction to IPSec IPSec (Internet Protocol Se...

HTML sample code for implementing tab switching

Tab switching is also a common technology in proj...

How to solve the margin collapse problem in CSS

First, let's look at three situations where m...

Summary of solutions to common Linux problems

1. Connect Centos7 under VMware and set a fixed I...