nginx负载均衡和动静分离配置

环境

主机IP
nginx192.168.159.100
httpd1192.168.159.101
httpd2192.168.159.102
tomcat192.168.159.200

nginx反向代理和负载均衡

关闭防火墙和selinux

部署nginx

RS1安装httpd

[root@RS1 ~]# yum -y install httpd
[root@RS1 ~]# echo "This is RS1" > /var/www/html/index.html
[root@RS1 ~]# systemctl restart httpd
[root@RS1 ~]# systemctl enable httpd

RS2安装httpd

[root@RS2 ~]# yum -y install httpd
[root@RS2 ~]# echo "This is RS2" >/var/www/html/index.html
[root@RS2 ~]# systemctl restart httpd
[root@RS2 ~]# systemctl enable httpd

nginx上修改配置文件

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
    upstream stat {
        server 192.168.159.101;
        server 192.168.159.102 weight=2;
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;


        location / {
                proxy_pass http://stat;
                }

[root@nginx ~]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@nginx ~]# nginx -s reload

访问测试

在这里插入图片描述
在这里插入图片描述

访问RS2两次后才会轮询到RS1


nginx动静分离

部署tomcat

[root@tomcat ]# yum -y install java-11-openjdk

[root@tomcat ~]# tar -zxf apache-tomcat-10.0.23.tar.gz -C /usr/local/
[root@tomcat ~]# cd /usr/local/
[root@tomcat local]# ln -s apache-tomcat-10.0.23/ tomcat
[root@tomcat local]# cd tomcat/
[root@tomcat tomcat]# bin/startup.sh
Using CATALINA_BASE:   /usr/local/tomcat
Using CATALINA_HOME:   /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Using CATALINA_OPTS:
Tomcat started.

[root@tomcat tomcat]# cd webapps/
[root@tomcat webapps]# mkdir test
[root@tomcat webapps]# echo "teeeeeest" >test/index.html
[root@tomcat webapps]# cd ..
[root@tomcat tomcat]# bin/catalina.sh stop
[root@tomcat tomcat]# bin/catalina.sh start

在这里插入图片描述

配置nginx

[root@nginx ~]# vim /usr/local/nginx/conf/nginx.conf
    upstream stat {
        server 192.168.159.101;
        server 192.168.159.102 weight=2;
    }
    upstream tomcat {
        server 192.168.159.200:8080;
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;


        location / {
                proxy_pass http://stat;
                }

        location /test {
                proxy_pass http://tomcat;
                }

[root@nginx ~]# nginx -s reload

测试访问

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值