Nginx How to set the expiration time of files such as JS, CSS, etc.
First open the configuration file/etc/nginx/nginx.conf, and then find the following sentence:
location~. *. (gif|jpg|png|htm|html|css|js|flv|ico|swf) (.*) {
Expires 30d;
}
Set expires this parameter, 30d means 30 days, note that there is a semicolon ";".
If the expiration time is an hour, use "H", such as "Expires 1h;" Indicates that the file cache expires after 1 hours. Take a look at the following example:
#图片缓存30天
Location ~.*. (JPG|PNG|JPEG) $
{
Expires 30d;
}
#js CSS Cache one hour
Location ~.*. (JS|CSS)? $
{
Expires 1h;
}
Grammar explanation
Nginx as a Web server, there are times when browser caching (Browser Caching) is needed to speed browsing and conserve network resources, where browsers store recently requested documents on a user's disk.
Nginx 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 current time of the server; -1s, which means that the file is never cached.
How do I detect if a page expiration setting takes effect?
Open Webkaka Web Site Speed Diagnostics tool (online test tool URL below), enter your Web address, after detection, immediately can see if the settings are effective.
Detect if the page cache is in effect
The figure above, the detected Web page JS, CSS file expiration time is 1 days.