nginx 反向代理置将文件缓存在内存中

PHPABC Nginx 1,373 次浏览 , , 1条评论

nginx 反向代理配置小示例,将缓存文件存放在/dev/shm

server
    {
        listen  80;
        server_name st001.img.linuxtone.org;
        proxy_temp_path  /dev/shm/proxy_temp ;
        proxy_set_header Accept-Encoding '';
        location / {
                   proxy_pass href="http://st001.img.linuxtone.org/" target="_blank">http://st001.img.linuxtone.org;
                   include /usr/local/nginx/conf/proxy.conf;
                  }
 
 
 
       location /thumbnail {
       root /dev/shm/proxy_temp/$host;  #缓存文件存储目录
 
       proxy_set_header Accept-Encoding '';
       add_header      X-Cache   HIT-LT;
       expires 1y;
       if (!-e $request_filename) {
          proxy_pass http://st001.img.linuxtone.org;
          break;
          }
          include /usr/local/nginx/conf/proxy.conf;
       }
}

这样第一次访问:
http://st001.img.linuxtone.org/thumbnail/55/cd/2e/55cd2ecbb6d24cb60b2c657958fdaab0_250x400.jpg
将缓存/dev/shm/proxy_temp/st001.img.linuxtone.org/目录下.

[root@www tmp]# wget -S   http://st001.img.linuxtone.org/thumbnail/55/cd/2e/55cd2ecbb6d24cb60b2c657958fdaab0_250x400.jpg  
 
//以下是 header信息.
--12:16:37-- 
Resolving st001.img.linuxtone.org... 60.28.208.52
Connecting to st001.img.linuxtone.org|60.28.208.52|:80... connected.
 
    HTTP request sent, awaiting response...
      HTTP/1.1 200 OK
      Server: nginx
      Date: Sun, 18 Oct 2009 04:16:37 GMT
      Content-Type: image/jpeg
      Content-Length: 24524
      Last-Modified: Thu, 15 Oct 2009 12:25:39 GMT
      Connection: keep-alive
      Expires: Mon, 18 Oct 2010 04:16:37 GMT
      Cache-Control: max-age=31536000
        X-Cache: HIT-LT
      Accept-Ranges: bytes
    Length: 24524 (24K) [image/jpeg]
    Saving to: `55cd2ecbb6d24cb60b2c657958fdaab0_250x400.jpg.13'

要注意内存问题,如果内存不是很大,就写个脚本定时清理一下/dev/shm下的文件吧。

一条评论

  1. arlen 2010年8月31日 17:33 回复

    学习了。我也在用VPS了

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据

Go