---
title: "nginx实现负载均衡及静态资源处理"
author: "cjj_720"
date: "2010-08-05T10:34:04Z"
tags: []
categories: ["互联网"]
slug: "nginx"
draft: false
lastmod: "2010-08-05T14:25:47Z"
id: 729533
url: "https://cprime.iteye.com/blog/729533"
---

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 用来缓存静态资源