1.配置nginx实现负载均衡
实现效果:输入相同的地址,让他分发不同的tomcat服务器
1.1 创建两台tomcat服务器
1)开启后台tomcat1服务器:
[root@tomcat ~]# cd tomcat8080/apache-tomcat-7.0.105/webapps/edu/
[root@tomcat edu]# cat a.html
8080!!!
[root@tomcat edu]# vim a.html
[root@tomcat edu]# cat a.html
8080!!!server1
[root@tomcat edu]# cd ..
[root@tomcat webapps]# cd..
bash: cd..: command not found...
[root@tomcat webapps]# cd ..
[root@tomcat apache-tomcat-7.0.105]# cd bin/
[root@tomcat bin]# ./startup.sh
Using CATALINA_BASE: /root/tomcat8080/apache-tomcat-7.0.105
Using CATALINA_HOME: /root/tomcat8080/apache-tomcat-7.0.105
Using CATALINA_TMPDIR: /root/tomcat8080/apache-tomcat-7.0.105/temp
Using JRE_HOME: /usr
Using CLASSPATH: /root/tomcat8080/apache-tomcat-7.0.105/bin/bootstrap.jar:/root/tomcat8080/apache-tomcat-7.0.105/bin/tomcat-juli.jar
Tomcat started.
2)客户端测试:
3)开启后台tomcat2服务器:
[root@tomcat2 ~]# mkdir tomcat8081
[root@tomcat2 ~]# cp apache-tomcat-7.0.105.tar.gz tomcat8081/
[root@tomcat2 ~]# cd tomcat8081/
[root@tomcat2 tomcat8081]# ls
apache-tomcat-7.0.105.tar.gz
[root@tomcat2 tomcat8081]# tar zxf apache-tomcat-7.0.105.tar.gz
[root@tomcat2 tomcat8081]# ls
apache-tomcat-7.0.105 apache-tomcat-7.0.105.tar.gz
[root@tomcat2 tomcat8081]# cd apache-tomcat-7.0.105/conf/
[root@tomcat2 conf]# vim server.xml
connectionTimeout="20000"
redirectPort="8443" />
:wq
[root@tomcat2 conf]# cd
[root@tomcat2 ~]# cd tomcat8081/apache-tomcat-7.0.105/webapps/
[root@tomcat2 webapps]# mkdir edu
[root@tomcat2 webapps]# cd edu/
[root@tomcat2 edu]# vim a.html
[root@tomcat2 edu]# cat a.html
8081!!!server2
[root@tomcat2 edu]# cd..
bash: cd..: command not found...
[root@tomcat2 edu]# cd ..
[root@tomcat2 webapps]# cd ..
[root@tomcat2 apache-tomcat-7.0.105]# cd bin/
[root@tomcat2 bin]# ./startup.sh
Using CATALINA_BASE: /root/tomcat8081/apache-tomcat-7.0.105
Using CATALINA_HOME: /root/tomcat8081/apache-tomcat-7.0.105
Using CATALINA_TMPDIR: /root/tomcat8081/apache-tomcat-7.0.105/temp
Using JRE_HOME: /usr
Using CLASSPATH: /root/tomcat8081/apache-tomcat-7.0.105/bin/bootstrap.jar:/root/tomcat8081/apache-tomcat-7.0.105/bin/tomcat-juli.jar
Tomcat started.
4)客户端开启测试:
1.2 nginx负载均衡配置
1.2.1 轮询(默认)
1)修改nginx的配置文件:
[root@nginx ~]# cd /usr/local/nginx/conf/
[root@nginx conf]# ls
fastcgi.conf fastcgi_params.default mime.types nginx.conf.default uwsgi_params
fastcgi.conf.default koi-utf mime.types.default scgi_params uwsgi_params.default
fastcgi_params koi-win nginx.conf scgi_params.default win-utf
[root@nginx conf]# vim nginx.conf
http {
include mime.types;
default_type application/octet-stream;
upstream myserver {
server 172.25.5.15:8080;
server 172.25.5.16:8081;
}
server {
listen 80;
server_name 172.25.5.10;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
proxy_pass http://myserver;
index index.html index.htm;
}
}
:wq
2)启动nginx服务:
[root@nginx ~]# cd /usr/local/nginx/sbin/
[root@nginx sbin]# ./nginx
客户端进行访问测试
1)先访问一次:
2)再次访问:
1.2.2 weight权重策略
默认为1,权重越高被分配给的客户端越多:
1)配置文件中增加权重:
[root@nginx sbin]# vim /usr/local/nginx/conf/nginx.conf
upstream myserver {
server 172.25.5.15:8080 weight=1;
server 172.25.5.16:8081 weight=2;
}
2)重启nginx服务:
[root@nginx sbin]# ./nginx -s reload
3)客户端测试:
测试结果为server1访问一次,server2访问两次。
1.2.3 ip_hash
每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决 session 的问题。
也就是说让你的服务器每次访问指定的某一台服务器:
1)修改nginx 的配置文件:
[root@nginx sbin]# vim /usr/local/nginx/conf/nginx.conf
upstream myserver {
ip_hash;
server 172.25.5.15:8080;
server 172.25.5.16:8081;
}
2)重启nginx服务:
[root@nginx sbin]# ./nginx -s reload
3)测试结果:每次刷新都是第一次访问的server1,只有清除缓存,再次访问才会再次访问server2.
1.2.4 fair
按照后方服务器的访问时间进行分配,响应时间越短,越先分配:
1)修改nginx 的配置文件:
[root@nginx sbin]# vim /usr/local/nginx/conf/nginx.conf
upstream myserver {
server 172.25.5.15:8080;
server 172.25.5.16:8081;
fair;
}