1.环境准备

操作系统 应用服务 外网地址 内网地址
CentOS7.6 LB01 10.0.0.5 172.16.1.5
CentOS7.6 Web01 10.0.0.7 172.16.1.7

2.web01操作

2.1建立相关目录
[root@web01 ~]# mkdir -p /soft/code{1..3} 2.2建立相关html文件
[root@web01 ~]# for i in {1..3};do echo Code1-Url$i > /soft/code1/url$i.html;done
[root@web01 ~]# for i in {1..3};do echo Code2-Url$i > /soft/code2/url$i.html;done
[root@web01 ~]# for i in {1..3};do echo Code3-Url$i > /soft/code3/url$i.html;done 2.3配置Nginx
[root@web01 conf.d]# cat node.conf
server {
listen 8081;
server_name web.cheng.com;
root /soft/code1;
index index.html;
} server {
listen 8082;
server_name web.cheng.com;
root /soft/code2;
index index.html;
} server {
listen 8083;
server_name web.cheng.com;
root /soft/code3;
index index.html;
} 2.4检查端口是否开启
[root@web01 conf.d]# netstat -lntup
tcp 0 0 0.0.0.0:8081 0.0.0.0:* LISTEN 1597/nginx: master
tcp 0 0 0.0.0.0:8082 0.0.0.0:* LISTEN 1597/nginx: master
tcp 0 0 0.0.0.0:8083 0.0.0.0:* LISTEN 1597/nginx: master

3.LB01配置代理缓存

-------->配置详解:
proxy_cache 存放缓存临时文件
levels 按照两层目录分级
keys_zone 开辟空间名, 10m:开辟空间大小, 1m可存放8000key
max_size 控制最大大小, 超过后Nginx会启用淘汰规则
inactive 60分钟没有被访问缓存会被清理
use_temp_path 临时文件, 会影响性能, 建议关闭 [root@lb01 conf.d]# cat proxy_cache.conf
proxy_cache_path /soft/cache levels=1:2 keys_zone=code_cache:10m max_size=10g inactive=60m use_temp_path=off; upstream cache {
server 172.16.1.7:8081;
server 172.16.1.7:8082;
server 172.16.1.7:8083;
} server {
listen 80;
server_name 10.0.0.5;
index index.html; location / {
proxy_pass http://cache;
proxy_cache code_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;
include proxy_params; }
} ------------------------------------------------------------------------------
proxy_cache 开启缓存
proxy_cache_valid 状态码200|304的过期为12h, 其余状态码10分钟过期
proxy_cache_key 缓存key
add_header 增加头信息, 观察客户端respoce是否命中
proxy_next_upstream 出现502-504或错误, 会跳过此台服务器访问下台

4.LB01通过浏览器测试

1.第一次访问没有命中;



2.第二次访问命中;

[root@lb01 conf.d]# tree /soft/cache/
/soft/cache/
├── 7
│ └── 06
│ └── 289114bc31bdbeab995daebbcd107067
├── 8
│ └── c5
│ └── b39aea32bccf0c3e468f726ae9c75c58
└── d
└── f1
└── 002e95b5414ffb82dacc2e914ee3df1d

5.清理proxy_cache代理的缓存

方式一:使用rm删除已缓存数据

[root@lb01 cache]# rm -rf /soft/cache/*
[root@lb01 cache]# curl -s -I http://10.0.0.5/url1.html |grep "Nginx-Cache"
Nginx-Cache: MISS

方式二:使用ngx_cache_purge扩展模块清理, 需要编译安装Nginx

