2、为upstream启用缓存
http {include mime.types;default_type application/octet-stream;sendfile on;keepalive_timeout 65;proxy_cache_path /nginx/cache/first levels=1:2 keys_zone=first:10m max_size=512m; -->设定缓存upstream websrv {server 10.1.249.168 weight=1;server 10.1.249.157 weight=1;server 127.0.0.1:8080 backup;}server {listen 80;add_header X-Via $server_addr; -->添加首部信息add_header X-Cache-Status $upstream_cache_status;location / {proxy_pass http://websrv;proxy_cache first; -->启用缓存proxy_cache_valid 200 1d;proxy_cache_valid 301 302 10m;proxy_cache_valid any 1m;index index.html index.htm;}error_page 500 502 503 504 /50x.html;location = /50x.html {root html;}}server {listen 8080;server_name localhost;root /nginx/htdocs;index index.html;}}
测试:
默认情况下,nginx对定义了权重的upstream服务器使用加权轮调的方法调度访问,
因此,其多次访问应该由不同的服务器进行响应
但是由于此时我们开启的nginx的缓存功能
第一次访问某可缓存资源时,在本地缓存中尚未有其对应的缓存对象,
因此,其一定为未命中状态。而第二次请求时,则可以直接从本地缓存构建响应报文。
后续的请求将直接从缓存中返回结果,而不会采用轮询方法
如图所示





[root@node1 ~]# ls /nginx/cache/first/9/14/f3e89306acaef2952ab9f1eae6507149
/nginx/cache/first/9/14/f3e89306acaef2952ab9f1eae6507149 --> 缓存文件
对于nginx自身的缓存key - value
key缓存在内存在
value缓存在磁盘上

浙公网安备 33010602011771号