Nginx代理缓存功能

Nginx缓存主要是用于减轻后端服务器的负载,提高网站并发量,提升用户体验度。

注意:Nginx反向代理的缓存功能是由ngx_http_proxy_module提供,在使用缓存功能时务必要nginx支持该模块。可能有些选项的不支持Nginx的版本,具体看官方文档: http://nginx.org/en/docs/http/ngx_http_proxy_module.html

后端服务器可能无法承受负载
为了更好的提升用户体验环境介绍

             服务器IP            服务器角色

           192.168.123.36          代理服务器

           192.168.123.34          上游服务器

部署环境配置表 1-0

Proxy cache配置

1.server配置

proxy_cache_path /cache/nginx/ levels=1:2 keys_zone=mycache:64m;
server {
listen 80;
location / {
proxy_cache mycache;
proxy_pass http://192.168.123.34:80/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_cache_methods GET HEAD;
proxy_cache_revalidate on;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
proxy_cache_valid any 1m;
proxy_cache_min_uses 1;
proxy_cache_use_stale error timeout invalid_header http_500 http_502 http_503 http_504;
}
} 2.参数详解

proxy_cache_path /cache/nginx/ levels=1:2 keys_zone=mycache:64m;
#proxy_cache_path 为缓存存放路径;
#levels的第一位表示使用1级子目录冒号隔开第二位表示使用2级子目录,其最多使用三级,1表示每个一级子目录的名字只能使用1个字符;
#key_zone中的mycache为缓存名字,可以在location或者server中通过proxy_cache引用;64m表示用多少内存空间存储nginx key;
proxy_cache mycache;
#引用mycache缓存空间;
proxy_pass http://192.168.123.34:80/;
#将来自 / 的请求代理至192.168.123.34:80 该服务器,后面的 '/' 是必须的;
proxy_set_header Host $host;
#用于后端的real server区分不同的虚拟主机;
proxy_set_header X-Real-IP $remote_addr;
#记录客户端真实ip地址,而不是代理服务器地址,需要后端web服务器开启日志相应功能接收;
proxy_cache_methods GET HEAD;
#表示对客户端请求的GET 和 HEAD方法进行缓存;
proxy_cache_revalidate on;
#本地缓存过期会检查后端服务器该缓存是否存在,避免后端重传占据带宽;
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
proxy_cache_valid any 1m;
#针对于不同的响应码进行缓存不同的时间设定;
proxy_cache_min_uses 1;
#某一个请求被响应多少次才会被缓存,默认是1,可以将该值设置为大一些;
proxy_cache_use_stale error timeout invalid_header http_500 http_502 http_503 http_504;
#指明哪种场景可以使用过期缓存,提升用户体验;
补充:
proxy_hide_header;
#隐藏由proxy响应客户端时指定的首部;
proxy_buffer 4|8k
#为了响应客户端更快,服务器端响应客户端可能分成多个ip报文响应,也可以整合在一起再一次响应;
Nginx缓存是键值存储,URL是键,文件路径是值。键值存储的速度就是加快在文件系统中查找的速度。所以,存储的key是哈希过的值。

3.平滑重启nginx

[root@localhost nginx]# nginx -s reload
[root@localhost nginx]# ps -elf|grep nginx
1 S root 10175 1 0 80 0 - 27830 sigsus 09:52 ? 00:00:00 nginx: master process nginx
5 S www 11165 10175 0 80 0 - 28893 ep_pol 18:10 ? 00:00:00 nginx: worker process
5 S www 11166 10175 0 80 0 - 28893 ep_pol 18:10 ? 00:00:00 nginx: worker process
5 S www 11167 10175 0 80 0 - 27830 ep_pol 18:10 ? 00:00:00 nginx: cache manager process
重启完成这里会多一个cache manager,其主要作用和memcached的LRU算法相似,删除过期缓存。而如果缓存没过期其上有服务器数据发生变化则依旧访问是错误的数据。可以通过程序实现。

4. 后端服务器配置静态页面

4.1 虚拟主机配置

server {
listen 80;
server_name www.test.com;
location / {
root html/bbs;
index index.html index.htm;
}
} 4.2 页面内容

