nginx 反向代理置小示例(将文件缓存在内存中)
转载自:http://www.linuxtone.org/html/42/t-4042.html
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 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_250×400.jpg
将缓存/dev/shm/proxy_temp/st001.img.linuxtone.org/目录下.
[root@www tmp]# wget -S http://st001.img.linuxtone.org/thumbnail/55/cd/2e/55cd2ecbb6d24cb60b2c657958fdaab0_250×400.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.
QUOTE:
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_250×400.jpg.13′
要注意内存问题,如果内存不是很大,就写个脚本定时清理一下/dev/shm下的文件吧。
如是你用nginx 做反向代理,挑一个大内存的机器是一个不错的选择哦!
欢迎朋友们讨论补充。