cache 应用加速
1.功能描述
通常为了节省带宽、以及能够快速获取资源,在中间代理服务器上,通常会配置缓存。缓存机制的基本原理是将 Web 资源(如 HTML、CSS、JavaScript、图像等)保存在客户端或中间代理服务器上,以便在后续请求中直接使用该缓存副本,而不必重新获取资源。当客户端或代理服务器收到对资源的请求时,它们首先检查缓存,如果存在有效的缓存副本,就可以直接返回缓存的副本,从而避免了请求的发送和服务器端的处理过程。
但是上述的缓存机制仍然存在一定的问题,就是第一次访问资源是没有缓存的,所以肯定都是要跟服务器通信,然后去下载资源,如果带宽有限而且资源很大的情况(比如视频文件等),客户端就会长时间处于下载阶段,效率低下。针对这种情况,我们需要实现cache应用加速功能。
cache应用加速,由web管理员提前通过下发配置到代理服务器,由代理服务器提前下载资源并进行缓存,这样当客户端首次访问的时候也能够直接从缓存中获取资源,避免等待。
2.依赖模块
load_module modules/njt_http_sendmsg_module.so; #依赖sendmsg模块
load_module modules/njt_http_cache_quick_module.so; #加载cache加速模块so
3.指令说明
- 需要http层配置proxy_cache_path(zone下面以cache作为示例)
- 需要http层配置map $purge_method (支持通过PURGE方法删除)
proxy_cache_path /data/njet/cache levels=1:2 keys_zone=cache:10m purger=on max_size=20g inactive=30m;
map $request_method $purge_method{
PURGE 1;
default 0;
}
proxy_cache_path参数说明:
参数 | 说明 |
---|---|
/data/njet/cache | 缓存文件存放本地磁盘位置 |
levels=1:2 | 最多三级存放 |
keys_zone=cache:10m | 缓存使用的共享内存名称以及大小 |
purger=on | 是否支持purge清除 |
max_size=20g | 最大 cache 空间 |
inactive=30m | 缓存资源不活跃时间,超过该时间没被访问,该资源就会被清理 |
proxy_cache_valid 指令后面的时间表示最长缓存时间,在满足上面inactive时间内保持访问,也最多到这个最长缓存时间有效 |
4.配置样例
njet.conf
http {
dyn_kv_conf conf/iot-work.conf;
include mime.types;
include conf.d/*.conf;
proxy_cache_path /home/njet/test_cache/cache levels=1:2 keys_zone=cache_quick:10m purger=on max_size=20g inactive=1d;
map $request_method $purge_method{
PURGE 1;
default 0;
}
variables_hash_max_size 2048;
#用于http cache加速测试
server {
listen 80;
proxy_cache_valid any 1d;
expires 1d;
}
#用于https cache加速测试
server {
listen 443 ssl;
proxy_cache_valid any 1h;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
ssl_ciphers AES128-SHA:AES256-SHA:RC4-SHA:DES-CBC3-SHA:RC4-MD5;
ssl_certificate /etc/vsftpd/vsftpd.pem;
ssl_certificate_key /etc/vsftpd/vsftpd.pem;
}
}
njet_ctrl.conf
load_module modules/njt_http_cache_quick_module.so;
http {
dyn_sendmsg_conf conf/iot-ctrl.conf;
access_log logs/access_ctrl.log combined;
include mime.types;
server {
listen 8081;
location /api {
dyn_module_api;
}
location /doc {
doc_api;
}
location /metrics {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
#配置cache加速开关location
location /cache {
cache_quick_api;
}
}
}
示例:在另一台服务器157上配置后端server:
njet.conf:
http {
dyn_kv_conf conf/iot-work.conf;
include mime.types;
include conf.d/*.conf;
server {
listen 8001;
location /aa.log {
root /home/limin/test_cache/download;
add_header Content-Disposition: "attachment";
add_header Content-Type application/octet-stream;
autoindex off;
autoindex_exact_size off;
autoindex_localtime on;
}
}
server {
listen 8003;
location /cc.log {
root /home/limin/test_cache/download;
add_header Content-Disposition: "attachment";
add_header Content-Type application/octet-stream;
autoindex off;
autoindex_exact_size off;
autoindex_localtime on;
}
}
server {
listen 8002 ssl;
# server_name localhost;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
ssl_ciphers AES128-SHA:AES256-SHA:RC4-SHA:DES-CBC3-SHA:RC4-MD5;
ssl_certificate /etc/vsftpd/.sslkey/vsftpd.pem;
ssl_certificate_key /etc/vsftpd/.sslkey/vsftpd.pem;
location /bb.log {
root /home/limin/test_cache/download;
add_header Content-Disposition: "attachment";
add_header Content-Type application/octet-stream;
autoindex off;
autoindex_exact_size off;
autoindex_localtime on;
}
location /aa.log {
root /home/limin/test_cache/download;
add_header Content-Disposition: "attachment";
add_header Content-Type application/octet-stream;
autoindex off;
autoindex_exact_size off;
autoindex_localtime on;
}
}
}
5.api
5.1api 列表
5.1.1 添加删除cache 配置
添加删除接口
PUT http://IP+port/cache
添加cache 配置
- 往特定server下添加特定的资源location,通过调用动态location 添加接口实现 动态location api 添加location格式如下
{
"type": "add",
"server_name": "www.a.com",
"location_name": "/music.mp4",
#代理的server类型与backend_server的类型一致
#http 固定使用80端口
#https 固定使用443端口
"backend_server":"https://192.168.40.157:8090"
}
参数 | 类型 | 必填 | 取值 | 说明 |
---|---|---|---|---|
type | string | 是 | add | |
server_name | string | 否 | 如果有servername必须填 | |
location_name | string | 是 | location 名字 | |
backend_server | string | 是 | http://{ip}:{port} 或者https://{ip}:{port} | #代理的server类型与backend_server的类型一致 #http 固定使用80端口 #https 固定使用443端口 |
删除cache配置与动态删除location接口一致
{
"type": "del",
"server_name": "www.a.com",
#location_name 与backend_server字段要与add cache配置时一致
"location_name": "/music.mp4",
"backend_server":"https://192.168.40.157:8090"
}
参数 | 类型 | 必填 | 取值 | 说明 |
---|---|---|---|---|
type | string | 是 | del | |
location_name | string | 是 | location 名字 | |
backend_server | string | 是 | http://{ip}:{port} 或者https://{ip}:{port} |
5.1.2 查询接口
GET' http://IP+port//api/v1/cache
5.2 调用样例
5.2.1 添加cache 配置
curl -X 'PUT' \
'http://192.168.40.158:8081/api/v1/cache' \
-H 'accept: */*' \
-H 'Content-Type: application/json' \
-d '{
"type": "add",
"location_name": "/aa.log",
"backend_server":"https://192.168.40.157:8002"
}'
5.2.3删除cache 配置
curl -X 'PUT' \
'http://192.168.40.158:8081/api/v1/cache' \
-H 'accept: */*' \
-H 'Content-Type: application/json' \
-d '{
"type": "del",
"location_name": "/aa.log",
"backend_server":"https://192.168.40.157:8002"
}'
5.2.4查询cache 配置
curl -X 'GET' 'http://192.168.40.158:8081/api/v1/cache' |jq