ICode9

精准搜索请尝试: 精确搜索
首页 > 系统相关> 文章详细

运维之道 | Nginx 代理缓存

2020-02-22 11:07:39  阅读:215  来源: 互联网

标签:运维之道 缓存 http nginx cache Nginx proxy root


一、缓存介绍

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 2月  21 22:48 9
drwx------. 3 nobody root   15 2月  21 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%
VillianTsang 发布了155 篇原创文章 · 获赞 14 · 访问量 1万+ 私信 关注

标签:运维之道,缓存,http,nginx,cache,Nginx,proxy,root
来源: https://blog.csdn.net/VillianTsang/article/details/104436247

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有