Nginx负载均衡配置及策略:
轮询(默认)
优点:实现简单
缺点:不考虑每台服务器的处理能力
配置示例如下:
upstream www.xxx.com {
# 需要负载的server列表
server www.xxx.com:8080;
server www.xxx.com:9080;
}
权重,使用的较多的策略
优点:考虑了每台服务器处理能力的不同,哪台机器性能高就给哪台机器的权重高一些
配置示例如下:
upstream www.xxx.com {
# 需要负载的server列表,weight表示权重,weight默认为1,如果多个配置权重的节点,比较相对值
server www.xxx.com:8080 weight=15;
server www.xxx.com:9080 weight=10;
}
ip hash
优点:能实现同一个用户始终访问同一个服务器
缺点:根据 ip hash 不一定平均
配置示例如下:
upstream www.xxx.com {
ip_hash;
# 需要负载的server列表
server www.xxx.com:8080;
server www.xxx.com:9080;
}
url hash (第三方插件)
优点:能实现同一个服务访问同一个服务器,也就是根据url进行负载
缺点:和ip hash一样,根据 url hash 分配请求不一定平均,请求频繁的url会请求到同一台服务器上
配置示例如下(需要事先安装插件)
upstream www.xxx.com {
# 需要负载的server列表
server www.xxx.com:8080;
server www.xxx.com:9080;
hash $request_uri;
}
fair (第三方插件)
特点:按后端服务器的响应时间来分配请求,响应时间短的优先分配
配置示例如下(需要事先安装插件)
upstream www.xxx.com {
# 需要负载的server列表
server www.xxx.com:8080;
server www.xxx.com:9080;
fair;
}
一些负载均衡参数简介:
upstream www.xxx.com {
ip_hash;
# 需要负载的server列表
server www.xxx.com:8080 down; # down表示当前的server暂时不参与负载
server www.xxx.com:9080 weight=2; # weight默认值为1,weight的值越大,负载的权重就越大
server www.xxx.com:7080 backup; # 其他所有的非backup机器,在down掉或者很忙的时候,才请求backup机器,也就是一个备用机器
server www.xxx.com:6080;
}
1.安装两个tomcat实例
要修改8005 8080 8009三个端口 以免冲突
修改index.jsp分别加入<h2>I 'm 8088</h2> <h2>I 'm 8089</h2>
启动服务:sh startup.sh
2.配置nginx
nginx.conf
user nobody nobody;
worker_processes 2;
#error_log /usr/local/nginx/logs/nginx_error.log crit;
#pid /usr/local/nginx/logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 6000;
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 3526;
server_names_hash_max_size 4096;
log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
' $host "$request_uri" $status'
' "$http_referer" "$http_user_agent"';
sendfile on;
tcp_nopush on;
keepalive_timeout 30;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
connection_pool_size 256;
client_header_buffer_size 1k;
large_client_header_buffers 8 4k;
request_pool_size 4k;
output_buffers 4 32k;
postpone_output 1460;
client_max_body_size 10m;
client_body_buffer_size 256k;
#client_body_temp_path /usr/local/nginx/client_body_temp;
#proxy_temp_path /usr/local/nginx/proxy_temp;
#fastcgi_temp_path /usr/local/nginx/fastcgi_temp;
fastcgi_intercept_errors on;
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 8k;
gzip_comp_level 5;
gzip_http_version 1.1;
gzip_types text/plain application/x-javascript text/css text/htm
application/xml;
add_header Access-Control-Allow-Origin *;
include /etc/nginx/conf.d/*.conf;
}
vim /etc/nginx/conf.d/tomcat.conf
upstream 192.168.66.128 {
server 192.168.66.128:8088 weight=1;
server 192.168.66.128:8089 weight=3;
}
server {
listen 80;
autoindex on;
server_name _;
access_log /usr/local/nginx/logs/access.log combined;
index index.html index.htm index.jsp;
root /usr/share/nginx/html/;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://192.168.66.128;
add_header Access-Control-Allow-Origin *;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
nginx -t 检测配置是否正确
启动服务 nginx
查看端口是否监听
lsof -i:80
lsof -i:89
然后浏览器输入
192.168.66.128
刷新页面