反向代理与负载均衡
反向代理
反向代理服务器位于用户与目标服务器之间,但是对于用户而言,反向代理服务器就相当于目标服务器,即用户直接访问反向代理服务器就可以获得目标服务器的资源。
Nginx配置实例—反向代理:
实验环境:(全部服务器都采用centos6)
- 一台Nginx服务器(192.168.110.111)
- 一台Tomcat服务器(192.168.110.222)
- 测试:使用Windows进行测试,在浏览器中输入www.reverseproxy.com,经反向代理后,出现tomcat初始化界面
一、开启Nginx服务
[root@tomcat1 ~]# service start nginx
二、进入nginx配置文件nginx/conf/nginx.conf,配置域名解析
[root@tomcat1 ~]# vim /usr/local/nginx/conf/nginx.conf
只要修改nginx的配置文件后一定要,reload一下nginx,使刚刚的配置文件生效
[root@tomcat1 ~]# service nginx reload
三、修改windows中hosts文件,文件路径C:\Windows\System32\drivers\etc\hosts
四、测试域名是否生效,测试前要保证网络是通的!
五、在第二台虚拟机上开启tomcat服务
[root@tomcat1 ~]# ./apache-tomcat-8.5.54/bin/startup.sh
六、修改nginx配置文件,配置反向代理,指向tomcat服务器
[](https://imgchr.com/i/UsuAwn
重新加载nginx配置文件
[root@tomcat1 ~]# service nginx reload
结果测试:
为了测试时,使效果明显,在此修改了,tomcat的默认界面。tomcat默认界面位置:apache-tomcat-8.5.54/webapps/ROOT/index.jsp
记得重启tomcat服务,使index.jsp生效
负载均衡
负载均衡建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。
Nginx配置实例—负载均衡:
实验环境:
- 一台Nginx服务器(192.168.110.111)
- 两台Tomcat服务器(192.168.110.222 192.168.110.223)
- 测试:使用Windows进行测试,在浏览器中输入www.reverseproxy.com,经负载均衡后,展现tomcat界面
一、开启两台服务器的tomcat服务
[root@tomcat1 ~]# ./apache-tomcat-8.5.54/bin/startup.sh
二、为了验证负载均衡是否生效,修改tomcat默认界面,并重启
[root@tomcat1 bin]# pwd
[root@tomcat1 bin]# /root/apache-tomcat-8.5.54/bin
[root@tomcat1 bin]# ./shutdown.sh
[root@tomcat1 bin]# ./startup.sh
三、使用nginx的upstream模块,实现负载均衡。upstream定义了负载应用的模块
weight是权重,weight代表的是权值。缺省情况下,weight=1
upstream tomcat-server{
server 192.168.110.222:8080 weight=5;
server 192.168.110.223:8080 weight=5;
}
server {
listen 80;
server_name www.reverseproxy.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://tomcat-server;
}
}
四、重新加载nginx的配置文件并测试
[root@nginx ~]# service nginx reload
重新载入 nginx: [确定]