Explains the simple way to set up a local browser cache in a Nginx server _nginx

Source: Internet
Author: User
Tags browser cache nginx server

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.

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.