码迷,mamicode.com
首页 > 其他好文 > 详细

使用nginx实现负载均衡和动静分离

时间:2018-04-19 16:10:06      阅读:134      评论:0      收藏:0      [点我收藏+]

标签:nginx实现负载均衡和动态分离

使用nginx实现负载均衡和动静分离

  • 在192.168.221.10这台机器上源码编译安装nginx
yum -y install gcc gcc-c++ autoconf automake zib zib-devel openssl openssl-devel pcre pcre-devel

技术分享图片

cd /usr/local/src/
wget http://nginx.org/download/nginx-1.8.0.tar.gz
tar zxvf nginx-1.8.0.tar.gz
cd nginx-1.8.0/
./configure --prefix=/usr/local/nginx-1.8.0 --with-http_dav_module --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module

技术分享图片

make -j 4 && make install
useradd -u 8000 -s /sbin/nologin nginx
/usr/local/nginx-1.8.0/sbin/nginx
echo "/usr/local/nginx-1.8.0/sbin/nginx &" >> /etc/rc.local

nginx的操作

cd /usr/local/nginx-1.8.0/sbin/
./nginx -t
./nginx -s reload|stop
  • 配置nginx成为分发器,实现动静分离
cd /usr/local/nginx-1.8.0/conf/
cp nginx.conf nginx.conf.back
vim nginx.conf
user  nginx nginx;
// 在 location {...}内添加以下内容
if ($request_uri ~* \.html$){
                proxy_pass http://htmlservers;
            }
            if ($request_uri ~* \.php$){
                proxy_pass http://phpservers;
            }
            proxy_pass http://picservers;
  • 定义负载均衡设备的ip
    在配置文件ngin.conf的最后一行}前,添加如下内容
upstream htmlservers {
        server 192.168.221.20:80;
        server 192.168.221.30:80;
        }
    upstream phpservers {
        server 192.168.221.20:80;
        server 192.168.221.30:80;
        }
    upstream picservers {
        server 192.168.221.20:80;
        server 192.168.221.30:80;
        }

检测语法,重新加载nginx.conf

../sbin/nginx -t
../sbin/nginx -s reload
  • 配置后端服务器(192.168.221.20)
yum install httpd php -y

在网站根目录下创建以下文件
技术分享图片
启动 httpd

service httpd start
  • 配置后端服务器(192.168.221.30)

安装httpd php,在网站根目录下创建 index.html,test.php,1.png,启动httpd服务

  • 测试转发静态页

技术分享图片

技术分享图片

  • 测试转发动态页

技术分享图片

技术分享图片

  • 测试转发图片

技术分享图片

技术分享图片

  • 测试自动剔除坏的节点

停止 192.168.221.20机器上的httpd服务,再次访问

这样就会一直访问到192.168.221.30上的站点

使用nginx实现负载均衡和动静分离

标签:nginx实现负载均衡和动态分离

原文地址:http://blog.51cto.com/13480443/2105404

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!