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

JavaScript Basics Variables

Table of contents 1. Variable Overview 1.1 Storag...

Tutorial on building an FTP server in Ubuntu 16.04

Ubuntu 16.04 builds FTP server Install ftp Instal...

Native JavaScript to achieve the effect of carousel

This article shares the specific code for JavaScr...

How to convert extra text into ellipsis in HTML

If you want to display extra text as ellipsis in ...

HTML table cross-row and cross-column operations (rowspan, colspan)

Generally, the colspan attribute of the <td>...

How to deploy kafka in docker

Table of contents 1. Build Docker 2. Enter the co...

Basic usage details of Vue componentization

Table of contents 1. What is componentization? 2....

Summary of 16 XHTML1.0 and HTML Compatibility Guidelines

1. Avoid declaring the page as XML type . The pag...

The simplest form implementation of Flexbox layout

Flexible layout (Flexbox) is becoming increasingl...

The implementation process of long pressing to identify QR code in WeChat applet

Preface We all know that the QR codes in official...

How to build a Vue3 desktop application

In this article, we will look at how to develop a...

Detailed explanation of MySQL database binlog cleanup command

Overview Today I will mainly share how to correct...

MySQL foreign key constraint (FOREIGN KEY) case explanation

MySQL foreign key constraint (FOREIGN KEY) is a s...

How to maintain MySQL indexes and data tables

Table of contents Find and fix table conflicts Up...