Nginx set JS, CSS Expiration Time method

Source: Internet
Author: User
Tags current time

  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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.