nginx+Tomcat 负载均衡
Posted 睡着的冰淇淋
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx+Tomcat 负载均衡相关的知识,希望对你有一定的参考价值。
实验环境
实验目的
当请求访问到我这台 nginx 服务器时我本身不做为 web 服务器解析,我把你的请求转换到后台的服务器做解析,让你们做解析。
实验准备
实现三台设备的局域网互通
三台虚拟机之间使用 VMnet3 对接,只需要配置IP地址,不需要配置网关
nginx
[root@bobi ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
[root@bobi ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
BOOTPROTO=static
DEVICE=ens33
ONBOOT=yse
IPADDR=172.16.1.1
NETMASK=255.255.255.0
[root@bobi ~]#
[root@bobi ~]# ifdown ens33
^[[ADevice ens33 successfully disconnected.
[root@bobi ~]# ifdown ens33
[root@bobi ~]# ifup ens33
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/7)
[root@bobi ~]#
[root@bobi ~]# ipaddr
bash: ipaddr: command not found...
[root@bobi ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:f4:e9:60 brd ff:ff:ff:ff:ff:ff
inet 172.16.1.1/24 brd 172.16.1.255 scope global noprefixroute ens33
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fef4:e960/64 scope link
valid_lft forever preferred_lft forever
3: virbr0: <BROADCAST,MULTICAST> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
link/ether 52:54:00:bd:8a:7a brd ff:ff:ff:ff:ff:ff
4: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast master virbr0 state DOWN group default qlen 1000
link/ether 52:54:00:bd:8a:7a brd ff:ff:ff:ff:ff:ff
5: ens37: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:f4:e9:6a brd ff:ff:ff:ff:ff:ff
inet 192.168.192.144/24 brd 192.168.192.255 scope global noprefixroute dynamic ens37
valid_lft 1268sec preferred_lft 1268sec
inet6 fe80::b056:e73b:2369:6dce/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@bobi ~]#
tomcat1
[root@bobi ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
[root@bobi ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=static
DEVICE=eth0
ONBOOT=yes
IPADDR=172.16.1.10
NETMASK=255.255.255.0
[root@bobi ~]#
[root@bobi ~]# ifdown eth0
Device eth0 successfully disconnected.
[root@bobi ~]# ifup eth0
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/7)
[root@bobi ~]#
[root@bobi ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:57:b3:2f brd ff:ff:ff:ff:ff:ff
inet 172.16.1.10/24 brd 172.16.1.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe57:b32f/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:57:b3:39 brd ff:ff:ff:ff:ff:ff
inet 192.168.192.146/24 brd 192.168.192.255 scope global noprefixroute dynamic eth1
valid_lft 1638sec preferred_lft 1638sec
inet6 fe80::f43e:7f0c:bfff:d907/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@bobi ~]#
tomcat2
[root@bobi ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
[root@bobi ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=static
DEVICE=eth0
ONBOOT=yes
IPADDR=172.16.1.20
NETMASK=255.255.255.0
[root@bobi ~]#
[root@bobi ~]# ifdown eth0
Device eth0 successfully disconnected.
[root@bobi ~]# ifup eth0
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)
[root@bobi ~]#
[root@bobi ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:02:69:75 brd ff:ff:ff:ff:ff:ff
inet 172.16.1.20/24 brd 172.16.1.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe02:6975/64 scope link
valid_lft forever preferred_lft forever
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:02:69:7f brd ff:ff:ff:ff:ff:ff
inet 192.168.192.145/24 brd 192.168.192.255 scope global noprefixroute dynamic eth1
valid_lft 1155sec preferred_lft 1155sec
inet6 fe80::f59b:e22f:f08a:8728/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@bobi ~]#
测试
nginx 和 tomcat
tomcat 之间
网络互通
开启 Nginx 服务
修改 tomcat1 和 tomcat2 的主页内容
<用于区分两个 tomcat 服务>
tomcat1
[root@bobi webapps]# ls
docs examples host-manager manager ROOT
[root@bobi webapps]# mv ROOT/ ROOTBAK
[root@bobi webapps]# ls
docs examples host-manager manager ROOTBAK
[root@bobi webapps]#
[root@bobi webapps]# mkdir ROOT
[root@bobi webapps]# cd ROOT
[root@bobi ROOT]# ls
[root@bobi ROOT]# vim index.jsp
[root@bobi ROOT]#
[root@bobi ROOT]# cat index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP TEST1 page</title>
</head>
<body>
<% out.println("Welcome to TEST1 web,http:/www.test1.com");%>
</body>
</html>
tomcat2
[root@bobi webapps]# ls
docs examples host-manager manager ROOT
[root@bobi webapps]# mv ROOT/ ROOTBAK
[root@bobi webapps]# ls
docs examples host-manager manager ROOTBAK
[root@bobi webapps]#
[root@bobi webapps]# mkdir ROOT
[root@bobi webapps]# cd ROOT
[root@bobi ROOT]# ls
[root@bobi ROOT]# vim index.jsp
[root@bobi ROOT]#
[root@bobi ROOT]# cat index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<title>JSP TEST2 page</title>
</head>
<body>
<% out.println("Welcome to TEST2 web,http:/www.test2.com");%>
</body>
</html>
配置 Nginx 服务端
安装 Nginx 反向代理服务
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module
[root@bobi src]# cd nginx-1.6.2/
[root@bobi nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module
checking for OS
+ Linux 3.10.0-1160.el7.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
...
checking for OpenSSL library ... not found
./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library
into the system, or build the OpenSSL library statically from the source
with nginx by using --with-openssl=<path> option.
缺少开发组件,安装即可
[root@bobi nginx-1.6.2]# yum install openssl openssl-devel
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
dvdrom | 3.6 kB 00:00:00
(1/2): dvdrom/group_gz | 153 kB 00:00:00
(2/2): dvdrom/primary_db | 3.3 MB 00:00:00
Package 1:openssl-1.0.2k-19.el7.x86_64 already installed and latest version
Resolving Dependencies
...
----------------------------------------------------------------------------------
Total 9.1 MB/s | 2.1 MB 00:00
Dependency Installed:
keyutils-libs-devel.x86_64 0:1.5.8-3.el7 krb5-devel.x86_64 0:1.15.1-50.el7
libcom_err-devel.x86_64 0:1.42.9-19.el7 libselinux-devel.x86_64 0:2.5-15.el7
libsepol-devel.x86_64 0:2.5-10.el7 libverto-devel.x86_64 0:0.2.5-4.el7
Complete!
注:
刚开始 yum install openssl openssl-devel 的时候出现下面的错误
Error downloading packages:
libsepol-devel-2.5-10.el7.x86_64: [Errno 256] No more mirrors to try.
libverto-devel-0.2.5-4.el7.x86_64: [Errno 256] No more mirrors to try.
libselinux-devel-2.5-15.el7.x86_64: [Errno 256] No more mirrors to try.
keyutils-libs-devel-1.5.8-3.el7.x86_64: [Errno 256] No more mirrors to try.
krb5-devel-1.15.1-50.el7.x86_64: [Errno 256] No more mirrors to try.
1:openssl-devel-1.0.2k-19.el7.x86_64: [Errno 256] No more mirrors to try.
libcom_err-devel-1.42.9-19.el7.x86_64: [Errno 256] No more mirrors to try.
百度一下,需要进行下面的操作
yum clean all
yum makecache
yum update
但是又出现错误
failure: repodata/repomd.xml from dvdrom: [Errno 256] No more mirrors to try.
file:/media/repodata/repomd.xml: [Errno 14] curl#37 - "Couldnt open file /media/repodata/repomd.xml"
最后发现是本地的镜像没有挂载
(虽然在前面的实验中挂载了,但是是临时挂载,重启后就失效了,开机的时候反而忘记了这回事,重新挂载一下就好了)
[root@bobi yum.repos.d]# cd /media/
[root@bobi media]# ls
[root@bobi media]#
[root@bobi media]# mount /dev/cdrom /media/
mount: /dev/sr0 is write-protected, mounting read-only
开发组件安装完成后,继续检查编译环境
[root@bobi nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module
checking for OS
+ Linux 3.10.0-1160.el7.x86_64 x86_64
checking for C compiler ... found
+ using GNU C compiler
+ gcc version: 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
...
nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
make -j 4
make install
验证
[root@bobi local]# cd /usr/local/nginx/
[root@bobi nginx]# ls
client_body_temp fastcgi_temp logs sbin uwsgi_temp
conf html proxy_temp scgi_temp
[root@bobi nginx]# cd sbin/
[root@bobi sbin]# ls
nginx nginx.old
注:
nginx.old 是之前安装的nginx服务
nginx 是现在安装的nginx反向代理服务
配置 Nginx 反向代理服务
修改配置文件
vim /usr/local/nginx/conf/nginx.conf
upstream tomcatserver
server 172.16.1.10:8080 weight=1;
server 172.16.1.20:8080 weight=1;
以上是关于nginx+Tomcat 负载均衡的主要内容,如果未能解决你的问题,请参考以下文章
nginx+2tomcat的负载均衡环境,负载均衡策略用权重形式