wslsg 发表于 2018-11-8 11:31:49

nginx的cache功能(修正版)

location ~ /purge(/.*)  {
  allow 127.0.0.1;
  allow 192.168.1.0/24;
  deny all;
  proxy_cache_purge cache_one $host$1$is_args$args;
  }
  # 设置缓存的文件类型,建议在后端设置缓存时间通过expires cache-control:max-age=
  location ~ .*\.(gif|jpg|png|bmp|swf|js|css)$
  {
  proxy_cache cache_one;
  # 在cache端强制设置缓存时间
  proxy_cache_valid 200 304 12h;
  proxy_cache_valid 301 302 1m;
  proxy_cache_valid any 1m;
  proxy_cache_key $host$uri$is_args$args;
  #####
  add_header X-Cache $upstream_cache_status;
  proxy_set_header X-Forwarded-For $remote_addr;
  proxy_pass http://my_server_pool;
  }
  }
  四.测试
  访问 http://xxxx/logo.jpg
  命中率:
  #!/bin/bash
  # author:gupeng
  # proxy_cache hit rate
  if [ "$1" != '' ];then
  if [ -e "$1" ];then
  HIT=`cat $1 | grep HIT | wc -l`
  ALL=`cat $1 | wc -l`
  Hit_rate=`echo "scale=2;($HIT/$ALL)*100" | bc`
  echo "Hit rate=$Hit_rate%"
  else
  echo "$1 not exsist!"
  fi
  else
  echo "usage: ./hit_rate.sh file_path"
  fi
  五、命中状态说明
  $upstream_cache_status
  Appeared in 0.8.3. Possible values:
  ·MISS
  ·EXPIRED - expired, request was passed to backend 请求被传送到后端
  ·UPDATING - expired, stale response was used due to proxy/fastcgi_cache_use_stale
  updating 正在更新,将使用旧的应答
  ·STALE - expired, stale response was used due to proxy/fastcgi_cache_use_stale 后端将
  得到过期的应答
  ·HIT

页: [1]
查看完整版本: nginx的cache功能(修正版)