`
lp895876294
  • 浏览: 280020 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

nginx集群和动静分离初步配置

 
阅读更多
  • nginx安装

        参考《Nginx教程从入门到精通》中的nginx安装说明,为了能够方便nginx中location的调试,最好安装nginx-echo模块,添加nginx-echo模块方式如下:

        1. 首先下载nginx-echo模块源码:https://github.com/agentzh/echo-nginx-module/tags

        2. 解压到某个路径,假设为 /path/to/echo-nginx-module

        3. $ ./configure --prefix=/opt/nginx --add-module=/path/to/echo-nginx-module

        4. make install

        经测试,即使在nginx安装完成之后,使用如上命令之后nginx的配置文件内容也不会被替换,只是增加了对echo模块的支持。

 

  • 使用nginx合并多个js和css请求为一个请求
         参考:https://github.com/alibaba/nginx-http-concat , 参考安装方式:
# cd /usr/local/src/
# wget http://nginx.org/download/nginx-1.4.2.tar.gz
# wget https://github.com/alibaba/nginx-http-concat/archive/master.zip -O nginx-http-concat-master.zip
# unzip nginx-http-concat-master.zip
# tar -xzvf nginx-1.4.2.tar.gz
# cd nginx-1.4.2
# ./configure --prefix=/usr/local/nginx-1.4.2 --with-http_stub_status_module \
--add-module=../nginx-http-concat-master
# make
# make install
        注:1. concat模块默认js文件的请求类型为application/x-javascript,而在nginx中js文件的请求类型为application/java-script,所以在配置文件中需要指定需要合并的文件类型。
               2.经测试concat只对通过root配置读取的静态文件有效,对于proxy_pass配置的文件获取方式无效。参考配置如下:
location /seajsdemo/resources {
    concat on;
    concat_types text/html application/javascript;
    root /usr/local/tomcat/webapps;
}
  
  • 动态资源和静态资源分离配置

       项目使用maven构建管理,静态资源放在resources文件夹下,现有的配置resources下的静态资源和后台请求进行了分离,简单配置如下:

worker_processes  2;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    keepalive_timeout  65;

    gzip  on;
	
	#cluser
    upstream tomcat_server{
		server 127.0.0.1:8080;
		server 127.0.0.1:8081;
    }
	#server listen
    server {
        listen       80;
        server_name  localhost;

        charset utf-8;
	
		#proxy static resource , load static resource from file system , 'project_web' is name of project
		location /project_web/resources {
			root /usr/local/tomcat-newcenter/webapps;
		}
		#dispatch request
		location / {
		   proxy_set_header Host $host;
		   proxy_set_header X-Forwarded-For $remote_addr;
		   proxy_pass http://tomcat_server;
		}
	}
}
  •  nginx反向代理集群中请求分发方式
1、轮询(默认)
每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。
2、weight
指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。
例如:
upstream bakend {
server 192.168.159.10 weight=10;
server 192.168.159.11 weight=10;
}
3、ip_hash
每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。
例如:
upstream resinserver{
ip_hash;
server 192.168.159.10:8080;
server 192.168.159.11:8080;
}
4、fair(第三方)
按后端服务器的响应时间来分配请求,响应时间短的优先分配。
upstream resinserver{
server server1;
server server2;
fair;
}
5、url_hash(第三方)
按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,后端服务器为缓存时比较有效。
例:在upstream中加入hash语句,server语句中不能写入weight等其他的参数,hash_method是使用的hash算法
upstream resinserver{
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}
tips:
upstream resinserver{#定义负载均衡设备的Ip及设备状态
ip_hash;
server 127.0.0.1:8000 down;
server 127.0.0.1:8080 weight=2;
server 127.0.0.1:6801;
server 127.0.0.1:6802 backup;
}
在需要使用负载均衡的server中增加
proxy_pass http://resinserver/;
每个设备的状态设置为:
1.down 表示单前的server暂时不参与负载
2.weight 默认为1.weight越大,负载的权重就越大。
3.max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
4.fail_timeout:max_fails次失败后,暂停的时间。
5.backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。
nginx支持同时设置多组的负载均衡,用来给不用的server来使用。
client_body_in_file_only 设置为On 可以讲client post过来的数据记录到文件中用来做debug
client_body_temp_path 设置记录文件的目录 可以设置最多3层目录
location 对URL进行匹配.可以进行重定向或者进行新的代理 负载均衡

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics