WEB服务器负载均衡之Nginx

    负载均衡 (Load Balancing) 负载均衡建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。    

 

    最近在自学负载均衡技术,网络上有不少文章,但写的都不是很明了,现把测试的笔记日记发表出来,以供大家学习使用;本文只介绍入门级的负载均衡,进阶还在学习中……

 

一、基础信息

操作系统:CentOS6.0

服务器:三台服务器(Load Balancing、WEB1、WEB2)

负载均衡技术:Nginx

web服务技术:apache

IP地址:Nginx 192.168.1.113、Web1 192.168.1.77、Web2 192.168.1.78

 

二、Web服务器的安装与配置

请参考本博客的apache服务器搭建文章:http://523514.blog.51cto.com/513514/1350015

 

三、负载均衡服务器(Nginx)的安装与配置

3.1 安装基础支持套件

yum -y install gcc openssl openssl-devel pcre pcre-devel

 

3.2 安装nginx

cd

wget http://nginx.org/download/nginx-1.5.8.tar.gz

tar xzvf nginx-1.5.8.tar.gz

cd nginx-1.5.8

./cofnigure --prefix=/usr/local/nginx

make

make install

 

3.3 配置nginx

编辑/usr/local/nginx/conf/nginx.conf

cd /usr/local/nginx/conf/

mv nginx.conf nginx.conf.bak

vim nginx.conf

新增如下内容

user  apache;
worker_processes  10;
pid        logs/nginx.pid;
events {        use epoll;
    worker_connections  2048;
    }

http {    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
        '$status $body_bytes_sent "$http_referer" '
        '"$http_user_agent" "$http_x_forwarded_for"';
        
access_log  logs/access.log  main;
        sendfile        on;
        tcp_nopush     on;
        
upstream http_pool {
     server 192.168.1.77:80;
     server 192.168.1.78:80;
     }    

     keepalive_timeout  65;

server {
         listen       80;
         server_name  
         access_log  logs/63638790.access.log  main; 
     location / { 
         proxy_pass    ;        
         proxy_set_header   Host             $host;   
         proxy_set_header   X-Real-IP        $remote_addr;       
         proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;  
      }        
        error_page   500 502 503 504  /50x.html;        
        location = /50x.html {            
        root   html;        
        }    
  }
 }

 

 

四、启动Nginx

4.1 验证nginx配置是否正常

# /usr/local/nginx/sbin/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

 

4.2 启动Nginx

# /usr/local/nginx/sbin/nginx

 

4.3 停止Nginx

# killall -9 nginx

 

4.4 DNS域名解析

有域名的同学可以上DNS服务商上进行解析设置;我是更改本地电脑hosts文件来进行测试的,编辑C:\Windows\System32\drivers\etc\hosts ,在最后一行新增如下内容:

 

192.168.1.113    www.63638790.cn

 

4.5 测试访问

打开浏览器 输入 http://www.63638790.cn  即能显示出web服务器的网站内容,停掉其中一台web服务器也不影响用户的正常使用;

 

结束词:Nginx实现负载均衡就是这样简单,给一万个赞,进阶配置学习中……

 

 

你可能感兴趣的:(nginx,负载均衡,服务器,操作系统,web服务器)