常見Nginx的中介軟體架構(四)
#Nginx快取服務
#配置語法
#定義
Syntax: 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_file=number]
[loader_sleep=time][loader_threshold=time][purger=on|off]
[purger_file=number][purger_sleep=time]
[purger_threshold=time];
Default : ——
Context:http
#使用
Syntax: proxy_cache zone on|off;
Default : proxy_cache off
Context:http,server,location
#快取過期週期
Syntax: proxy_cache_valid [code...] time;
Default : proxy_cache off
Context:http,server,location
#快取維度
Syntax: proxy_cache_key string;
Default : proxy_cache_key $scheme$proxy_host$request_uri;
Context:http,server,location
#例:
# 存放臨時快取檔案 按2層目錄分級 開啟zone空間名字:大小 最大10g 60分鐘之內無訪問刪除 存放臨時檔案關閉
#proxy_cache_path /opt/app/cache levels=1:2 keys_zone=imooc_cache:10m max_size=10g inactive=60m use_temp_path=off;
# location / {
# proxy_cache imooc_cache;
# 狀態200、304的週期是12h
# proxy_cache_valid 200 304 12h;
# 其他狀態10m
# 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;
# }
#部分頁面不快取
Syntax: proxy_no_cache string;
Default : ——
Context:http,server,location
#例:
#if($request_uri ~ ^/(url3|login|register|password\/reset)){
# set $cookie_nocache 1;
#}
# location / {
# proxy_cache imooc_cache;
# 狀態200、304的週期是12h
# proxy_cache_valid 200 304 12h;
# 其他狀態10m
# 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_pragma $http_authorization;
# 返回頭可以看出是否命中
# add_header Nginx-Cache "$upstream_cache_status";
#
# include proxy_params;
# }
#大檔案分片請求
#切割成size片
Syntax: slice size;
Default : slice 0;
Context:http,server,location