運維之道 | Nginx 代理緩存

一、緩存介紹

1.代理服務器端緩存作用

Nginx緩存主要是用於減輕後端服務器的負載,減少後端壓力,提高網站併發量,提升用戶體驗度,提高網站併發延時;

2.緩存常見類型

服務器端緩存:代理緩存,獲取服務器端內容進行緩存
瀏覽器端緩存

3.nginx代理緩存:proxy_cach

Nginx代理緩存功能


二、代理緩存配置

1.緩存配置
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf

worker_processes  1;
events {
    worker_connections  1024;
}
http{
upstream node {
    server 192.168.182.10:80;
    server 192.168.182.10:81;
        }

proxy_cache_path /var/cache levels=1:2 keys_zone=cache:10m max_size=10g inactive=60m use_temp_path=off;

server {
    listen 80;
    server_name www.zwl.com;
    index index.html;

    location / {
    proxy_pass http://node;
    proxy_cache cache;
    proxy_cache_valid   200 304 12h;
    proxy_cache_valid   any 30s;
    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 /var/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.重啓nginx服務
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t							///檢測配置
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload					///重新加載配置

PS:重新加載配置後,會自動生成/var/cache緩存文件

4.測試緩存
[root@localhost ~]# curl -s -I www.zwl.com | grep -i nginx-cache
Nginx-Cache: MISS
[root@localhost ~]# curl -s -I www.zwl.com | grep -i nginx-cache
Nginx-Cache: HIT
[root@localhost ~]# curl -s -I www.zwl.com | grep -i nginx-cache
Nginx-Cache: HIT
[root@localhost ~]# curl -s -I www.zwl.com | grep -i nginx-cache
Nginx-Cache: HIT
  • 生成緩存信息
[root@localhost ~]# ll /cache/
總用量 0
drwx------. 3 nobody nobody 16 221 22:48 9
drwx------. 3 nobody root   15 221 22:48 temp
5.清除緩存
  • rm刪除已緩存的數據 :rm -rf /cache/*
  • 通過ngx_cache_purge擴展模塊清理,需要編譯安裝nginx

三、部分不緩存配置

1.部分頁面不緩存
worker_processes  1;
events {
    worker_connections  1024;
}
http{


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;

upstream node {
    server 192.168.182.10:80;

        }

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.zwl.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 30s;
    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服務
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t							///檢測配置
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s reload					///重新加載配置
3.測試緩存
  • 因爲測試界面是靜態界面,則沒有被擊中緩存
[root@localhost conf]# curl -s -I www.zwl.com | grep -i nginx-cache
Nginx-Cache: MISS
[root@localhost conf]# curl -s -I www.zwl.com | grep -i nginx-cache
Nginx-Cache: MISS
[root@localhost conf]# curl -s -I www.zwl.com | grep -i nginx-cache
Nginx-Cache: MISS
  • 將不緩存配置中靜態static去掉,則被擊中緩存
[root@localhost conf]# curl -s -I www.zwl.com | grep -i nginx-cache
Nginx-Cache: MISS
[root@localhost conf]# curl -s -I www.zwl.com | grep -i nginx-cache
Nginx-Cache: HIT
[root@localhost conf]# curl -s -I www.zwl.com | grep -i nginx-cache
Nginx-Cache: HIT

四、日誌信息

1.日誌格式:變量$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"';
access_log logs/access.log main;
error_log logs/error.log;

在這裏插入圖片描述

2.統計緩存率
[root@localhost conf]#  awk '{if($NF = "HIT"){count++;}} END{printf "%.2f%",count/NR*100}' /usr/local/nginx/logs/access.log
100.00%
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章