Linuxのレコード-Nginx + Tomcatの負荷分散の設定

nginxの構成と負荷分散戦略:

ポーリング(デフォルト)

利点:簡単
欠点:各サーバの処理容量を考慮していない
次のような構成例を:
上流www.xxx.com {
負荷サーバリストによって必要#
サーバwww.xxx.com:8080、
サーバwww.xxx.com:9080;
}
重量は、より多くの戦略が使用する
利点が各サーバの処理能力のために異なる考慮事項高い重量機械する権利を与える、高性能の機械
構成例は以下の通り:
上流www.xxx.com {
#サーバをロードする必要がリストには、重量が重みの複数のノードを配置した場合、相対値比較、1重量、重量のデフォルト値を表し
、サーバーwww.xxx.com:8080重量= 15
;サーバーwww.xxx.com:9080重量= 10
}
ハッシュIP
利点を一と同じユーザが常に同じサーバにアクセスでき
欠点:IPハッシュを必ずしも意味するものではない
以下のような構成例を:
上流www.xxx.com {
ip_hash;
負荷サーバリストによって必要とされる
、サーバwww.xxx.com:8080
サーバWWWを。 xxx.com:9080;
}
(サードパーティのプラグイン)ハッシュをURL
長所:負荷のURLに基づいているのと同じサーバにアクセスするために同じサービスを、達成することができます
短所:IPハッシュと同じ、必ずしも平均要求を、URLハッシュ割り当てに基づいて、同じサーバへのリクエストURL頻繁に要求し
、次のような構成の例がある(必須プリインストールプラグイン)
上流www.xxx.com {
負荷サーバリストによって必要#
サーバwww.xxx.com:8080、
サーバwww.xxx.com:9080;
;ハッシュ$ REQUEST_URI
}
フェア(サードパーティのプラグイン)
を押し:特性サーバ側の応答時間優先割当要求に割り当てられ、応答時間が短く、
次のような構成例(プラグインをインストールする必要がある)
www.xxx.com {上流
負荷サーバリストによって必要#
サーバwww.xxx.com:8080、
サーバwww.xxx .COM:9080;
フェア;
}
はじめのいくつかのロード・バランシング・パラメーター:

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

 

 刷新页面

 

おすすめ

転載: www.cnblogs.com/xinfang520/p/11085641.html