nginx利用proxy_cache来缓存文件

张映 发表于 2010-09-15

分类目录: apache/nginx

标签:, , , , ,

proxy_cache是nginx自带的内置缓存模块,配置一下就可以用了,看下面的配置

vi /usr/local/nginx/conf/nginx.conf

  1. user  zhangy users;  
  2. worker_processes 10;  
  3. error_log  /var/vlogs/nginx_error.log  crit;  
  4. pid        /var/vlogs/nginx.pid;  
  5. #Specifies the value for maximum file descriptors that can be opened by this process.  
  6. worker_rlimit_nofile 65535;  
  7. events  
  8. {  
  9.  use epoll;  
  10.  worker_connections 65535;  
  11. }  
  12. http  
  13. {  
  14.  include       mime.types;  
  15.  default_type  application/octet-stream;  
  16.  #charset  gb2312;  
  17.  server_names_hash_bucket_size 128;  
  18.  client_header_buffer_size 32k;  
  19.  large_client_header_buffers 4 32k;  
  20.  client_max_body_size 8m;  
  21.  sendfile on;  
  22.  tcp_nopush     on;  
  23.  keepalive_timeout 60;  
  24.   
  25.  tcp_nodelay on;  
  26.   
  27.  fastcgi_connect_timeout 300;  
  28.  fastcgi_send_timeout 300;  
  29.  fastcgi_read_timeout 300;  
  30.  fastcgi_buffer_size 64k;  
  31.  fastcgi_buffers 4 64k;  
  32.  fastcgi_busy_buffers_size 128k;  
  33.  fastcgi_temp_file_write_size 128k;  
  34. //============  
  35.  client_body_buffer_size  512k;  
  36.  proxy_connect_timeout    5;  
  37.  proxy_read_timeout       60;  
  38.  proxy_send_timeout       5;  
  39.  proxy_buffer_size        16k;  
  40.  proxy_buffers            4 64k;  
  41.  proxy_busy_buffers_size 128k;  
  42.  proxy_temp_file_write_size 128k;  
  43.  proxy_temp_path   /usr/local/nginx/proxy_temp;  
  44. /*levels设置目录层次 
  45. keys_zone设置缓存名字和共享内存大小 
  46. inactive在指定时间内没人访问则被删除在这里是1天 
  47. max_size最大缓存空间*/  
  48. proxy_cache_path /usr/local/nginx/proxy_cache levels=1:2 keys_zone=content:20m inactive=1d max_size=100m;  
  49. //============等号中间要加的,关键只要加上proxy_cache_path  
  50.   
  51.  gzip on;  
  52.  gzip_min_length  1k;  
  53.  gzip_buffers     4 16k;  
  54.  gzip_http_version 1.0;  
  55.  gzip_comp_level 2;  
  56.  gzip_types       text/plain application/x-javascript text/css application/xml;  
  57.  gzip_vary on;  
  58.   
  59.  upstream myselfxtajmd {  
  60.  server 127.0.0.1:10002;  
  61.  server 127.0.0.1:10001 weight=5;  
  62.  }  
  63.   
  64.  server  
  65.  {  
  66.  listen       10000;  
  67.  server_name  localhost;  
  68.  index index.html index.htm index.php;  
  69.  log_format  access  '$remote_addr - $remote_user [$time_local] "$request" '  
  70.  '$status $body_bytes_sent "$http_referer" '  
  71.  '"$http_user_agent" $http_x_forwarded_for';  
  72.  access_log  /var/log/test.log  access;  
  73.   
  74.  location /  
  75.  {  
  76.  proxy_cache content; //根keys_zone后的内容对应  
  77.  proxy_cache_valid  200 304 301 302 10d;   //哪些状态缓存多长时间  
  78.  proxy_cache_valid  any 1d;    //其他的缓存多长时间  
  79.  proxy_cache_key $host$uri$is_args$args;   //通过key来hash,定义KEY的值  
  80.   
  81.  proxy_pass http://myselfxtajmd;  
  82.  proxy_redirect                      off;  
  83.  proxy_set_header   Host             $host;  
  84.  proxy_set_header   X-Real-IP        $remote_addr;  
  85.  proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;  
  86.  }  
  87.   
  88. //动态的放过  
  89.  location ~ .*\.(php|jsp|cgi)?$  
  90.  {  
  91.  proxy_set_header Host  $host;  
  92.  proxy_set_header X-Forwarded-For  $remote_addr;  
  93.  proxy_pass http://myselfxtajmd;  
  94.  }  
  95.  }  
  96. }  

上面只是配置的部分内容,反向代理的那部分没有放进去了,参考linux下nginx反向代理,实现负载均衡,当我们浏览http://localhost:10000/222.jpg时,在代理端就把图片缓存了,不用到代理终端去缓存了,可以节省资源。缓存的内容放在你设置的proxy_cache_path路径下面,看下图

nginx proxy_cache

第一层目录只有一个字符,是由levels=1:2设置,总共二层目录,子目录名字由二个字符组成。突然发现,我在我的系统里面,配置过varnish,squid,apache和mod_cache,nginx和proxy_cache,他们都是可以对文件进行缓存,不知道他们哪一个对文件的缓存效果最好呢?有空比较一下,对了这4种缓存,这个博客里面都有的,有兴趣的朋友,可以看一下



转载请注明
作者:海底苍鹰
地址:http://blog.51yip.com/apachenginx/1018.html

2 条评论

  1. solar panel 留言

    学习了。

  2. 轩脉刃 留言

    能定义那些key缓存多少事件么