Nginx 巧用Linux内存加速静态文件访问

上一篇 / 下一篇  2012-05-18 08:40:53 / 个人分类:Linux

 nginx 静态文件处理能力是非常棒的,我们能不能进一步优化呢?静态文件的读取,会损耗IO资源。可以考虑把静态文件转移到linux内存中,每次从内存读取资源,效果应该会好很多。不过,系统重启时,内存文件会自动消失。针对这种情况,我们需要做个shell,在系统重启时,把静态文件拷贝到内存中。51Testing软件测试网&j'a-R [I.E k0y

  在给出shell示例之前,先做几个假设。nginx.conf中所配置站点的路径是/home/wwwroot/res,站点所对应文件原始存储路径:/opt/web/res51Testing软件测试网/uG[S[_/r

51Testing软件测试网 W?u$OWGPN+I$mJ

  开始编写拷贝内存initwebres脚本:51Testing软件测试网FW5y-ny(v_IS,m

vim /root/.bin/initwebres.sh

lPUm{"v\0  脚本内容如下:51Testing软件测试网 ~9b7fx%]

h)li0gB|0#! /bin/bash51Testing软件测试网6LF7]4^1q(F4l'{

51Testing软件测试网Ea~:`(Gh

res_path="/opt/web/res"
7tg0l6s+_[8w0mem_path="/dev/shm/res"51Testing软件测试网)_#UG9n8UP2uw4{
lk_path="/home/wwwroot/res"
51Testing软件测试网+a W3C,^i3l%P

51Testing软件测试网zoH _vC8A

if [ ! -d "$mem_path" ]; then
0x%_{)sO~9_0v0        cp -r "$res_path" "$mem_path"51Testing软件测试网&G&Q1N/AGy6l,c g
fi

u,T0R+IO Y0

Gc;v8vR)`.n?3J9q8c0if [ ! -L "$lk_path" ]; then
/V$cUe-TE:B0        ln -s "$mem_path" "$lk_path"
]j UX#Qh"QEzM0fi
51Testing软件测试网|B^D0kx+`*v

51Testing软件测试网7iW,r)]Vyc

  脚本写好之后,需要让脚本自启动。假设bash路径/bin/bash,然后在/etc/rc.local末尾添加:51Testing软件测试网{,e0yXy k7@

/bin/bash /root/.bin/initwebres.sh

V(L#Xad1Y`mEX0  通过上述操作,基本搞定一切。不过,更新网站静态文件时,切记要更新内存/dev/shm/中的相应文件哦!51Testing软件测试网;F"w5R$?:}4l


TAG:

 

评分:0

我来说两句

Open Toolbar