jokerchu 发表于 2018-11-13 11:43:46

使用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>  


[*]配置nginx成为分发器,实现动静分离
  

cd /usr/local/nginx-1.8.0/conf/  
cp nginx.conf nginx.conf.back
  
vim nginx.conf
  
usernginx 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>  


[*]配置后端服务器(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上的站点


[*]性能测试
  

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


  

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

  设置进程最多同时允许打开的默认文件个数为2000
  

ulimit -n 2000  

  这样就可以解决报错问题


页: [1]
查看完整版本: 使用nginx实现负载均衡和动静分离