1. 静态资源类型

非服务器动态生成的文件,包括

  • 浏览器端渲染 html, css, js
  • 图片 jpeg, gif, png
  • 视频 flv mpeg
  • 文件 txt等任意下载文件

    2. 静态资源服务场景

    image.png

    2.1 cdn

    下面介绍基本的配置语法基本配置语法

    2.1.1 文件读取

    1. syntax: sendfile on|off;
    2. default: sendfile off;
    3. context: http, server, location, if in location

2.1.2 tcp_nopush

sendfile开启的情况下,提高网路包的传输效率

  1. syntax: tcp_nopush on|off;
  2. default: tcp_nopush off;
  3. context:http, server, location

2.1.3 tcp_nodelay

keepalive连接下,提高网络包的传输实时性

  1. syntax: tcp_nodelay on|off;
  2. default: tcp_nodelay off;
  3. context:http, server, location

2.2 压缩

2.2.1 配置语法-压缩

压缩传输

  1. syntax: gzip on|off
  2. default: gzip off;
  3. context: http, server, location, if in location

压缩比率()

  1. syntax: gzip_comp_level level;
  2. default: gzip_comp_level 1;
  3. context:http, server, location

2.2.2 扩展nginx压缩模块

http_gzip_static_module 预读gzip功能
http_gunzip_module 应用支持gunzip的压缩方式

2.3 浏览器缓存

http协议定义的缓存机制(expires, cache-control)

  • 浏览器无缓存

image.png

  • 浏览器有缓存

image.png

2.3.1 校验过期机制

校验是否过期 Expires, Cache-Control(max-age)
协议中etag头信息校验 Etag
Last-modified头信息校验 Last-modified
image.png

2.3.3 配置语法

  • expires

添加cache-control、expires头

  1. syntax: expires [modified] time;
  2. expires epoch| max | off;
  3. default: expires off;
  4. context: http, server, location, if in location

2.4 跨站访问

浏览器禁止跨域访问,容易出现csrf攻击
如何配置,浏览器端会读取access-control-allow-origin

  1. syntax: add_header name value [always];
  2. default: -
  3. context: http, server, location, if in location

2.5 防止盗链

防止资源被盗用

2.5.1 防盗链设置思路

首要方式:区别哪些请求是非正常的用户请求

2.5.2 基于http_refer防盗链配置模块

  1. syntax: valid_referers none| blocked | server_names | string..
  2. default: -
  3. context: http, server, location