反向代理
浏览器不直接访问下图192.168.20.20:8080,在浏览器地址栏输入www.test.com,跳转到tomcat主页(192.168.20.20:8080)
@nginx.conf文件组成
1)全局块
主要设置一些影响nginx服务器整体运行的配置,比如work_process 1; 值越大则支持的并发处理量越大
2)events块
主要设置nginx服务器与用户的网络连接的配置,比如worker_connections 1024; 支持的最大连接数
3)http块(配置最频繁的部分)
包括http全局块和server块
@tomcat安装(默认端口8080)
1)下载安装包
2)tar.gz解压 => bin/目录执行./startup.sh启动tomcat
3)对外开放8080访问端口
firewall-cmd --add-port=8080/tcp--permanent
firewall-cmd --reload
查看已开放端口
firewall-cmd --list-all
4)为了测试稍微修改主页的内容,例如
# cd /usr/local/src/apache-tomcat-8.5.54/webapps/ROOT
# vi index.jsp
修改index.jsp的内容, Home => Home-test
<body>
<div id="wrapper">
<div id="navigation" class="curved container">
<span id="nav-home"><a href="${tomcatUrl}">Home-test</a></span>
……
5)通过浏览器直接访问192.168.20.20:8080可以看到tomcat主页
说明:tomcat默认端口8080,可在conf/server.xml中修改
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
@配置步骤
浏览器在设备1,nginx在设备2,tomcat在设备3
【例1】实现上图例
1)设备1 hosts文件中进行域名和ip对应关系的配置,echo "192.168.4.2 www.test.com" > /etc/hosts
2)设备2 nginx进行请求转发的配置(反向代理)
编辑nginx.conf,访问192.168.4.28:80(第2、3行)转发到192.168.20.20:8080(第7行)
server{
listen 80;
server_name 192.168.4.28;
location / {
root html;
proxy_pass http://192.168.20.20:8080;
index index.html index.htm;
}
}
3)设备1 浏览器访问www.test.com
【例2】根据访问的路径跳转
访问http://www.test.com/flight/index.html 跳转到http://192.168.20.20:8080/flight/index.html
访问http://www.test.com/hotel/index.html 跳转到http://192.168.20.20:8080/hotel/index.html
1)设备3 在apache-tomcat-tomcat-8.5.54/webapps下创建文件夹和测试页面
[root@xxx webapps]# mkdir flight
[root@xxx webapps]# mkdir hotel
[root@xxx webapps]# cd flight/
[root@xxx flight]# echo "<h1>flight</h1>" > index.html
[root@xxx flight]# cd ../hotel/
[root@xxx hotel]# echo "<h1>hotel</h1>" > index.html
2)设备2 编辑nginx.conf,反向代理配置
server{
listen 80;
server_name 192.168.4.28;
location ~ /flight/ {
proxy_pass http://192.168.20.20:8080
}
location ~ /hotel/ {
proxy_pass http://192.168.20.20:8080
}
}
说明:
~ 表示URI包含正则表达式,区分大小写
~* 表示URI包含正则表达式,不区分大小写
3)设备1 分别打开两个页面
访问http://www.test.com/flight/index.html
访问http://www.test.com/hotel/index.html
负载均衡
配置三台tomcat,测试负载均衡其中tomcat1部署在一台虚拟机,tomcat2和tomcat3部署在另一台虚拟机,如下
tomcat1 192.168.20.19:8080
tomcat2 192.168.20.20:8080
tomcat3 192.168.20.20:8090
说明:
1.为了测试方便,对应地修改主页内容
2.因为tomcat2和tomcat3部署在同一台虚拟机,修改tomcat3的server.xml中涉及的端口,然后分别启动
<Server port="8005" shutdown="SHUTDOWN"> 中8005 => 8015
<Connector port="8080" protocol="HTTP/1.1"...> 中8080 => 8090
[root@xxx ROOT]# cd ../../bin/
[root@xxx bin]# ./startup.sh
Using CATALINA_BASE: /usr/local/src/apache-tomcat-3
Using CATALINA_HOME: /usr/local/src/apache-tomcat-3
Using CATALINA_TMPDIR: /usr/local/src/apache-tomcat-3/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/src/apache-tomcat-3/bin/bootstrap.jar:/usr/local/src/apache-tomcat-3/bin/tomcat-juli.jar
Tomcat started.
[root@xxx bin]# cd ..
[root@xxx apache-tomcat-3]# cd ..
[root@xxx src]# cd apache-tomcat-2/bin
[root@xxx bin]# ./startup.sh
Using CATALINA_BASE: /usr/local/src/apache-tomcat-2
Using CATALINA_HOME: /usr/local/src/apache-tomcat-2
Using CATALINA_TMPDIR: /usr/local/src/apache-tomcat-2/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/src/apache-tomcat-2/bin/bootstrap.jar:/usr/local/src/apache-tomcat-2/bin/tomcat-juli.jar
Tomcat started.
[root@xxx bin]#
[root@xxx bin]# netstat -ntl | grep 8080
tcp 0 0 :::8080 :::* LISTEN
[root@xxx bin]# netstat -ntl | grep 8090
tcp 0 0 :::8090 :::* LISTEN
[root@xxx bin]#
编辑 /usr/local/nginx/conf/nginx.conf
upstream tomcatlist{
server 192.168.20.19:8080 weight=1;
server 192.168.20.20:8080 weight=1;
server 192.168.20.20:8090 weight=2;
}
server {
listen 80;
server_name www.test.com;
location / {
proxy_pass http://tomcatlist; # http:// + upstream后的名称
}
……
为了测试方便,对应地修改
浏览器打开www.test.com,会根据配置去连后台的tomcat,有三种可能
连接到tomcat1
连接到tomcat2
连接到tomcat3
tomcat1,2,3的权重比是1:1:2,预期每4次打开www.test.com,会有两次连到tomcat3,分别一次连到tomcat1,2
测试结果:浏览器8次打开www.test.com,如下,符合配置规则
第1次 tomcat3
第2次 tomcat1
第3次 tomcat3
第4次 tomcat2
第5次 tomcat3
第6次 tomcat1
第7次 tomcat3
第8次 tomcat2
@关于upstream
除了weight控制负载均衡,还可以使用如下方式
ip_hash 同一个IP访问固定的tomcat,同一个访客登录固定访问一个后端tomcat,例如登录之后进行其它操作不会因为换了服务器而没有登录
fair(第三方)按后端服务器的响应时间来分配请求,响应时间短的优先分配。
url_hash(第三方)按访问url的hash结果来分配请求
参考资料:
Nginx教程 https://www.bilibili.com/video/BV1zJ411w7SV
Nginx配置upstream实现负载均衡 https://www.cnblogs.com/wzjhoutai/p/6932007.html
tomcat,nginx,apache的联系 https://www.zhihu.com/question/32212996