静态资源web服务
静态资源
定义
非服务器动态生成的文件
![](https://img.haomeiwen.com/i4788137/e0d6dada00674a5b.png)
![](https://img.haomeiwen.com/i4788137/bdb50a8646b8c489.png)
静态资源服务场景-CDN
![](https://img.haomeiwen.com/i4788137/ec15d2d46867c5ff.png)
文件读取配置
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的压缩方式
配置截图
![](https://img.haomeiwen.com/i4788137/cebbc074d620a447.png)
浏览器缓存
http协议定义的缓存机制(如:expires,cache-control 等)
- 浏览器无缓存
![](https://img.haomeiwen.com/i4788137/ee5e1eae10ac3db3.png)
- 浏览器有缓存
![](https://img.haomeiwen.com/i4788137/061c07f1e00a8c99.png)
检测过期机制
作用 | 请求头 |
---|---|
检验是否过期 | expires, cache-control (max-age) |
协议中Etag头信息校验 | etag |
last-modified 头信息校验 | last-modified |
** 浏览器请求服务器过程(缓存版本)**
![](https://img.haomeiwen.com/i4788137/3f24d981c55303e6.png)
相关配置
expires
添加cache-control、expires头
syntax: expires [modified] time;
expires epoch | max | off;
default: expires off;
context:http, server, location
配置例子
![](https://img.haomeiwen.com/i4788137/701445db3ad43a53.png)
跨域访问
![](https://img.haomeiwen.com/i4788137/71eb5ebef97998bd.png)
为什么浏览器禁止跨域访问
不安全,容易出现CSRF攻击
![](https://img.haomeiwen.com/i4788137/fa70f18afa71e1ae.png)
nginx配置
syntax: add_header name value [always]
default: -
context:http, server, location, if in location
添加请求头:Access-Control-Allow-Origin
配置截图
![](https://img.haomeiwen.com/i4788137/9f15d97f6faa1f06.png)
防盗链
目的
防止资源被盗用
** 防盗链设置思路**
首要方式:区别哪些请求是非正常的用户请求
基于http_refer防盗链配置模块
syntax: valid_referers none | blocked | server_names | string...;
default: -
context:server, location
配置截图
![](https://img.haomeiwen.com/i4788137/2b24d29d0a343e2e.png)
none:表示如果没带refer blocked:代表不是标准的http写过过来的
一个命令
curl -e "http://www.baidu.com" -I http://116.62.103.228/wei.png
-e:表示refer -i:表示只显示请求头
代理服务
代理-代为办理(代理理财、代理收货等)
![](https://img.haomeiwen.com/i4788137/2c6cd5ea45cf43a4.png)
代理服务
![](https://img.haomeiwen.com/i4788137/2d30f402b14e8aa6.png)
代理区别
区别在于代理的对象不一样
- 正向代理的对象是客户端
- 反向代理代理的是服务器
正向代理
![](https://img.haomeiwen.com/i4788137/d219ddb25e95e6e7.png)
反向代理
![](https://img.haomeiwen.com/i4788137/7c68bd51eacf5ce7.png)
代理的配置
syntax: proxy_pass URL;
default: -
context:location, if in location, limit_except
url一般为:
反向代理配置截图
![](https://img.haomeiwen.com/i4788137/fda51f3a10d745f9.png)
想访问8080,只能访问到80,通过80然后通过反向代理可以访问到8080
正向代理配置截图
![](https://img.haomeiwen.com/i4788137/4affab64a8a09278.png)
116.62.103.228的配置如下(其实和反向代理配置参不多)
![](https://img.haomeiwen.com/i4788137/205ae8e51807f32e.png)
客户端配置
![](https://img.haomeiwen.com/i4788137/4f2a16c1b724234e.png)
缓存区配置
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
总的配置
![](https://img.haomeiwen.com/i4788137/37c00b28fafc8b6c.png)
负载均衡调度器 SLB
nginx负载均衡
![](https://img.haomeiwen.com/i4788137/33249546fc8ec2c7.png)
GSLB
GSLB 是英文Global Server Load Balance的缩写,意思是全局负载均衡。作用:实现在广域网(包括互联网)上不同地域的服务器间的流量调配,保证使用最佳的服务器服务离自己最近的客户,从而确保访问质量。
![](https://img.haomeiwen.com/i4788137/54d1afeef839c823.png)
SLB
负载均衡(Server Load Balancer,简称SLB)是一种网络负载均衡服务,针对阿里云弹性计算平台而设计,在系统架构、系统安全及性能,扩展,兼容性设计上都充分考虑了弹性计算平台云服务器使用特点和特定的业务场景。
4层负载均衡
![](https://img.haomeiwen.com/i4788137/2a851516b5858a03.png)
在iso模型中的传输层(包的转发)
7层负载均衡
![](https://img.haomeiwen.com/i4788137/5da39aa561b52511.png)
在应用层实现
nginx实现的负载均衡(7层)
![](https://img.haomeiwen.com/i4788137/5558bf5f1b83af62.png)
配置
syntax: upstream name{...}
default: -
context:http
配置截图
![](https://img.haomeiwen.com/i4788137/7cd7477c26104804.png)
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 | 限制最大的接收的连接数 |
配置截图
![](https://img.haomeiwen.com/i4788137/8c0d7e63b4ee5c17.png)
调度算法
字段 | 作用 |
---|---|
轮询 | 按时间顺序逐一分配到不同的后端服务器 |
加权轮询 | weight值越大,分配到的访问几率越高 |
ip_hash | 每个请求按访问ip的hash结果分配,这样来自同一个ip的固定访问一个后端服务器 |
least_conn | 最少链接数,那个机器连接数少就分发 |
url_hash | 按照访问的url的hash结果来分配请求,是每个url定向到同一个后端服务器 |
hash关键值 | hash自定义的key |
iphash
![](https://img.haomeiwen.com/i4788137/e79c25f83f8edcb8.png)
url——hash
![](https://img.haomeiwen.com/i4788137/9560be2287a27d01.png)
配置语法
url_hash
syntax: hash key [consistent];
default:-
context:upstream
this directive appeared in version 1.7.2
配置截图
![](https://img.haomeiwen.com/i4788137/56cc2481f9e7d6ec.png)
动态缓存
缓存的类型
![](https://img.haomeiwen.com/i4788137/26b0f6e1658eece9.png)
代理缓存
![](https://img.haomeiwen.com/i4788137/e61838eef958d04f.png)
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
配置截图
![](https://img.haomeiwen.com/i4788137/0b33f9c132957418.png)
level:目录分级 inactive:不活跃就清理
如何清理指定缓存
- rm-rf缓存目录内容
- 第三方扩展模块ngx_cache_purge
如何让部分页面不缓存
syntax : proxy_no_cache string ...;
default: -;
context:http,server,location
配置截图
![](https://img.haomeiwen.com/i4788137/432ffda67b0442cf.png)
大文件分片请求
syntax : slice size
default: slice o
context:http,server,location
http_slice_module
![](https://img.haomeiwen.com/i4788137/daede5e28fcd6e97.png)
优势
- 每个子请求收到的数据都会形成一个独立的文件,一个请求断了,其它请求不受到影响
缺点
- 当文件很大或者slice很小的时候, 可能会导致文件描述符耗尽等情况。
网友评论