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

Solution to the automatic termination of docker run container

Today I encountered a problem when I used Dockerf...

Node.js returns different data according to different request paths.

Table of contents 1. Learn to return different da...

Docker+nacos+seata1.3.0 installation and usage configuration tutorial

I spent a day on it before this. Although Seata i...

Analysis of the Principle of MySQL Index Length Limit

This article mainly introduces the analysis of th...

HTML table tag tutorial (13): internal border style attributes RULES

RULES can be used to control the style of the int...

How to use yum to configure lnmp environment in CentOS7.6 system

1. Installation version details Server: MariaDB S...

Docker file storage path, modify port mapping operation mode

How to get the container startup command The cont...

The latest version of MySQL5.7.19 decompression version installation guide

MySQL version: MySQL Community Edition (GPL) ----...

Install Docker on CentOS 7

If you don't have a Linux system, please refe...

How to open a page in an iframe

Solution: Just set the link's target attribute...

Vue installation and use

Table of contents 1. Vue installation Method 1: C...

How to connect JDBC to MySQL 5.7

1. First prepare the MySQL and Eclipse environmen...

WeChat applet realizes left-right linkage

This article shares the specific code for WeChat ...

Implementing a puzzle game with js

This article shares the specific code of js to im...