Nginx作为静态资源web服务_浏览器缓存场景演示

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/longgeqiaojie304/article/details/85041856

Nginx作为静态资源web服务_浏览器缓存场景演示

1、expires过期时间语法

Nginx在响应头中添加:Cache-Control、Expires头

Syntax:

expires [modified] time;
expires epoch | max | off;

Default:

expires off;

Context:

http, server, location, if in location

语法解释:

The time in the “Expires” field is computed as a sum of the current time and time specified in the directive. If the modified parameter is used (0.7.0, 0.6.32) then the time is computed as a sum of the file’s modification time and the time specified in the directive.

In addition, it is possible to specify a time of day using the “@” prefix (0.7.9, 0.6.34):

        expires @15h30m;

The epoch parameter corresponds to the absolute time “Thu, 01 Jan 1970 00:00:01 GMT”. The contents of the “Cache-Control” field depends on the sign of the specified time:

扫描二维码关注公众号,回复: 4543188 查看本文章
  • time is negative — “Cache-Control: no-cache”.
  • time is positive or zero — “Cache-Control: max-age=t”, where t is a time specified in the directive, in seconds.

 

The max parameter sets “Expires” to the value “Thu, 31 Dec 2037 23:55:55 GMT”, and “Cache-Control” to 10 years.

The off parameter disables adding or modifying the “Expires” and “Cache-Control” response header fields.

The last parameter value can contain variables (1.7.9):

map $sent_http_content_type $expires {
    default         off;
    application/pdf 42d;
    ~image/         max;
}
expires $expires;

2、nginx没有配置expires场景

(1)Nginx配置location

        
(2)创建新的html文件

        在/opt/app/html目录下创建html文件并编写内容

        

        

(3)验证Nginx配置的location是否生效

温馨提示:每次验证前,记得清下浏览器缓存。

可以访问新配置的location

       第一次请求:查看请求头和响应头信息

 

第二次请求:查看请求头和响应头信息

客户端请求头携带这两个字段,目的是跟服务端本地缓存文件对比

If-Modified-Since:

Thu, 13 Dec 2018 13:23:19 GMT

If-None-Match:

"5c125d47-6a"

3、Nginx配置expires场景

(1)Nginx配置location

 

(2)验证Nginx配置的location是否生效

       温馨提示:每次验证前,记得清下浏览器缓存。

       第一次请求:查看请求头和响应头信息

 

响应头多了两个字段:

Cache-Control:

max-age=30

Expires:

Thu, 13 Dec 2018 13:42:57 GMT

 

请求头没有啥变化

第二次请求:查看请求头和响应头信息

 

响应头中Cache-Control字段在第一次和第二次的值都一样

Cache-Control:

max-age=30

响应头中Expires字段值会在每次请求之后30s

Expires:

Thu, 13 Dec 2018 13:47:14 GMT

 

不管Nginx是否配置expires过期时间,第二次请求都会携带这两个字段

If-Modified-Since:

Thu, 13 Dec 2018 13:23:19 GMT

If-None-Match:

"5c125d47-6a"

 

猜你喜欢

转载自blog.csdn.net/longgeqiaojie304/article/details/85041856