nginx反向代理和负载均衡搭建与测试

  1. 前期准备

    1. 安装nginx(我前面的博客有说明)
    2. 安装apache-tomcat-7.0.47.tar.gz
  2. 过程步骤

    1. 解压apache-tomcat-7.0.47.tar.gz,tar -zxvf apache-tomcat-7.0.47.tar.gz,
    2. 重命名,mv apache-tomcat-7.0.47 apache-tomcat-sina
    3. 复制apache-tomcat-sina,命令:cp -r apache-tomcat-sina apache-tomcat-sina2
    4. 复制apache-tomcat-sina,命令:cp -r apache-tomcat-sina apache-tomcat-sohu
    5. 修改apache-tomcat-sina/webapps/ROOT/index.jsp文件,将后面加上-sina,如下图:
    6. 同理,也修改apache-tomcat-sina2/webapps/ROOT/index.jsp文件,后面加上-sina2
    7. 也修改apache-tomcat-sohu/webapps/ROOT/index.jsp文件,后面加上-sohu
    8. 修改apache-tomcat-sohu/conf/server.xml配置文件,修改相对应的端口,将8005改为8006,将8080改为8081,将8009改为8010
    9. 修改apache-tomcat-sina/conf/server.xml配置文件,修改相对应的端口,将8005改为8007,将8080改为8082,将8009改为8011
    10. 启动tomcat,通过浏览器访问http://ip:8080,http://ip:8081,http://ip:8082
    11. 修改nginx.conf配置文件,如下:
      upstream sina{
      		server 192.168.1.107:8080;
      		server 192.168.1.107:8082 weight=2;
      	}
      	
      	server {
              listen       80;
              server_name  www.sina.com.cn;
      
              location / {
                  proxy_pass   http://sina;
                  index  index.html index.htm;
              }
      
              error_page   500 502 503 504  /50x.html;
              location = /50x.html {
                  root   html;
              }
          }
      	
      	upstream sohu{
      		server 192.168.1.107:8081;
      	}
      	
      	server {
              listen       80;
              server_name  www.sohu.com;
      
              location / {
                  proxy_pass   http://sohu;
                  index  index.html index.htm;
              }
      
              error_page   500 502 503 504  /50x.html;
              location = /50x.html {
                  root   html;
              }
          }

       

    12. 修改windows系统中的host文件,该文件在C:\Windows\System32\drivers\etc该路径下,如下:

      192.168.1.107 www.sina.com.cn
      192.168.1.107 www.sohu.com

      你在浏览器上访问www.sina.com.cn,请求就回到192.168.1.107服务器地址上了,nginx就会根据域名重新找服务器

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章