1. 静态资源类型
非服务器动态生成的文件,包括
- 浏览器端渲染 html, css, js
- 图片 jpeg, gif, png
- 视频 flv mpeg
- 文件 txt等任意下载文件
2. 静态资源服务场景
2.1 cdn
下面介绍基本的配置语法基本配置语法2.1.1 文件读取
syntax: sendfile on|off;
default: sendfile off;
context: http, server, location, if in location
2.1.2 tcp_nopush
sendfile开启的情况下,提高网路包的传输效率
syntax: tcp_nopush on|off;
default: tcp_nopush off;
context:http, server, location
2.1.3 tcp_nodelay
keepalive连接下,提高网络包的传输实时性
syntax: tcp_nodelay on|off;
default: tcp_nodelay off;
context:http, server, location
2.2 压缩
2.2.1 配置语法-压缩
压缩传输
syntax: gzip on|off
default: gzip off;
context: http, server, location, if in location
压缩比率()
syntax: gzip_comp_level level;
default: gzip_comp_level 1;
context:http, server, location
2.2.2 扩展nginx压缩模块
http_gzip_static_module 预读gzip功能
http_gunzip_module 应用支持gunzip的压缩方式
2.3 浏览器缓存
http协议定义的缓存机制(expires, cache-control)
- 浏览器无缓存
- 浏览器有缓存
2.3.1 校验过期机制
校验是否过期 Expires, Cache-Control(max-age)
协议中etag头信息校验 Etag
Last-modified头信息校验 Last-modified
2.3.3 配置语法
- expires
添加cache-control、expires头
syntax: expires [modified] time;
expires epoch| max | off;
default: expires off;
context: http, server, location, if in location
2.4 跨站访问
浏览器禁止跨域访问,容易出现csrf攻击
如何配置,浏览器端会读取access-control-allow-origin
syntax: add_header name value [always];
default: -
context: http, server, location, if in location
2.5 防止盗链
2.5.1 防盗链设置思路
2.5.2 基于http_refer防盗链配置模块
syntax: valid_referers none| blocked | server_names | string..
default: -
context: http, server, location