码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx+Tomcat实现负载均衡及动静分离

时间:2018-08-22 21:53:36      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:using   share   str   protoc   ups   toc   art   系统   命名   

  • 内部模拟两台服务器taoba1和taobao2
  • 当访问 www.taobao.com 时候会依据负载均衡策略来进行访问
    技术分享图片

  • 拷贝两份tomcat文件,分别命名为taobao1、taobao2

    [root@fudanwuxi003 conf.d]# cd /root/software/
    [root@fudanwuxi003 software]# ll
    总用量 190720
    -rw-r--r--. 1 root root     60564 8月  21 23:36 1.jpg
    drwxr-xr-x. 9 root root       160 8月  20 14:56 apache-tomcat-8.5.32
    -rw-r--r--. 1 root root   9584807 8月  20 13:40 apache-tomcat-8.5.32.tar.gz
    -rw-r--r--. 1 root root 185646832 8月  20 13:34 jdk-8u181-linux-x64.tar.gz
    [root@fudanwuxi003 software]# cp -r apache-tomcat-8.5.32 taobao1
    [root@fudanwuxi003 software]# cp -r apache-tomcat-8.5.32 taobao2
    [root@fudanwuxi003 software]# pwd
    /root/software
  • 修改taobao1和taobao2的tomcat端口号
  • [root@fudanwuxi003 software]# vim taobao1/conf/server.xml 
     22 <Server port="8006" shutdown="SHUTDOWN">  #将默认的8005修改为8006
    
     68     -->
     69     <Connector port="8081" protocol="HTTP/1.1"  #将默认的8080修改为8081
     70                connectionTimeout="20000"
     71                redirectPort="8443" />
     72     <!-- A "Connector" using the shared thread pool-->
     73     <!--
    
    116     <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />  #将默认的8009修改为8010
    [root@fudanwuxi003 software]# vim taobao2/conf/server.xml
     22 <Server port="8007" shutdown="SHUTDOWN">  #将默认的8005修改为8007
    
     68     -->
     69     <Connector port="8082" protocol="HTTP/1.1"  #将默认的8080修改为8082
     70                connectionTimeout="20000"
     71                redirectPort="8443" />
     72     <!-- A "Connector" using the shared thread pool-->
     73     <!--
    
     116     <Connector port="8011" protocol="AJP/1.3" redirectPort="8443" />  #将默认的8009修改为8011
    • 分别修改taobao1和taobao2的默认页面,进行区分
    [root@fudanwuxi003 software]# pwd
    /root/software
    [root@fudanwuxi003 software]# vim taobao1/webapps/ROOT/index.jsp
    <h2>If you‘re seeing this, you‘ve successfully access to taobao1!</h2>
    
    [root@fudanwuxi003 software]# vim taobao2/webapps/ROOT/index.jsp
    <h2>If you‘re seeing this, you‘ve successfully access to taobao2!</h2>
    • 分别启动taobao1和taobao2的tomcat
    [root@fudanwuxi003 software]# ./taobao1/bin/startup.sh 
    [root@fudanwuxi003 software]# ./taobao2/bin/startup.sh 
    • 验证结果

    打开192.168.10.191:8081时候返回的是taobao1的页面
    技术分享图片

    打开192.168.10.191:8082时候返回的是taobao2的页面
    技术分享图片

    • 创建虚拟主机文件并配置负载均衡
    [root@fudanwuxi003 software]# cd /etc/nginx/conf.d/
    [root@fudanwuxi003 conf.d]# cp proxy.conf taobao.conf
    [root@fudanwuxi003 conf.d]# vim taobao.conf 
    #后台服务器列表
    upstream taobaohost{
             server 192.168.10.191:8081;
             server 192.168.10.191:8082;
    }
    
    server {
           listen   80;
           server_name www.taobao.com;
    
           location / {
               proxy_pass http://taobaohost;  #指定代理的后台服务器
        }
    
    }
    ~                                                
    • 在windows的hosts文件中添加域名解析
    192.168.10.191      www.taobao.com
    • 验证

    打开 www.taobao.com,会随机打开taobao1或taobao2的页面
    技术分享图片
    技术分享图片

    • 负载均衡策略(默认为轮询,修改为根据权重,服务器的性能不均的情况)
    [root@fudanwuxi003 conf.d]# vim taobao.conf
    upstream taobaohost{
             server 192.168.10.191:8081 weight=8;  weight代表权重,数值越大,被访问的概率越大
             server 192.168.10.191:8082 weight=2;
    }
    systemctl restart nginx
    • 此时打开 www.taobao.com 的时候,taobao1的页面出现的次数明显多于taobao2页面出现的次数

    • 由于Tomcat处理静态资源效率不高,会导致Web应用相应慢,占用系统资源。将静态资源交给Nginx处理,动态资源仍由Tomcat处理,实现动静分离。

    技术分享图片

    • 编辑taobao.conf,将静态资源交给Nginx处理
    [root@fudanwuxi003 conf.d]# vim taobao.conf 
    upstream taobaohost{
             server 192.168.10.191:8081 weight=8;
             server 192.168.10.191:8082 weight=2;
    }
    
    server {
           listen   80;
           server_name www.taobao.com;
    
    #处理动态资源
           location / {
               proxy_pass http://taobaohost;
        }
    #处理静态资源
           location ~ .*\.(js|css|ico|png|jpg|eot|svg|ttf|woff) {
              root /servers/taobao/static;
        }
    
    }
    ~                                                     
    • 由于还没有创建静态资源的目录,所以此时打开 www.taobao.com 如下图

    技术分享图片

    技术分享图片

    • 创建存放静态资源的目录,并将静态资源放在该目录中
    
    [root@fudanwuxi003 conf.d]# mkdir -p /servers/taobao/static
    [root@fudanwuxi003 conf.d]# cd /servers/taobao/static/
    [root@fudanwuxi003 static]# cp /root/software/taobao1/webapps/ROOT/tomcat.png ./
    [root@fudanwuxi003 static]# cp /root/software/taobao1/webapps/ROOT/tomcat.css ./
    [root@fudanwuxi003 static]# ll
    总用量 16
    -rw-r-----. 1 root root 5581 8月  22 18:39 tomcat.css
    -rw-r-----. 1 root root 5103 8月  22 18:39 tomcat.png
    [root@fudanwuxi003 static]# pwd
    /servers/taobao/static
    
    [root@fudanwuxi003 static]# chmod 755 *
    [root@fudanwuxi003 static]# ll
    总用量 16
    -rwxr-xr-x. 1 root root 5581 8月  22 18:39 tomcat.css
    -rwxr-xr-x. 1 root root 5103 8月  22 18:39 tomcat.png
    • 验证

    再次打开 www.taobao.com 的时候就正常了
    技术分享图片

    Nginx+Tomcat实现负载均衡及动静分离

    标签:using   share   str   protoc   ups   toc   art   系统   命名   

    原文地址:http://blog.51cto.com/jschinamobile/2163068

    (0)
    (0)
       
    举报
    评论 一句话评论(0
    登录后才能评论!
    © 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
    迷上了代码!