nginx代理緩存


(1)緩存介紹

1.代理服務器端緩存作用
減少后端壓力,提高網站並發延時
2.緩存常見類型
服務器端緩存:代理緩存,獲取服務器端內容進行緩存
瀏覽器端緩存
3.nginx代理緩存:proxy_cache

(2)代理緩存配置

1.緩存配置

#vim /usr/local/nginx/conf/nginx.conf 
upstream node {
	server 192.9.191.31:8081;
	server 192.9.191.31:8082;
}
proxy_cache_path /cache levels=1:2 keys_zone=cache:10m max_size=10g inactive=60m use_temp_path=off;
server {
	listen 80;
	server_name www.test.com;
	index index.html;
	location / {
	proxy_pass http://node;
	proxy_cache	cache;
	proxy_cache_valid	200 304 12h;
	proxy_cache_valid	any	10m;
	add_header	Nginx-Cache	"$upstream_cache_status";
	proxy_next_upstream	error timeout invalid_header http_500 http_502 http_503 http_504;
		}
	}

2.參數詳解

proxy_cache_path /soft/cache levels=1:2 keys_zone=cache:10m max_size=10g inactive=60m use_temp_path=off;
	#proxy_cache	//存放緩存臨時文件
	#levels			//按照兩層目錄分級
	#keys_zone		//開辟空間名,10m:開辟空間大小,1m可存放8000key
	#max_size		//控制最大大小,超過后Nginx會啟用淘汰規則
	#inactive		//60分鍾沒有被訪問緩存會被清理
	#use_temp_path	//臨時文件,會影響性能,建議關閉
proxy_cache	cache;
proxy_cache_valid	200 304 12h;
proxy_cache_valid	any	10m;
add_header	Nginx-Cache	"$upstream_cache_status";
proxy_next_upstream	error timeout invalid_header http_500 http_502 http_503 http_504;
	#proxy_cache			//開啟緩存
	#proxy_cache_valid		//狀態碼200|304的過期為12h,其余狀態碼10分鍾過期
	#proxy_cache_key		//緩存key
	#add_header				//增加頭信息,觀察客戶端respoce是否命中
	#proxy_next_upstream 	//出現502-504或錯誤,會跳過此台服務器訪問下一台服務器


3.創建緩存目錄

mkdir /cache 
nginx -t 
nginx -s reload 

4.驗證

(3)清除緩存

1.rm刪除已緩存的數據
rm -rf /cache/*
2.通過ngx_cache_purge擴展模塊清理,需要編譯安裝nginx

(4)部分頁面不緩存

1.nginx配置

#vim /usr/local/nginx/conf/nginx.conf 
upstream node {
        server 192.9.191.31:8081;
        server 192.9.191.31:8082;
}
proxy_cache_path /cache levels=1:2 keys_zone=cache:10m max_size=10g inactive=60m use_temp_path=off;
server {
        listen 80;
        server_name www.test.com;
        index index.html;
        if ($request_uri ~ ^/(static|login|register|password)) {
                set $cookie_nocache 1;
                }
        location / {
                proxy_pass http://node;
                proxy_cache     cache;
                proxy_cache_valid       200 304 12h;
                proxy_cache_valid       any     10m;
                add_header      Nginx-Cache     "$upstream_cache_status";
                proxy_next_upstream     error timeout invalid_header http_500 http_502 http_503 http_504;
                proxy_no_cache $cookie_nocache $arg_nocache $arg_comment;
                proxy_no_cache $http_pargma $http_authorization;
                }
        }


2.重啟加驗證

nginx -t 
nginx -s reload 

兩次都沒有命中

(4)統計日志命中率

1.日志格式:變量$upstream_cache_status"

#vim /usr/local/nginx/conf/nginx.conf 		
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"';
access_log logs/access.log main;
error_log logs/error.log;

2.查看日志

3.統計日志命中率加入到計划任務中這里省略

awk '{if($NF = "HIT"){count++;}} END{printf "%.2f%",count/NR*100}' /usr/local/nginx/logs/access.log


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM