nginx负载均衡配置实现动静分离

反向代理与负载均衡

环境说明
主机名IP服务
agent192.168.47.115nginx
nginx192.168.47.120nginx
httpd192.168.129.121httpd

注:nginx服务都是源码安装 、httpd为yum安装
nginx源码安装请参考:nginx

准备工作

每台主机开启服务,并关闭防火墙与selinux

systemctl distable --now firewalld
sed -ri 's/^(SELINUX=).*/\1disabled/g' /etc/sysconfig/selinux
setenforce 0
agent端配置
root@agent ~]# vim /usr/local/nginx/conf/nginx.conf
 33     #gzip  on;
 34 
 35     upstream webservers {					#配置负载均衡
 36         server 192.168.47.120;
 37         server 192.168.47.121;
 38     }
 39 
 40     server {
 41         listen       80;
 42         server_name  localhost;
 43 
 44         #charset koi8-r;
 45 
 46         #access_log  logs/host.access.log  main;
 47 
 48         location / {						#配置反向代理
 49             proxy_pass http://webservers;
 50         }
[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主机IP地址访问,并刷新测试

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

动静分离

环境说明
主机名IP服务
agent192.168.47.115nginx
nginx192.168.47.120lnmp
httpd192.168.129.121httpd

lnmp架构部署详细步骤请参考:lnmp

修改agent主机配置文件

    #gzip  on;
    
    upstream dynamic {        
        server 192.168.47.120;			#lnmp主机的ip
    }
    upstream static {
        server 192.168.47.121;			#httpd主机的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

使用agent主机IP地址访问测试

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值