1、實驗環境
Nginx 192.168.1.101 80
tomcat01 192.168.1.102 8080
tomcat02 192.168.1.103 8080
2、負載均衡主流算法
輪詢,權重,IP雜湊演算法,最少連接配接數,fair(第三方插件)
3、輪詢
配置nginx.conf
vi /usr/local/nginx/conf/nginx.conf
配置内容如下
#輪詢
upstream web {
server 192.168.1.102:8080;
server 192.168.1.103:8080;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
proxy_pass http://web; #此處配置位址為上面的自定義名稱web
index index.html index.htm;
}
}
測試配置
192.168.1.102
vi /usr/local/tomcat9/webapps/ROOT/index.html
配置内容如下
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>淘淘網</title>
</head>
<body>
淘淘網001号伺服器
</body>
</html>
192.168.1.103
vi /usr/local/tomcat9/webapps/ROOT/index.html
配置内容如下
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>淘淘網</title>
</head>
<body>
淘淘網002号伺服器
</body>
</html>
效果
備注:如果隻輪詢一台,請備份tomcat安裝檔案裡的webapps下ROOT目錄,然後删除ROOT目錄下所有檔案隻保留index.html檔案
4、權重
192.168.1.102為五分之四
192.168.1.103為五分之一
配置nginx.conf
vi //usr/local/nginx/conf/nginx.conf
配置内容如下
#權重
upstream web {
server 192.168.1.102:8080 weight=4;
server 192.168.1.103:8080;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# root html;
proxy_pass http://web;
index index.html index.htm;
}
}
5、雜湊演算法
請求按通路ip的hash結果配置設定,這樣每個IP固定通路一 個應用伺服器,可以解決session共享的問題,配置如下
因為通路者的IP位址是固定的,那麼根據IP位址的Hash值也是感光固定的,那 麼一個ip就一直通路對應的那個伺服器,不會跨伺服器
配置nginx.conf
vi /usr/local/nginx/conf/nginx.conf
配置内容如下
#權重
upstream web {
ip_hash;
server 192.168.1.102:8080;
server 192.168.1.103:8080;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# root html;
proxy_pass http://web;
index index.html index.htm;
}
}
6、最少連接配接數
配置nginx.conf
vi /usr/local/nginx/conf/nginx.conf
配置内容如下
#權重
upstream web {
least_conn;
server 192.168.1.102:8080;
server 192.168.1.103:8080;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# root html;
proxy_pass http://web;
index index.html index.htm;
}
}
7、fair(第三方插件)
配置nginx.conf
vi /usr/local/nginx/conf/nginx.conf
配置内容如下
upstream web {
server 192.168.1.102:8080;
server 192.168.1.103:8080;
fair;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# root html;
proxy_pass http://web;
index index.html index.htm;
}
}
8、負載均衡備機和當機配置
備機backup配置
當其他非backup機器挂掉後,才會請求backup機器
配置nginx.conf
vi /usr/local/nginx/conf/nginx.conf
配置内容如下
upstream web {
server 192.168.1.102:8080;
server 192.168.1.103:8080 backup;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# root html;
proxy_pass http://web;
index index.html index.htm;
}
}
當機down配置
配置down的伺服器不參與負載均衡,這樣down所标記的伺服器就可以安心的更新了
配置nginx.conf
vi /usr/local/nginx/conf/nginx.conf
配置内容如下
upstream web {
server 192.168.1.102:8080;
server 192.168.1.103:8080 down;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
# root html;
proxy_pass http://web;
index index.html index.htm;
}
}