nginx的负载均衡实战

前言

  nginx是一个高性能的HTTP和反向代理的服务器。它有三个最基本的功能,一是当做web服务器、二是作为反向代理服务器、三是提供负载均衡(在反向代理基础上),由于它占有内存小,并发能力强,所以在同类型的服务器中脱颖而出,淘宝,腾讯,百度等国内巨头互联网公司也常用nginx。文将与大家分享一下nginx应用在tomcat上的负载均衡实例。

一、运行环境

  ubuntu服务器一台

  nginx-1.9.15

  apache-tomcat-9.0.0(两个,由于只创建了一个ubuntu服务器,所以通过更改端口来模拟负载均衡场景)

二、ubuntu下安装nginx

1、安装openssl

  openssl是一个强大的套接字层密码库,常用的密钥和证书封装管理功能及SSL协议,并提供丰富的应用程序测试或其他目的使用

  nginx不仅支持http协议,还支持https(即在ssl协议上传输http),所以需要在linux安装openssl库

命令:sudo apt-get install openssl

2、安装PCRE

  PCRE是一个perl库,包括perl兼容的正则表达式库。nginx的http模块使用pcre来解析正则表达式,所以需要在linux上安装pcre库

命令:sudo apt-get install libcre3 libpcre3-dev

3、安装zlib

  zlib库提供了很多压缩和解压缩的方式,nginx使用zlib对http包的内容进行gzip,所以需要在linux上安装zlib库

4、下载nginx-1.9.15.tar.gz

  我的百度云连接

  https://pan.baidu.com/s/1GeQSIF1QL5UiZt_kyCikPw

  之后用XFftp工具将文件上传到ubuntu上

  执行命令

  tar -zxvf nginx-1.9.15.tar.gz

5、进行configure

  cd到nginx的根目录执行

./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi

6、make编译

  命令:make

7、make install安装

  命令:make install

三、nginx的启动与关闭

  启动:进入nginx的sbin目录,执行./nginx

  关闭:./nginx -s stop

  刷新配置:./nginx -s reload

  启动成功之后,浏览器输入localhost出现

  

四、配置Tomcat的server.xml(修改端口)

  Tomcat91修改端口为8081

    <Server port="8006" shutdown="SHUTDOWN">    
    <Connector port="8081" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8444" />
    <Connector port="8010" protocol="AJP/1.3" redirectPort="8444" />

  Tomcat92修改端口为8082

    <Server port="8007" shutdown="SHUTDOWN">    
    <Connector port="8082" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8445" />
    <Connector port="8011" protocol="AJP/1.3" redirectPort="8445" />

  继续修改一下tomcat/webapps/ROOT的index.jsp,目的是为了区别两个tomcat的启动首页

五、配置nginx的nginx

upstream stonegeek.com{ #服务器集群名字
    server 127.0.0.1:8081 weight=1; #集群的服务器列表,最终请求都会转发到这里执行
    server 127.0.0.1:8082 weight=2; #weight是权重的意思,权重越大,分配的概率越大
    }
    server {
        listen       80; #默认监听80端口,可以改成其他端口
        server_name  localhost; #当前服务器的域名

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            #root   html;
            #index  index.html index.htm;
        proxy_pass http://stonegeek.com; #要与服务器集群名字相同,如果请求为localhost:80,则交给名称为stonegeek.com的nginx集群来处理
 proxy_redirect default; }

  至此,nginx的负载均衡配置完毕

六、演示负载均衡

  刷新nginx配置:进入nginx的sbin目录 执行./nginx -s reload

  启动两个tomcat:分别进入tomcat的bin目录 执行./startup.sh

  浏览器输入http://192.168.43.230/index.jsp(192.168.43.230是我安装的ubuntu虚拟机的IP地址)

  结果展示

  

  刷新一次

  

  

七、总结

  至此,利用nginx的反向代理来实现负载均衡的实现就已经完成了,这是基于tomcat服务器的负载均衡,因为仅仅创建了一台服务器所以通过修改端口来模拟两台服务器的效果,还可以配置其他的,比如说apache web服务器。所以nginx作为一款自由的、开源的、高性能的HTTP服务器和反向代理服务器是非常受广大开发人员青睐的,简单、高效!

  此篇文章只是介绍nginx利用反向代理实现负载均衡的简单例子,后续文章我会继续更新更多关于nginx的配置介绍,敬请期待!

猜你喜欢

转载自www.cnblogs.com/sxkgeek/p/8991976.html