2、代理缓存
3、客户端缓存
代理缓存的原理:
upstream imooc {
server 192.168.11.135:8001;
server 192.168.11.135:8002;
server 192.168.11.135:8003;
}
proxy_cache_path /etc/nginx/cache levels=1:2 keys_zone=imooc_cache:10m max_size=10g inactive=60m use_temp_path=off;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
proxy_cache imooc_cache;
proxy_pass http://imooc;
proxy_cache_valid 200 304 12h;
proxy_cache_valid any 10m;
proxy_cache_key $host$uri$is_args$args;
add_header Nginx-Cache "$upstream_cache_status";
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
}
}
server {
listen 8001;
server_name 127.0.0.1;
location / {
root /home/testzq/app/code1;
index index.html;
}
}
# /home/testzq/app/code1/index.html
第二台: # /etc/nginx/conf.d/server2.confserver 1 server 1
server {
listen 8002;
server_name 127.0.0.1;
location / {
root /home/testzq/app/code2;
index index.html;
}
}
# /home/testzq/app/code2/index.html
第三台: # /etc/nginx/conf.d/server3.confserver 2 server 2
server {
listen 8003;
server_name 127.0.0.1;
location / {
root /home/testzq/app/code3;
index index.html;
}
}
# /home/testzq/app/code3/index.html
重启nginx:server 3 server 3
nginx -t -c /etc/nginx/nginx.conf # 测试配置文件语法 nginx -s reload -c /etc/nginx/nginx.conf # 重新加载配置项>>>先在代理服务器中将缓存关闭 (proxy_cache off),刷新页面,发现页面可以在三个站点间轮询显示:
>>>然后在把代理缓存打开,发现页面不在轮询了,请求头多了缓存头(这头是我们配置的):
同时也会在我们配置的缓存目录(
/etc/nginx/cache)生成缓存目录:
缓存的内容如下:
如何让部分页面不缓存:
proxy_no_cache string ...;
默认:-
配置块:http、server、location
比如:这里配置的意思就是当url中匹配到了 index.html , login, register, password 和 reset 时,不缓存该url所对应的页面
方式2:
通过设置 log_format,打印日志进行分析。(打印 $upstream_cache_status 这个Nginx默认的变量)
$upstream_cache_status 这个变量有以下几种值:
缓存命中率 = HIT次数 / 总请求次数
1、首先在 /etc/nginx/nginx.conf 中的 log_format 中加入 $upstream_cache_status 这个变量:
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" ' '"$upstream_cache_status"';2、然后配置缓存代理的 access_log 的路径
3、然后使用linux 的awk 命分析日志
awk '{if($NF=="\"HIT\""){hit++}}END{printf "%.2f", hit/NR}' /var/log/nginx/proxy_cache_access.log
命令解释: