Nginx configuration and load balancing strategies:
Polling (default)
Advantages: simple
drawback: not considering processing capacity of each server
configuration example as follows:
upstream www.xxx.com {
# required by the load server list
server www.xxx.com:8080;
server www.xxx.com:9080;
}
weight, more strategies to use
advantages: the different considerations for each server processing power, high-performance machines which give the right to which high weight machine
configuration examples are as follows:
upstream www.xxx.com {
# need to load the server list, weight represents the weight, weight defaults to 1, if a plurality of weights arranged nodes, compare the relative value
Server www.xxx.com:8080 weight = 15;
Server www.xxx.com:9080 weight = 10;
}
IP the hash
advantages : one and the same user can always access the same server
drawback: the ip hash does not necessarily mean
a configuration example as follows:
upstream www.xxx.com {
ip_hash;
# required by the load server list
server www.xxx.com:8080;
server WWW. xxx.com:9080;
}
URL the hash (third-party plug-ins)
Advantages: can achieve the same service to access the same server, which is based on load url
Cons: ip hash and the same, not necessarily the average request, the request url frequent requests to the same server based on url hash assignment
configuration examples are as follows (required pre-installed plug-in)
upstream www.xxx.com {
# required by the load server list
server www.xxx.com:8080;
server www.xxx.com:9080;
; the hash $ REQUEST_URI
}
Fair (third-party plug-ins)
press: characteristics the response time of the server side is preferentially allocated to an allocation request, the response time is short,
a configuration example as follows (need to install plug)
upstream www.xxx.com {
# required by the load server list
server www.xxx.com:8080;
server www.xxx .com: 9080;
Fair;
}
Some load-balancing parameters of Introduction:
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
刷新页面