读书人

nginx兑现负载均衡及静态资源处理

发布时间: 2012-09-04 14:19:30 作者: rapoo

nginx实现负载均衡及静态资源处理
nginx 安装
首先安装pcre
./configure
make
make install
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make
make install

nginx 负载均衡配置

upstream dyRes {

server 127.0.0.1:8081;

server 127.0.0.1:8082;
}

upstream staticRes {

server 127.0.0.1:81;

server 127.0.0.1:82;
}

server {
listen 80;
server_name l27.0.0.1;

charset gbk;

#access_log logs/host.access.log main;


location ~* \.(ico|gif|bmp|jpg|jpeg|png|swf|js|css|html|htm)$ {

access_log on;

index index.html index.htm;

proxy_pass http://staticRes ;

}

location ~* \.(js)$ {

access_log on;

index index.html index.htm;

proxy_pass http://staticRes ;

}

#dwr 特殊处理
location ^~/cprime/dwr {

access_log on ;

index index.jsp ;

proxy_pass http://dyRes ;
}

location ~* \.(action|jsp)$ {

access_log on ;

index /cprime/example/login.jsp ;

proxy_pass http://dyRes ;
}
#设定查看Nginx状态的地址
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
# auth_basic_user_file conf/htpasswd;
}
}

静态资源缓存配置
在serser同级添加
proxy_cache_path /usr/local/resource/cache levels=1:2 keys_zone=NAME:10m inactive=1d max_size=300m ;

修改location如下
location ~* ^.+.(ico|gif|bmp|jpg|jpeg|png|swf)$ {

root /usr/local/resource/;

access_log on;

index index.html index.htm;

expires 5d;

proxy_cache NAME;#使用NAME这个keys_zone

proxy_cache_valid 200 302 1h;#200和302状态码保存1小时

proxy_cache_valid 301 1d;#301状态码保存一天

proxy_cache_valid any 1m;#其它的保存一分钟

}

proxy_cache 用来缓存静态资源

读书人网 >互联网

热点推荐