nginx实现负载均衡及静态资源处理
nginx安装
首先安装pcre
./configure
make
makeinstall
./configure--prefix=/usr/local/nginx--with-http_stub_status_module--with-http_ssl_module
make
makeinstall
nginx负载均衡配置
upstreamdyRes{
server127.0.0.1:8081;
server127.0.0.1:8082;
}
upstreamstaticRes{
server127.0.0.1:81;
server127.0.0.1:82;
}
server{
listen80;
server_namel27.0.0.1;
charsetgbk;
#access_loglogs/host.access.logmain;
location~*\.(ico|gif|bmp|jpg|jpeg|png|swf|js|css|html|htm)${
access_logon;
indexindex.htmlindex.htm;
proxy_passhttp://staticRes;
}
location~*\.(js)${
access_logon;
indexindex.htmlindex.htm;
proxy_passhttp://staticRes;
}
#dwr特殊处理
location^~/cprime/dwr{
access_logon;
indexindex.jsp;
proxy_passhttp://dyRes;
}
location~*\.(action|jsp)${
access_logon;
index/cprime/example/login.jsp;
proxy_passhttp://dyRes;
}
#设定查看Nginx状态的地址
location/NginxStatus{
stub_statuson;
access_logon;
auth_basic"NginxStatus";
#auth_basic_user_fileconf/htpasswd;
}
}
静态资源缓存配置
在serser同级添加
proxy_cache_path/usr/local/resource/cachelevels=1:2keys_zone=NAME:10minactive=1dmax_size=300m;
修改location如下
location~*^.+.(ico|gif|bmp|jpg|jpeg|png|swf)${
root/usr/local/resource/;
access_logon;
indexindex.htmlindex.htm;
expires5d;
proxy_cacheNAME;#使用NAME这个keys_zone
proxy_cache_valid2003021h;#200和302状态码保存1小时
proxy_cache_valid3011d;#301状态码保存一天
proxy_cache_validany1m;#其它的保存一分钟
}
proxy_cache用来缓存静态资源