语法配置

  1. Syntax: proxy_cache_path path;
  2. Default:
  3. Context: http;
  1. Syntax: proxy_cache zone | off;
  2. Default: proxy_cache off
  3. Context: http,server,location;

缓存过期周期

  1. Syntax: proxy_cache_valid [code...] time;
  2. Default: proxy_cache off
  3. Context: http,server,location;

缓存的维度

  1. Syntax: proxy_cache_key string;
  2. Default: proxy_cache_key $scheme$proxy_host$request_uri;
  3. Context: http,server,location;

示例

  1. upstream imooc {
  2. server 116.62.103.228:8001;
  3. server 116.62.103.228:8002;
  4. server 116.62.103.228:8003;
  5. }
  6. proxy_cache_path /opt/app/cache levels=1:2 keys_zone=imooc_cache:10m max_size=10g inactive=60m use_temp_path=off; # 定义缓存路径,分缓存目录,开辟缓存空间及大小,缓存最大容量,超过多少分钟没被使用则清理缓存
  7. server {
  8. listen 80;
  9. server_name localhost jeson.t.imooc.io;
  10. #charset koi8-r;
  11. access_log /var/log/nginx/test_proxy.access.log main;
  12. location / {
  13. proxy_cache imooc_cache; #缓存空间
  14. proxy_pass http://imooc; #反向代理上游几个服务器
  15. proxy_cache_valid 200 304 12h; # 返回状态码200,304时缓存12个小时
  16. proxy_cache_valid any 10m; # 其它缓存10分钟
  17. proxy_cache_key $host$uri$is_args$args; # key的定义
  18. add_header Nginx-Cache "$upstream_cache_status";
  19. proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; # 防止其中一台服务错误影响到整个资源获取流程
  20. include proxy_params;
  21. }
  22. error_page 500 502 503 504 /50x.html;
  23. location = /50x.html {
  24. root /usr/share/nginx/html;
  25. }
  26. }