1.反向代理多机
实验:Nginx要开启upstream(负载均衡)、location(url链接)、proxy_pass(反向代理)
配置:7-3做代理服务器;7-1 和 7-2做Tomcat服务器
关闭防火墙和selinux
1.准备配置
7-3安装nginx;7-1 和 7-2安装Tomcat;都开启服务!
###服务安装,可参考前面文章
7-3
7-1
7-2
2.配置7-3Nginx服务器
vim /etc/nginx/conf/nginx.conf17 http {18 upstream tomcat {19 server 192.168.91.100:8080;20 server 192.168.91.102:8080;21 }48 location ~* \.(jpg|png)$ {49 root /data/html;50 }51 52 location / {53 proxy_pass http://tomcat;54 }
nginx -t
nginx -s reload
3.建立动态资源存放位置(本机中)
[root@localhost ~]# mkdir /data/html
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@localhost ~]# nginx -s reload
[root@localhost ~]# cd /data/html
[root@localhost html]# vim test.html
[root@localhost html]# cat test.html
test test
[root@localhost html]#
4.配置7-2Tomcat服务器
[root@localhost ~]# cd /usr/local/tomcat/webapps/ROOT
[root@localhost ROOT]#
[root@localhost ROOT]# ls
asf-logo-wide.svg bg-upper.png tomcat.css tomcat.svg
bg-button.png favicon.ico tomcat.gif WEB-INF
bg-middle.png index.jsp tomcat.png
bg-nav.png RELEASE-NOTES.txt tomcat-power.gif
[root@localhost ROOT]#
[root@localhost ROOT]# vim test.jsp
[root@localhost ROOT]# cat test.jsp
7-2
[root@localhost ROOT]#
5.配置7-1Tomcat服务器
[root@centos1 ~]# cd /usr/local/tomcat/webapps/ROOT
[root@centos1 ROOT]# ls
asf-logo-wide.svg bg-upper.png tomcat.css tomcat.svg
bg-button.png favicon.ico tomcat.gif WEB-INF
bg-middle.png index.jsp tomcat.png
bg-nav.png RELEASE-NOTES.txt tomcat-power.gif
[root@centos1 ROOT]# vim test.jsp
[root@centos1 ROOT]# cat test.jsp
7-1
[root@centos1 ROOT]#
6.Nginx服务器7-3,curl去访问验证
[root@localhost ~]# curl 192.168.91.103/test.html
test test
[root@localhost ~]# curl 192.168.91.103/test.jsp
7-1
[root@localhost ~]# curl 192.168.91.103/test.jsp
7-2
[root@localhost ~]# curl 192.168.91.103/test.jsp
7-1
[root@localhost ~]#
2.反向代理多机多级
实验:7-3为Nginx代理服务器;7-4和7-5为Nginx服务器;7-1和7-2为Tomcat服务器
配置:给7-4和7-5安装Nginx;其他主机的服务也得配置和打开!
关闭防火墙和selinux
1.准备配置
先做第一层;7-3Nginx代理服务器传内容给7-4和7-5Nginx服务器
2.配置7-3Nginx代理服务器
vim /etc/nginx/nginx.conf17 http {18 upstream web {19 server 192.168.91.104;20 server 192.168.91.105;21 }48 location / {49 proxy_pass http://web;50 }51
nginx -t
nginx -s reload
3.配置7-4Nginx服务器
[root@centos4 ~]# vim /usr/share/nginx/html/index.html
[root@centos4 ~]#
[root@centos4 ~]# cat /usr/share/nginx/html/index.html
7-4
[root@centos4 ~]#
4.配置7-5Nginx服务器
[root@centos5 ~]# vim /usr/share/nginx/html/index.html
[root@centos5 ~]#
[root@centos5 ~]# cat /usr/share/html/index.html
cat: /usr/share/html/index.html: 没有那个文件或目录
[root@centos5 ~]# cat /usr/share/nginx/html/index.html
7-5
[root@centos5 ~]#
5.7-3curl访问,验证第一层配置(Nginx代理服务器 传内容给 Nginx服务器)
[root@localhost ~]# curl 192.168.91.103
7-4
[root@localhost ~]# curl 192.168.91.103
7-5
[root@localhost ~]# curl 192.168.91.103
7-4
[root@localhost ~]#
做第二层;7-4和7-5Nginx服务器传内容给7-1和7-2Tomcat服务器
6.配置7-4Nginx服务器
vim /etc/nginx/nginx.conf29 upstream tomcat {30 server 192.168.91.102:8080;31 server 192.168.91.103:8080;32 }48 location ~* \.jsp$ {49 proxy_pass http://tomcat;50 }51 52 location ~* \.html$ {53 root /usr/share/nginx/html;54 }
nginx -t
nginx -s reload
7.配置7-5Nginx服务器
vim /etc/nginx/nginx.conf30 upstream tomcat {31 server 192.168.91.102:8080;32 server 192.168.91.103:8080;33 }49 location ~* \.jsp$ {50 proxy_pass http://tomcat;51 }52 53 location ~* \.html$ {54 root /usr/share/nginx/html;55 }
nginx -t
nginx -s reload
8.7-3curl访问,验证第二层配置(7-3Nginx代理服务器 传内容给 7-4和7-5Nginx服务器;再给到7-1和7-2Tomcat服务器)
1.当访问静态资源(.html)时
[root@localhost ~]# curl 192.168.91.103/index.html
7-5
[root@localhost ~]# curl 192.168.91.103/index.html
7-4
[root@localhost ~]# curl 192.168.91.103/index.html
7-5
[root@localhost ~]#
2.当访问动态资源(.jsp)时
[root@localhost ~]# curl 192.168.91.103/test.jsp
7-1
[root@localhost ~]# curl 192.168.91.103/test.jsp
7-1
[root@localhost ~]# curl 192.168.91.103/test.jsp
7-2
[root@localhost ~]# curl 192.168.91.103/test.jsp
7-2
[root@localhost ~]#