• Nginx代理和负载均衡实验


    一、构建两个tomcat容器并启动

    [root@localhost bin]# ps -ef|grep tomcat
    root       1525      1 12 10:19 pts/0    00:00:03 /usr/bin/java -Djava.util.logging.config.file=/usr/local/tomcat-1/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/tomcat-1/bin/bootstrap.jar:/usr/local/tomcat-1/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat-1 -Dcatalina.home=/usr/local/tomcat-1 -Djava.io.tmpdir=/usr/local/tomcat-1/temp org.apache.catalina.startup.Bootstrap start
    root       1586      1 39 10:19 pts/0    00:00:02 /usr/bin/java -Djava.util.logging.config.file=/usr/local/tomcat-2/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/tomcat-2/bin/bootstrap.jar:/usr/local/tomcat-2/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat-2 -Dcatalina.home=/usr/local/tomcat-2 -Djava.io.tmpdir=/usr/local/tomcat-2/temp org.apache.catalina.startup.Bootstrap start

    二、修改两个容器的web首页,并测试两个容器是否正常

    三、启动Nginx并查看进程

    [root@www ~]# /usr/local/nginx/sbin/./nginx
    [root@www ~]# ps -ef|grep nginx
    root       5354      1  0 13:00 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/./nginx
    nobody     5355   5354  0 13:00 ?        00:00:00 nginx: worker process

    四、开始实验:

    Nginx代理服务器IP:192.168.249.132

    Tomcat-1 IP:192.168.249.133:8080

    Tomcat-2 IP:192.168.249.133:8081

    1.配置测试反向代理

    1)修改Nginx的配置文件

    [root@www conf]# cat nginx.conf|grep -E -v "#|^$"
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        server {
            listen       80;
            server_name  localhost;
    	location / {
    	    proxy_pass http://192.168.249.133:8080;  #将请求转向后端定义的Tomcat-1上.
    	}
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }
    

    2)重新载入nginx.conf

    [root@www conf]# cat nginx.conf|grep -E -v "#|^$"
    

    3)访问Nginx代理服务器,查看是否跳转

    2.负载均衡算法测试——轮询算法

    1)在nginx.conf的server块里写入upstream配置块,并修改proxy_pass参数

    [root@www conf]# cat nginx.conf|grep -E -v "#|^$"
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        upstream nginxtest {
            server 192.168.249.133:8080;
            server 192.168.249.133:8081;
        }
        server {
            listen       80;
            server_name  localhost;
    	location / {
    	    proxy_pass http://nginxtest;
    	}
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }
    

    2)重新载入nginx.conf

    [root@www conf]# ../sbin/./nginx -s reload
    

    3)多次访问Nginx代理服务器

    一直刷新页面会发现随机出现两个网页,这就是nginx默认的负载均衡算法:轮询算法

    3.负载均衡权重测试——权重算法

    1)在nginx.conf的server块里修改upstream配置块,在其中server参数后添加weight参数

    [root@www conf]# cat nginx.conf|grep -E -v "#|^$"
    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        upstream nginxtest {
            server 192.168.249.133:8080 weight=3;
            server 192.168.249.133:8081 weight=2;
        }
        server {
            listen       80;
            server_name  localhost;
            location / {
                proxy_pass http://nginxtest;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
                root   html;
            }
        }
    }
    

    2)重新载入nginx.conf

    [root@www conf]# ../sbin/./nginx -s reload

    3)访问nginx代理服务器

    发现刷新三次是tomcat-1后,后两次刷新是tomcat-2.

    剩下的两种算法持续更新哦……

  • 相关阅读:
    [导入]Interesting Finds: 2007.12.17
    [导入]Interesting Finds: 2007.12.15
    [导入]Interesting Finds: 2007.12.13
    Interesting Finds: 2007.12.22
    吾生也有涯,而知也无涯。
    Hello world
    学习了clipboard复制剪切插件的使用
    在博客园学习成长
    命名空间概述
    Linux vi/vim
  • 原文地址:https://www.cnblogs.com/quail2333/p/11188269.html
Copyright © 2020-2023  润新知