緩存類型:
服務端緩存

代理緩存

客戶端緩存

Nginx代理緩存

配置語法
使用之前需要先定義一個proxy_cache_path
配置語法:proxy_cache_path path [levels=levels]
[use_temp_path=on|off] keys_zone=name:size [inactive=time]
[max_size=size] [manager_files=number] [manager_sleep=time]
[manager_threshold=time] [loader_files=number]
[loader_sleep=time] [loader_threshold=time] [purger=on|off]
[purger_files=number] [purger_sleep=time]
[purger_threshold=time];
默認狀態:-;
配置方法:http
proxy_cache
配置語法:proxy_cache zone | off;
默認狀態:proxy_cache off;
配置方法:http、server、location
緩存過期周期
配置語法:proxy_cache_valid [code...] time;
默認狀態:-
配置方法:http、server、location
緩存的維度
配置語法:proxy_cache_key string;
默認狀態:proxy_cache_key $scheme$proxy_host$request_url;
配置方法:http、server、location
確保后端正常:netstat -luntp|grep 800

去到Nginx緩存服務器

upstream imooc {
server 116.62.103.228:8001;
server 116.62.103.228:8002;
server 116.62.103.228:8003;
}
proxy_cache_path /opt/app/cache levels=1:2 keys_zone=imooc_cache:10m max_size=10g inactive=60m use_temp_path=off;
server {
listen 80;
server_name localhost jeson.t.imooc.io;
#charset koi8-r;
access_log /var/log/nginx/test_proxy.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;
include proxy_params;
}

檢查配置並重啟
nginx -tc /etc/nginx/nginx.conf
nginx -s reload -c /etc/nginx/nginx.conf

這個時候去訪問,就會在/opt/app/cache下生成緩存文件,並且訪問的一直是這個頁面

關閉緩存

檢查並重啟


清理指定的緩存
方式一:rm -rf 緩存目錄內容
方式二:第三方擴展模塊ngx_cache_purge
讓部分頁面不緩存
配置語法:proxy_no_cache string ...;
默認狀態:-
配置方法:http、server、location
if ($request_uri ~ ^/(url3|login|register|password\/reset)){
set $cookie_nocahe 1;
}
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;
proxy_no_cache $cookie_nocache $arg_nocache $arg_comment;
proxy_no_cache $http_pargma $http__authorization;
add_header Nginx-Cache "$upstream_cache_status";
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
include proxy_params;
}
if ($request_uri ~ ^/(url3|login|register|password\/reset)){
set $cookie_nocahe 1;
}
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;
proxy_no_cache $cookie_nocache $arg_nocache $arg_comment;
proxy_no_cache $http_pargma $http__authorization;
add_header Nginx-Cache "$upstream_cache_status";
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
include proxy_params;
}

檢查並重啟

訪問url1,會產生緩存,不管怎么刷新都訪問這個

訪問url3,則沒有緩存,會進行頁面更替(輪詢)

