本文描述了如何使用Nginx实现在应用层实现7层负载均衡功能,Nginx支持虚拟主机,可以按照轮询,IP哈希,URL哈希,权重方式对后端服务器做负载均衡,还支持后端服务器健康检查功能。废话不多说,详细配置见下文~ 测试模型如下: http://fj.chaobaihe.net 潮白河点评网
1、一台Nginx做负载负载均衡代理 具体配置centos5.8 ip 10.0.211.5 2、三台tomcat做业务逻辑处理 具体配置centos5.8 10.0.211.2 10.0.211.3 10.0.211.4 具体部署步骤:
http://fj.chaobaihe.net 潮白河点评网1、 安装正则表达式支持库nginx支持rewrite重写 [root@server soft]# tar xzvf pcre-7.8.tar.gz #解压安装包 [root@server pcre-7.9]# cd pcre-7.9 #进入解压后的安装包 [root@server pcre-7.9]# ./configure #配置安装环境 [root@server pcre-7.9]# make && make install #编译安装
http://news.chaobaihe.net 潮白新闻网2、 安装nginx [root@server soft]# tar -zxvf nginx-1.3.3.tar.gz #解压安装包 [root@server soft]# cd nginx-1.3.3 [root@server nginx-1.3.3]# ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module #配置编译环境 [root@server nginx-1.3.3]# make && make install #编译安装
本文章来源于潮白家园网: http://www.chaobaihe.net3、配置Nginx 详细配置参数如下所示: user www www; #启动用户 #user nobody; worker_processes 48; #进程数量 error_log logs/error.log crit; #日志路径 #error_log logs/error.log notice; #error_log logs/error.log info; pid logs/nginx.pid; #启动pid存储文件 worker_rlimit_nofile 65536; #允许用户打开的最大句柄 events { use epoll; #查询模式 worker_connections 65536; #最大句柄,允许连接的最大文件数量 } http { include mime.types; default_type application/octet-stream; charset UTF-8; server_names_hash_bucket_size 128; #根据cpu一级缓存设置 ubuntu可以使用lscpu查看 client_header_buffer_size 4; large_client_header_buffers 4 8k; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #指定nginx是否调用sendfile函数(zero copy方式)来输出文件,普通应用必须设为on,对于普通文件用on。如果进行下载I/O负载应用,设置为off,以平衡磁盘磁盘和网络I/O处理速度。 #tcp_nopush on; #允许或禁止使用socket的TCP_NOPUSH仅当sendfile 为on时设置改选项生效 keepalive_timeout 65; #超时时间 tcp_nodelay on; fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; #keepalive_timeout 0; gzip on; #允许压缩传输文件 gzip_min_length 1k; #设置允许压缩最小字节数,0表示多大都压,最好设置大于1k,小于1k越压越大 gzip_buffers 4 16k; gzip_http_version 1.1; #判断http协议版本,是否支持压缩,否则用户看到乱码。默认即可 gzip_comp_level 2; #gzip压缩比,1 压缩比最小处理速度最快,9 压缩比最大但处理最慢 gzip_types text/plain application/x-javascript text/css application/xml; gzip_vary on; #加个vary头,给代理服务器用的,有的浏览器支持压缩,有的不支持,所以避免浪费不支持的也压缩,所以根据客户端的HTTP头来判断,是否需要压缩 client_max_body_size 50m; #允许客户端请求的最大单个文件字节数 client_body_buffer_size 128k; #缓冲区代理用户端请求的最大字节数,可以理解先保存到本地在发送给用户 proxy_connect_timeout 600; #后端服务器连接的超时时间,发起握手等候相应超时时间 proxy_read_timeout 600; 连接成功后,等待后端服务器相应时间,已经进入后端的排队之中等候处理 proxy_send_timeout 600; #后端服务器数据回传时间,在规定时间内服务器必须传完所有数据 proxy_buffer_size 16k; #保存用户头信息供nginx进行规则处理,用于缓存代理请求 proxy_buffers 4 32k; #nginx保存每个用户Buffer最大空间 proxy_busy_buffers_size 64k; #如果系统很忙可以申请更大的proxy_buffers,官方推荐 *2 proxy_temp_file_write_size 64k; upstream fengzhanhai{ #负载均衡池 # ip_hash; server 10.0.211.2 down weight=4 max_fails=2 fail_timeout=30s; server 10.0.211.2:8080 weight=4 max_fails=2 fail_timeout=30s; # server 10.0.211.3:8080 weight=4 max_fails=2 fail_timeout=30s; # server 10.0.211.4:8080 weight=4 max_fails=2 fail_timeout=30s; # server 192.168.202.81:8080 weight=2 max_fails=2 fail_timeout=30s; server 192.168.202.82 weight=4 max_fails=2 fail_timeout=30s; } server { #虚拟主机反向代理负载均衡池fengzhanhai的主机应用 listen 80; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_next_upstream error timeout invalid_header http_500 http_503 http_404; #当分发的后端业务宕机后无缝切换到pool中存活的业务之上 #反向代理资源池 proxy_set_header X-Forwarded-For $remote_addr; root html; index index.html index.htm; } access_log logs/feng.log combined; #访问日志 #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443; # server_name localhost; # ssl on; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_timeout 5m; # ssl_protocols SSLv2 SSLv3 TLSv1; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} } 4、 测试Nginx配置语法是否正常 5、 ./nginx –t 返回如下信息表示参数配置无误 nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful 6、 启动nginx进行测试./nginx 7、 Nginx负载均衡知识拓展 proxy_set_header Host $host :首先说明 proxy_set_header 指令在向反向代理的后端Web服务器发起请求时添加指定的 Header头信息,后端web服务器有多个基于域名的虚拟主机时,通过头信息Host,用于指定请求的域名,这样后端web才能识别反向代理请求哪个虚拟主机处理。 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for :在nginx反向代理添加Header头信息 X-Forwarded-For在配合后端服务器日志文件的$http_x_Forwarded_for这条就可以获得用户的IP地址
http://fj.chaobaihe.net 潮白河点评网8、 nginx支持的几种算法分析 1、 轮询 每个请求按时间顺序分配到不同的后端服务器了,后端服务器down掉,自动切除; 2、weight 设定服务器权值: 如weight=2 服务器性能不均时候使用 。weight: 默认为1,weight越大,负载的权重越大; 3、 ip_hash 每个请求按访问ip的hash结果分配,每个访客有固定的后端服务器,可以解决session问题; 4、 fair(第三方) 按后端服务器的响应时间来分配,响应时间短的优先分配 5、url_hash (第三方) 按访问的url的hash结果分配,使每个url定向到同一个后端服务器,后端为缓存服务器比较有效。
http://house.chaobaihe.net 潮白房产网详细参数介绍: 1)down : 当前的IP server暂时不参与负载,不进行反向代理; 2)max_fails: 允许请求失败的次数默认为1,当超过最大次数时,返回proxy_next_upstream模块定义的错误; 3)fail_timeout : max_fails次失败后,暂停的时间; 4)backup: 其它所有非backup机器down或者忙时候,请求backup机器,这台机器压力最轻。 |