Nginx负载均衡实现动态分离

Nginx负载均衡实现动态分离

环境

主机名IP服务
agent192.168.164.128Nginx
Nginx192.168.164.133Nginx
httpd192.168.164.137httpd

反向代理与负载均衡

每台主机都关闭防火墙和selinux

[root@localhost]# systemctl stop firewalld.service   //临时关闭防火墙,重启后依旧启动
[root@localhost]# setenforce 0
[root@localhost]# vim /etc/selinux/config
...
SELINUX=disabled
...

agent配置

[root@localhost]#  vim /usr/local/nginx/conf/nginx.conf
......  
     upstream webservers {					//配置负载均衡
          server 192.168.164.133;
         server 192.168.164.137;
    }

     server {
         listen       80;
         server_name  localhost;
          #charset koi8-r;
 
          #access_log  logs/host.access.log  main;
  
          location / {						// 配置反向代理
              proxy_pass http://webservers;
          }
  ......
[root@agent ~]# 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@agent ~]# nginx -s reload    //重新加载配置文件

使用agent的站点

在这里插入图片描述
在这里插入图片描述配置动静分离

修改agent主机的Nginx配置文件

[root@localhost]#  vim /usr/local/nginx/conf/nginx.conf
......
 #gzip  on;
upstream dynamic {        
        server 192.168.47.120;			//动态主机的ip
    }
    upstream static {
        server 192.168.47.121;			//静态主机的ip
    }	


    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://static;			//访问静态资源会自动跳转至静态主机
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80	
        #
        location ~ \.php$ {
            proxy_pass   http://dynamic;		//访问动态资源会自动跳转到动态主机
        }
......
[root@agent ~]# 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@agent ~]# nginx -s reload    //重新加载配置文件

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值