2、为upstream启用缓存



  1. http {
  2.     include       mime.types;
  3.     default_type  application/octet-stream;
  4.     sendfile        on;
  5.     keepalive_timeout  65;
  6.  
  7.     proxy_cache_path /nginx/cache/first  levels=1:2   keys_zone=first:10m max_size=512m; -->设定缓存
  8.  
  9.     upstream websrv {
  10.         server 10.1.249.168 weight=1;
  11.    server 10.1.249.157 weight=1;
  12.    server 127.0.0.1:8080 backup;
  13.     }
  14.     server {
  15.         listen       80;
  16.    add_header X-Via $server_addr; -->添加首部信息
  17.    add_header X-Cache-Status $upstream_cache_status;
  18.         location / {
  19.        proxy_pass http://websrv;
  20.        proxy_cache first; -->启用缓存
  21.             proxy_cache_valid 200 1d;
  22.             proxy_cache_valid 301 302 10m;
  23.             proxy_cache_valid any 1m;
  24.             index  index.html index.htm;

  25.         }
  26.         error_page   500 502 503 504  /50x.html;
  27.         location = /50x.html {
  28.             root   html;
  29.         }
  30.     }
  31.     server {
  32.     listen 8080;
  33.     server_name localhost;
  34.     root /nginx/htdocs;
  35.     index index.html;
  36.     }
  37. }

测试:
默认情况下,nginx对定义了权重的upstream服务器使用加权轮调的方法调度访问,
因此,其多次访问应该由不同的服务器进行响应
但是由于此时我们开启的nginx的缓存功能
第一次访问某可缓存资源时,在本地缓存中尚未有其对应的缓存对象,
因此,其一定为未命中状态。而第二次请求时,则可以直接从本地缓存构建响应报文。
后续的请求将直接从缓存中返回结果,而不会采用轮询方法 

如图所示

 
 
 
 

 

[root@node1 ~]# ls /nginx/cache/first/9/14/f3e89306acaef2952ab9f1eae6507149
/nginx/cache/first/9/14/f3e89306acaef2952ab9f1eae6507149  --> 缓存文件


对于nginx自身的缓存key - value 
key缓存在内存在
value缓存在磁盘上






posted @ 2016-11-21 18:39  studylinux  阅读(148)  评论(0)    收藏  举报