负载均衡的策略:轮询(默认)、权重、IP_HASH
1、配置nginx
2、配置tomcat8081
3、配置tomcat8080
4、配置nginx的conf 设置为轮询
5、配置nginx的conf 设置为权重
6、配置nginx的conf 设置为IP_HASH
1、配置nginx
version: '3.1'
services:
nginx01:
restart: always
image: daocloud.io/library/nginx:latest
container_name: nginx01
ports:
- 80:80
environment:
TZ: Asia/Shanghai
volumes:
- /root/nginx/conf.d:/etc/nginx/conf.d
~
2、配置tomcat8081
version: '3.1'
services:
tomcat8081:
restart: always
image: daocloud.io/library/tomcat:8.0.45
container_name: tomcat8081
ports:
- 8081:8080
environment:
TZ: Asia/Shanghai
volumes:
- /root/tomcat8081/webapps:/usr/local/tomcat/webapps/
3、配置tomcat8080
version: '3.1'
services:
tomcat8080:
restart: always
image: daocloud.io/library/tomcat:8.0.45
container_name: tomcat8080
ports:
- 8080:8080
environment:
TZ: Asia/Shanghai
volumes:
- /root/tomcat8080/webapps:/usr/local/tomcat/webapps/
~
4、配置nginx的conf 设置为轮询
upstream tomcat80 {
server 192.168.181.138:8080;
server 192.168.181.138:8081;
}
server {
listen 80; #监听端口是80
listen [::]:80; #
server_name localhost; #接受请求的IP
location / {
proxy_pass http://tomcat80/muop/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
5、配置nginx的conf 设置为权重
upstream tomcat80 {
server 192.168.181.138:8080 weight=10;
server 192.168.181.138:8081 weight=2;
}
server {
listen 80; #监听端口是80
listen [::]:80; #
server_name localhost; #接受请求的IP
location / {
proxy_pass http://tomcat80/muop/;
#proxy_pass http://192.168.181.138:8081/muop/;
# root /usr/share/nginx/html; #root 将接受到的请求根据 /usr/share/nginx/html去查找静态资源
#index index.html index.htm; #查找index.html 和 index.htm 响应给客户
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
6、配置nginx的conf 设置为IP_HASH: http://192.168.181.138/
upstream tomcat80 {
ip_hash;
server 192.168.181.138:8080 weight=10;
server 192.168.181.138:8081 weight=2;
}
server {
listen 80; #监听端口是80
listen [::]:80; #
server_name localhost; #接受请求的IP
location / {
proxy_pass http://tomcat80/muop/;
#proxy_pass http://192.168.181.138:8081/muop/;
# root /usr/share/nginx/html; #root 将接受到的请求根据 /usr/share/nginx/html去查找静态资源
#index index.html index.htm; #查找index.html 和 index.htm 响应给客户
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}