Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,并在一个BSD-like 协议下发行。其特点是占有内存少,并发能力强,事实上nginx的并发能力确实在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。
nginx不仅是一款优秀的web服务器,它同样具备反向代理,负载均衡等强大功能,在业界有广泛的应用,下面我们使用nginx+Tomcat实现负载均衡功能。
环境
os redhat7.1
ip 192.168.56.135(nginx+tomcat) 192.168.56.137(tomcat)
nginx-1.10.0
tomcat-8.5.46
jdk-1.8.0
安装nginx
nginx安装请参考https://blog.csdn.net/lucky_ykcul/article/details/96871851
安装Tomcat
Tomcat安装请参考
https://blog.csdn.net/lucky_ykcul/article/details/99298731
启动nginx
由于之前Apache没有停掉导致80端口被占用,nginx启动失败
[root@server1 sbin]# ./nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()
停掉httpd后,成功启动
[root@server1 sbin]# ./nginx
[root@server1 sbin]# netstat -antlpe |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 0 234895 18189/nginx: master
启动Tomcat
[root@server1 bin]# ./startup.sh
/usr/local/tomcat8/bin/catalina.sh: line 254: -Xmx1024m: command not found
Using CATALINA_BASE: /usr/local/tomcat8
Using CATALINA_HOME: /usr/local/tomcat8
Using CATALINA_TMPDIR: /usr/local/tomcat8/temp
Using JRE_HOME: /usr
Using CLASSPATH: /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar
Tomcat started.
[root@server1 bin]# ps -ef |grep java
root 5454 1 0 21:02 pts/0 00:00:13 /usr/bin/java -Djava.util.logging.config.file=/usr/local/tomcat8/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat8/bin/bootstrap.jar:/usr/local/tomcat8/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat8 -Dcatalina.home=/usr/local/tomcat8 -Djava.io.tmpdir=/usr/local/tomcat8/temp org.apache.catalina.startup.Bootstrap start
配置nginx文件,添加负载均衡
[root@server1 conf]# pwd
/usr/local/nginx/conf
[root@server1 conf]# vim nginx.conf
http {
upstream redhat {
server 192.168.56.135:8080;
server 192.168.56.137:8080;
}
include mime.types;
default_type application/octet-stream;
重启nginx使配置生效
[root@server1 nginx]# sbin/nginx -s reload
在Tomcat两台主机/etc/hosts中添加域名解析,否则无法访问域名
[root@server1 nginx]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.56.135 www.123.com
192.168.56.137 www.123.com
访问www.123.com验证负载均衡是否生效
这里没有更改Tomcat页面所以无法区分是否nginx将请求分发到两个Tomcat
更改Tomcat页面
tomcat的默认页面在ROOT目录下
[root@server1 ROOT]# pwd
/usr/local/tomcat8/webapps/ROOT
[root@server1 ROOT]# mkdir tomcat
[root@server1 ROOT]# mv * tomcat
mv: cannot move ‘tomcat’ to a subdirectory of itself, ‘tomcat/tomcat’
[root@server1 ROOT]# ls
tomcat
分别在两个Tomcat新建一个index.html文件并写入内容
[root@server1 ROOT]# echo hello,this is tomcat1 > index.html
[root@server2 ROOT]# echo hello, this is tomcat2 > index.html
关闭firewalld否则访问另外一台不通
[root@server2 bin]# systemctl stop firewalld.service
访问www.123.com验证负载均衡是否生效
刷新页面会分别跳转到Tomcat和Tomcat2上,实现了负载均衡的效果
负载均衡的策略
目前nginx支持6种模式的负载均衡
- rr轮询(默认)按照请求顺序轮流调度realserver
- weight (权重)根据weight值进行调度,weight值越大调用次数越多
- ip_hash 访问请求按照IP地址的hash值进行分配,ip的hash值只要相同就会被分配到同一台服务器上,适合做回话保持
- fair 根据realserver相应时间分配,响应时间短的优先分配
- least_conn 根据最少连接分配,那个realserver连接少就优先分配
- url_hash 按照访问的url来分配请求,让相同的url定向到同一个服务器