Browser caching (Browser Caching) is designed to speed browsing and save network resources, where browsers store recently requested documents on a user's disk.
Nginx can set the header of the browser via the expires directive
Syntax: Expires [Time|epoch|max|off]
Default value: Expires off
Scopes: HTTP, server, location
Use this directive to control the headers of the "Expires" and "Cache-control" in the HTTP answer (play a role in controlling page caching).
You can use a positive or negative number in a time value. The value of the "Expires" header will be obtained by the current system time plus the value you set.
EPOCH specifies that the value of "Expires" is 1 January, 1970, 00:00:01 GMT.
MAX Specifies the value of "Expires" to December 2037 23:59:59 GMT, and the value of "Cache-control" is 10 years.
-1 specifies that the value of "Expires" is the server's current time -1s, which expires forever
Example:
Picture Cache 30 Days
Location ~.*\. (jpg|png|jpeg) $
{
expires 30d;
}
JS CSS Cache one hour
Location ~.*\. (JS|CSS)? $
{
expires 1h;
}
The header information cached by the browser can also be set in Nginx, as follows.
#配置在server节点下
#将favicon. ico is placed in the HTML directory of the Nginx installation directory
location =/favicon.ico {
root html;
Index index.html index.htm;
Expires 30d; #会同时设置Expires and Cache-control:max-age
add_header Pragma Pragma;
}
After the settings are set, the request is viewed through the browser.
Increased the
cache-control:max-age=2592000
Expires:sun, Nov 2015 01:07:48 GMT
The cache is in effect.
Attention matters
There are several issues to be aware of when debugging:
Four types of user behavior Impact caching: F5 refresh, F5 Force refresh, click Refresh, Address bar return. will make expires ineffective.