1.创建对应的目录
[root@lb01 ~]# mkdir /soft/src && cd /soft/src/
2.上传nginx源码包
[root@lb01 src]# rz nginx-1.16.1.tar.gz
[root@lb01 src]# tar xf nginx-1.16.1.tar.gz
3.下载ngx_cache_purge模块
[root@lb01 ~]# wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
[root@lb01 ~]# tar xf ngx_cache_purge-2.3.tar.gz
4.编译Nginx
[root@lb01 nginx-1.12.2]# yum -y install pcre-devel openssl openssl-devel
[root@lb01 ~]# cd /soft/src/nginx-1.12.2/
[root@lb01 nginx-1.12.2]# ./configure --prefix=/server/nginx --add-module=/root/ngx_cache_purge-2.3 --with-http_stub_status_module --with-http_ssl_module
[root@lb01 ~]# make && make install 5.停掉之前的nginx
[root@lb01 nginx-1.12.2]# cd /server/
[root@lb01 server]# systemctl stop nginx 6.拷贝文件至
[root@lb01 ~]# cp /etc/nginx/conf.d/proxy_cache.conf /server/nginx/conf/conf.d/
[root@web01 conf.d]# scp -rp /etc/nginx/conf.d/node.conf root@172.16.1.5:/server/nginx/conf.d/ [root@lb01 conf]# vim nginx.conf 注释掉server相关的配置
在注释掉的server之上新增 include conf.d/*.conf; [root@lb01 conf]# cp /etc/nginx/proxy_params /server/nginx/conf/proxy_params
[root@lb01 conf]# /server/nginx/sbin/nginx -t
[root@lb01 conf.d]# /server/nginx/sbin/nginx
[root@lb01 conf.d]# /server/nginx/sbin/nginx -s reload [root@lb01 conf.d]# netstat -lntup
tcp 0 0 0.0.0.0:8081 0.0.0.0:* LISTEN 24583/nginx: master
tcp 0 0 0.0.0.0:8082 0.0.0.0:* LISTEN 24583/nginx: master
tcp 0 0 0.0.0.0:8083 0.0.0.0:* LISTEN 24583/nginx: master [root@lb01 conf.d]# rm -rf /soft/cache/* 编写配置文件
[root@lb01 conf.d]# vim proxy_cache.conf [root@lb01 conf.d]# /server/nginx/sbin/nginx -t
[root@lb01 conf.d]# /server/nginx/sbin/nginx -s reload
7.使用浏览器访问建立缓存
8.通过访问purge/url地址,删除对应的缓存
9.再次刷新就会因为缓存内容已清理,而出现404错误

6.配置指定的页面不缓存

[root@lb01 conf.d]# cat proxy_cache.conf
proxy_cache_path /soft/cache levels=1:2 keys_zone=code_cache:10m max_size=10g inactive=60m use_temp_path=off; upstream cache {
server 172.16.1.7:8081;
server 172.16.1.7:8082;
server 172.16.1.7:8083;
} server {
listen 80;
server_name 10.0.0.5;
index index.html; if ($request_uri ~ ^/(url3|login|register|password)) {
set $nocache 1;
} location / {
proxy_pass http://cache;
proxy_cache code_cache;
proxy_cache_valid 200 304 12h;
proxy_cache_valid any 10m;
add_header Nginx-Cache "$upstream_cache_status";
proxy_no_cache $nocache $arg_nocache $arg_comment; #不缓存变量为nocache
proxy_no_cache $http_pargma $http_authorization; #不缓存http参数以及http认证
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
include proxy_params; } location ~ /purge(/.*) {
allow 127.0.0.1;
allow 10.0.0.0/24;
deny all;
proxy_cache_purge code_cache $host$1$is_args$args;
}
} 提前先清理缓存
[root@lb01 conf.d]# rm -rf /soft/cache/* 无论如何请求url3都无法命中
[root@lb01 conf.d]# curl -s -I http://10.0.0.5/url3.html|grep "Nginx-Cache"
Nginx-Cache: MISS
[root@lb01 conf.d]# curl -s -I http://10.0.0.5/url3.html|grep "Nginx-Cache"
Nginx-Cache: MISS
[root@lb01 conf.d]# curl -s -I http://10.0.0.5/url3.html|grep "Nginx-Cache"
Nginx-Cache: MISS

7.配置缓存日志记录

  1. 修改nginx的log_format格式,增加"$upstream_cache_status"c
[root@lb01 conf.d]# vim /etc/nginx/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 /var/log/nginx/access.log main;
  1. 在server标签中添加对应的access日志
server {
...
access_log /var/log/nginx/proxy_cache.log main;
...
}
  1. 通过浏览器访问, 最后检查日志命令情况

21.Nginx代理缓存的更多相关文章

  1. nginx代理缓存

    (1)缓存介绍 1.代理服务器端缓存作用 减少后端压力,提高网站并发延时 2.缓存常见类型 服务器端缓存:代理缓存,获取服务器端内容进行缓存 浏览器端缓存 3.nginx代理缓存:proxy_cach ...

  2. Nginx代理缓存功能

    Nginx代理缓存功能      Nginx缓存主要是用于减轻后端服务器的负载,提高网站并发量,提升用户体验度. 注意:Nginx反向代理的缓存功能是由ngx_http_proxy_module提供, ...

  3. 12、Nginx代理缓存服务

    通常情况下缓存是用来减少后端压力, 将压力尽可能的往前推, 减少后端压力,提高网站并发延时 1.缓存常见类型 服务端缓存 代理缓存, 获取服务端内容进行缓存 客户端浏览器缓存 Nginx代理缓存原理 ...

  4. 网站集群架构(LVS负载均衡、Nginx代理缓存、Nginx动静分离、Rsync+Inotify全网备份、Zabbix自动注册全网监控)--技术流ken

    前言 最近做了一个不大不小的项目,现就删繁就简单独拿出来web集群这一块写一篇博客.数据库集群请参考<MySQL集群架构篇:MHA+MySQL-PROXY+LVS实现MySQL集群架构高可用/高 ...

  5. 使用Nginx压缩文件、设置反向代理缓存提高响应速度

    Gzip压缩: 最开始,这个竟然要6m多(大到不寻常),响应的速度3分多钟. 所以先对返回的文件进行gzip压缩.判断返回的资源是否有使用gzip压缩,观察响应头部里面,如果没有 Content-En ...

  6. nginx的缓存服务

    都知道缓存的目的是为了减小服务端的压力,可以在客户端直接取到数据 客户端---------------nginx(代理缓存)------------------服务端 代理缓存的描述: 就是客户端发送 ...

  7. Nginx做缓存

    查看服务 netstat -lntp|grep 80 Nginx作为缓存WEB服务 通常情况下缓存是用来减少后端压力, 将压力尽可能的往前推, 减少后端压力,提高网站并发延时 Nginx代理缓存原理 ...

  8. Nginx - 代理、缓存

    Nginx 标签 : nginx 代理 代理服务可简单的分为正向代理和反向代理: 正向代理: 用于代理内部网络对Internet的连接请求(如VPN/NAT),客户端指定代理服务器,并将本来要直接发送 ...

  9. nginx反向代理+缓存开启+url重写+负载均衡(带健康探测)的部署记录

    在日常运维工作中,运维人员会时常使用到nginx的反向代理,负载均衡以及缓存等功能来优化web服务性能. 废话不多说,下面对测试环境下的nginx反向代理+缓存开启+url重写+负载均衡(带健康探测) ...

随机推荐

  1. C++基础之泛型算法

    标准库并未给每个容器添加大量功能,因此,通过大量泛型算法,来弥补.这些算法大多数独立于任何特定的容器,且是通用的,可用于不同类型的容器和不同的元素. 迭代器使得算法不依赖容器,但是算法依赖于元素的类型 ...

  2. 夯实Java基础系列5:Java文件和Java包结构

    目录 Java中的包概念 包的作用 package 的目录结构 设置 CLASSPATH 系统变量 常用jar包 java软件包的类型 dt.jar rt.jar *.java文件的奥秘 *.Java ...

  3. SpringMVC 图片上传虚拟目录

    可以直接在tomcat的server.xml文件中进行设置,位置在Host中 添加内容为:<Context docBase="G:\JAVAtest\temp" path=& ...

  4. 手把手教你用最简便的方法免费安装SSL

    原文链接:小枫同学的个人博客 随时IT的发展,它几乎涵盖了世界发展中的任何一方面,几乎都和计算机挂钩,也有好多小伙伴想开一个自己的网站,分享一些知识,分享一些心情等等.但是随着IT的发展,网络安全也越 ...

  5. Sentinel Cluster流程分析

     前面介绍了sentinel-core的流程,提到在进行流控判断时,会判断当前是本地限流,还是集群限流,若是集群模式,则会走另一个分支,这节便对集群模式做分析. 一.基本概念  namespace:限 ...

  6. 使用springboot最新版本mysql-Connector连接数据库时报错解决

    在连接数据库时,使用了最新版本的mysql-Connector,即6.0以上版本 1.报错如下: Loading class `com.mysql.jdbc.Driver'. This is depr ...

  7. C++ProtoBuf的安装与使用

    目录 安装(Ubuntu 16.04) 简介 proto2 proto3 用法 proto3 输出结果 总结 @(目录) 安装(Ubuntu 16.04) sudo apt-get install a ...

  8. python+selenium遇到元素定位不到的问题,顺便记录一下自己这次的错误(报错selenium.common.exceptions.NoSuchElementException)

    今天在写selenium一个发送邮件脚本时,遇到一些没有找到页面元素的错误.经过自己反复调试,找原因百度,终于解决了.简单总结一下吧,原因有以下几点: 一:Frame控件嵌套,.Frame/Ifram ...

  9. Rust入坑指南:常规套路

    搭建好了开发环境之后,就算是正式跳进Rust的坑了,今天我就要开始继续向下挖了. 由于我们初来乍到 ,对Rust还不熟悉,所以我决定先走一遍常规套路. 变不变的变量 学习一门语言第一个要了解的当然就是 ...

  10. slf4j+logback&logback.xml

    添加maven依赖 <dependencies> <!--https://mvnrepository.com/artifact/org.slf4j/slf4j-api --> ...