<h1>i am node1</h1> 4.3 物理机尝试访问

#可以在代理服务器里查看到缓存的key

#此时修改index内容客户端请求并不会发生改变

<h1>i am node2 node2!!!</h1>
#客户端清理缓存再次请求

5.在不使用缓存与使用缓存的对比测速

1.分别处理10000个请求1000个并发对比,先是不使用缓存的情况

[root@localhost nginx]# ab -n 10000 -c 1000 ip地址
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, ip地址
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking http://www.test.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software: nginx/1.6.3
Server Hostname: http://www.test.com
Server Port: 80
Document Path: /index.html
Document Length: 20 bytes
Concurrency Level: 1000
Time taken for tests: 4.393 seconds
Complete requests: 10000
Failed requests: 836
(Connect: 0, Receive: 0, Length: 836, Exceptions: 0)
Write errors: 0
Non-2xx responses: 836
Total transferred: 2586108 bytes
HTML transferred: 343792 bytes
Requests per second: 2276.42 [#/sec] (mean)
Time per request: 439.286 [ms] (mean)
Time per request: 0.439 [ms] (mean, across all concurrent requests)
Transfer rate: 574.91 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 114 312.3 6 3018
Processing: 11 116 283.4 51 3038
Waiting: 1 112 283.4 48 3038
Total: 18 230 482.0 64 4019
Percentage of the requests served within a certain time (ms)
50% 64
66% 78
75% 93
80% 105
90% 1054
95% 1261
98% 2066
99% 2315
100% 4019 (longest request)
#在不使用缓存的情况下耗费了4.393秒,值可以多测试几次对比
2.使用缓存

[root@localhost nginx]# ab -n 10000 -c 1000 ip地址
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, ip地址
Licensed to The Apache Software Foundation,http://www.apache.org/
Benchmarking http://www.test.com (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software: nginx/1.6.3
Server Hostname: http://www.test.com
Server Port: 80
Document Path: /index.html
Document Length: 20 bytes
Concurrency Level: 1000
Time taken for tests: 1.179 seconds
Complete requests: 10000
Failed requests: 651
(Connect: 0, Receive: 0, Length: 651, Exceptions: 0)
Write errors: 0
Total transferred: 2337250 bytes
HTML transferred: 186980 bytes
Requests per second: 8483.63 [#/sec] (mean)
Time per request: 117.874 [ms] (mean)
Time per request: 0.118 [ms] (mean, across all concurrent requests)
Transfer rate: 1936.36 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 66 195.8 24 1044
Processing: 11 31 11.0 30 249
Waiting: 0 22 11.3 22 239
Total: 35 97 194.3 55 1070
Percentage of the requests served within a certain time (ms)
50% 55
66% 59
75% 62
80% 64
90% 84
95% 107
98% 1062
99% 1066
100% 1070 (longest request)
#可以看到在使用缓存的情况下处理10000个请求1000的并发耗时为1.179秒!!!

注意:在使用到缓存的时候务必考虑缓存过期!根据生产场景使用

如果你喜欢我写的技术文章以及面试总结,欢迎关注收看我的视频,并且点赞、收藏、关注我哦。

我是luke,感谢你的关注!

很多PHPer在进阶的时候总会遇到一些问题和瓶颈,业务代码写多了没有方向感,不知道该从那里入手去提升,对此我整理了一些资料,希望能够去帮助到小伙伴们,可以关注我私聊获取。也可以加入到我的圈子一起学习成长哦【架构师之路】点击链接申请加入圈子

架构师之路 - 知乎​www.zhihu.com

nginx 接收报文_Nginx代理缓存功能相关推荐

  1. 使用Nginx反向代理和proxy_cache缓存搭建CDN服务器加快Web访问速度

    碰到问题:移动用户访问web服务器www.osyunwei.com很慢 解决办法: 1.在移动机房放置一台nginx反向代理服务器 2.通过域名DNS智能解析,所有移动用户访问www.osyunwei ...

  2. nginx反向代理缓存服务器构建

    博主QQ:819594300 博客地址:http://zpf666.blog.51cto.com/ 有什么疑问的朋友可以联系博主,博主会帮你们解答,谢谢支持! 代理服务可简单的分为正向代理和反向代理: ...

  3. 基于nginx实现缓存功能及uptream模块详细使用方法

    基于nginx实现缓存功能及uptream模块详细使用方法 一般情况下,前端使用nginx做代理或7层负载并向后实现varish/squid做cache server的效果要好的多 nginx与squ ...

  4. Nginx服务器的压缩功能和缓存功能

    介绍 在Nginx服务器配置文件中可以通过配置Gzip的使用,可以配置在http块,server 块或者location块中设置,Nginx服务器可以通过ngx_http_gzip_module模块. ...

  5. NGINX做反向代理缓存服务器原理

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

  6. 【推荐】如何用 Nginx 构建反向代理缓存服务器?

    作者:一盏烛光,贤牛特邀工程师. 防伪码:曾经沧海难为水,除却巫山不是云. 代理服务可简单的分为正向代理和反向代理: 正向代理: 用于代理内部网络对 Internet 的连接请求(如×××/NAT), ...

  7. Nginx实现负载均衡Nginx缓存功能

    目录 一.Nginx是什么 二.Nginx实现反向代理 2.1 正向代理和反向代理 2.2 nginx实现反向代理 2.2.1 proxy_pass配置 2.2.1.1ngx_http_rewrite ...

  8. 使用Nginx的proxy_cache缓存功能取代Squid

    [文章作者:张宴 本文版本:v1.2 最后修改:2009.01.12 转载请注明原文链接:http://blog.s135.com/nginx_cache/] Nginx从0.7.48版本开始,支持了 ...

  9. 使用Nginx的proxy_cache缓存功能取代Squid[原创]

    使用Nginx的proxy_cache缓存功能取代Squid[原创] [文章作者:张宴 本文版本:v1.2 最后修改:2009.01.12 转载请注明原文链接:http://blog.zyan.cc/ ...

最新文章

  1. python 列表转字典
  2. tkinter 笔记:创建输入框并显示结果 (莫烦python笔记)
  3. Eclipse实现hibernate反向工程:从数据库逆向生成实体类和hbm文件
  4. EntitySpaces2009的开发文档地址
  5. 苹果CMS v10一键采集芒果tv官方直链地址插件
  6. 用java编写圆锥_求java大神帮忙 求大神帮助!Java
  7. 计算几何--二维几何前置基础知识
  8. Oracle Statspack分析报告详解(一)
  9. 伟福6000微型计算机,伟福6000仿真软件
  10. Jenkins checkout的文件 , TortoiseSVN 无法提交。 问题已经解决啦!
  11. 强制更改wifi名前缀CMCC
  12. 【小插件】文字镂空“LSP”制作空心文字CAD
  13. Linux C程序实现查看文件夹大小
  14. ModuleNotFoundError: No module named ‘keras.api‘解决
  15. yolo系列外文翻译_yolov3论文中英对照版
  16. windows环境下面批量修改文件夹名称
  17. 分词统计(四)唐宋元诗人吟诗作词的时候,最偏爱哪些词语呢?(附上AI写的1000句诗!)
  18. 百度地图迁徙大数据_百度地图迁徙大数据:除武汉外多地出行趋势回升
  19. 程序员的副业,有人做扮鬼演员月入5K,有人接私活年入80万!
  20. linux用shell脚本写游戏,shell脚本实现猜数游戏

热门文章

  1. WPF/Sliverlight ScrollViewer与Panel(2)
  2. Net设计模式实例之观察者模式(Observer Pattern)
  3. 当画面出现问题时,如何调试!
  4. 三次握手的本质_动画讲解TCP的3次握手,4次挥手,让你一次看明白
  5. 【Hive】hive表的文件存储格式
  6. Java对象序列化文件追加对象的问题,以及Java的读取多个对象的问题解决方法。
  7. Ubuntu16.04死机解决方案
  8. 解决UE4 Launcher启动速度极慢的方法
  9. ImportError: cannot import name ‘render_to_response‘ 解决方法
  10. 如何为iTunes 11中的歌曲列表着色算法有效? [关闭]