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

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

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

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

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

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

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的操做html

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.confnginx

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

在网站根目录下建立如下文件
使用nginx实现负载均衡和动静分离
启动 httpdc++

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

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

  • 测试转发静态页

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

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

  • 测试转发动态页

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

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

  • 测试转发图片

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

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

  • 测试自动剔除坏的节点

中止 192.168.221.20机器上的httpd服务,再次访问后端

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

  • 性能测试
yum install httpd-tools -y   //安装ab命令
ab -n 1000 -c 1000 http://192.168.221.10/index.html

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

ulimit -n   //系统默认一个进程最多同时容许打开1024个文件
1024

设置进程最多同时容许打开的默认文件个数为2000负载均衡

ulimit -n 2000

这样就能够解决报错问题ide

相关文章
相关标签/搜索