静态资源web服务
静态资源
定义
非服务器动态生成的文件


静态资源服务场景-CDN

文件读取配置
sendfile
syntax: sendfile on | off;
default:sendfile off
context:http,server,location,if in location
注
--with-file-aio异步文件读取
tcp_nopush
作用:sendfile 开启的情况下,提高网络包的传输效率(等待,一次传输)
syntax:tcp_nopush on | off
default:tcp_nopush off
context:http, server, location
相反的
syntax:tcp_nodelay on | off
default:tcp_nodelay on
context:http, server, location
作用 在keepalive连接下,提高网络包的传输实时性
压缩
作用
压缩传输
syntax:gzip on | off
default:gzip off
context:http, server, if in location
syntax:gzip_comp_level level;
default:gzip_comp_level 1;
context:http, server, location
扩展nginx压缩模块
- http_gzip_static_module:预读gzip功能
- http_gunzip_module: 应用支持gunzip的压缩方式
配置截图

浏览器缓存
http协议定义的缓存机制(如:expires,cache-control 等)
- 浏览器无缓存

- 浏览器有缓存

检测过期机制
作用 | 请求头 |
---|---|
检验是否过期 | expires, cache-control (max-age) |
协议中Etag头信息校验 | etag |
last-modified 头信息校验 | last-modified |
** 浏览器请求服务器过程(缓存版本)**

相关配置
expires
添加cache-control、expires头
syntax: expires [modified] time;
expires epoch | max | off;
default: expires off;
context:http, server, location
配置例子

跨域访问

为什么浏览器禁止跨域访问
不安全,容易出现CSRF攻击

nginx配置
syntax: add_header name value [always]
default: -
context:http, server, location, if in location
添加请求头:Access-Control-Allow-Origin
配置截图

防盗链
目的
防止资源被盗用
** 防盗链设置思路**
首要方式:区别哪些请求是非正常的用户请求
基于http_refer防盗链配置模块
syntax: valid_referers none | blocked | server_names | string...;
default: -
context:server, location
配置截图

none:表示如果没带refer blocked:代表不是标准的http写过过来的
一个命令
curl -e "http://www.baidu.com" -I http://116.62.103.228/wei.png
-e:表示refer -i:表示只显示请求头
代理服务
代理-代为办理(代理理财、代理收货等)

代理服务

代理区别
区别在于代理的对象不一样
- 正向代理的对象是客户端
- 反向代理代理的是服务器
正向代理

反向代理

代理的配置
syntax: proxy_pass URL;
default: -
context:location, if in location, limit_except
url一般为:
反向代理配置截图

想访问8080,只能访问到80,通过80然后通过反向代理可以访问到8080
正向代理配置截图

116.62.103.228的配置如下(其实和反向代理配置参不多)

客户端配置

缓存区配置
syntax: proxy_buffering on | off
default: proxy_buffering on
context:location,http,server
扩展
- proxy_buffer_size
- proxy_buffers
- proxy_busy_buffers size
跳转重定向配置
syntax: proxy_redirect default;proxy_redirect off;proxy_redirect redirect replacement;
default: proxy_redirect default;
context:location,http,server
头信息配置
syntax: proxy_set_header field value;
default: proxy_set_header host $proxy_host
proxy_set_header connection close;
context:location,http,server
超时配置
syntax: proxy_connect_timeout time;
default: proxy_connect_timeout 60s;
context:location,http,server
扩展
- proxy_read_timeout
- proxy_send_timeout
总的配置

负载均衡调度器 SLB
nginx负载均衡

GSLB
GSLB 是英文Global Server Load Balance的缩写,意思是全局负载均衡。作用:实现在广域网(包括互联网)上不同地域的服务器间的流量调配,保证使用最佳的服务器服务离自己最近的客户,从而确保访问质量。

SLB
负载均衡(Server Load Balancer,简称SLB)是一种网络负载均衡服务,针对阿里云弹性计算平台而设计,在系统架构、系统安全及性能,扩展,兼容性设计上都充分考虑了弹性计算平台云服务器使用特点和特定的业务场景。
4层负载均衡

在iso模型中的传输层(包的转发)
7层负载均衡

在应用层实现
nginx实现的负载均衡(7层)

配置
syntax: upstream name{...}
default: -
context:http
配置截图

upstream举例
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com:8080;
server unix:/tmp/backend3;
server backup1.exmple.com:8080 backup;
server backup2.example.com:8080 backup;
}
后端服务器在负载均衡调度中的状态
字段 | 作用 |
---|---|
down | 当前的server暂时不参与负载均衡 |
backup | 预留的备份服务器 |
max_fails | 允许请求失败的次数 |
fail_timeout | 经过max_fails失败后,服务暂停的时间 |
max_conns | 限制最大的接收的连接数 |
配置截图

调度算法
字段 | 作用 |
---|---|
轮询 | 按时间顺序逐一分配到不同的后端服务器 |
加权轮询 | weight值越大,分配到的访问几率越高 |
ip_hash | 每个请求按访问ip的hash结果分配,这样来自同一个ip的固定访问一个后端服务器 |
least_conn | 最少链接数,那个机器连接数少就分发 |
url_hash | 按照访问的url的hash结果来分配请求,是每个url定向到同一个后端服务器 |
hash关键值 | hash自定义的key |
iphash

url——hash

配置语法
url_hash
syntax: hash key [consistent];
default:-
context:upstream
this directive appeared in version 1.7.2
配置截图

动态缓存
缓存的类型

代理缓存

proxy_cache配置语法
Syntax: proxy_cache_path path [levels=levels]
Default:-
context:http
开关
syntax: proxy_cache zone | off;
default:proxy_cache off;
context:http, sercer, location
过期周期
syntax: proxy_cache_valid[code] time;
default:-
context:http, sercer, location
缓存的维度
syntax: proxy_cache_key string;
default: proxy_cache_key $scheme$proxy_host$request_uri;
context:http,server,location
配置截图

level:目录分级 inactive:不活跃就清理
如何清理指定缓存
- rm-rf缓存目录内容
- 第三方扩展模块ngx_cache_purge
如何让部分页面不缓存
syntax : proxy_no_cache string ...;
default: -;
context:http,server,location
配置截图

大文件分片请求
syntax : slice size
default: slice o
context:http,server,location
http_slice_module

优势
- 每个子请求收到的数据都会形成一个独立的文件,一个请求断了,其它请求不受到影响
缺点
- 当文件很大或者slice很小的时候, 可能会导致文件描述符耗尽等情况。
网友评论