天天看点

Nginx+Apache均衡负载

模拟均衡负载环境: 一台ubuntu15.04做Nginx分发服务器(192.168.0.101),两台window2008做Apache web服务器(192.168.0.15、192.168.0.26)

1、部署Nginx分发服务器(192.168.0.101)

1)、在ubuntu15.04服务器上安装Nginx服务

下载完成后解压缩到目录,执行 ./configure,如果无法完成,按照错误提示安装pcre、zlib

numbgui@numbgui:/home/numbgui/Public/nginx-.$ sudo apt-get install libpcre3 libpcre3-dev

numbgui@numbgui:/home/numbgui/Public/nginx-.$ sudo apt-get install zlib1g-dev
           

执行完成后重新执行 ./configure 来检测你的安装平台的目标特征

numbgui@numbgui:/home/numbgui/Public/nginx-.$ sudo ./confugure
           

检测通过没有问题之后执行,make操作

numbgui@numbgui:/home/numbgui/Public/nginx-.$ sudo make
           

ps: make是用来编译的,它从Makefile中读取指令,然后编译

编译完成会后执行make install 安装

numbgui@numbgui:/home/numbgui/Public/nginx-.$ sudo make install
           

到这里Nginx服务在ubuntu15.04中已经安装完成

2)、启动Nginx服务

启动Nginx服务

numbgui@numbgui:/home/numbgui$/usr/local/nginx/sbin/nginx -s reload
           

显示如下界面表示启动成功

Nginx+Apache均衡负载

如果启动Nginx服务提示是 :丢失nginx.pid

执行以下语句可以解决:

numbgui@numbgui:/home/numbgui$ sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
           

3)、配置Nginx均很负载(简单模拟)

编辑/usr/local/nginx/conf/nginx.conf 配置文件,如下

#user  nobody;
worker_processes  ;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  ;
}


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;

    #keepalive_timeout  0;
    keepalive_timeout  ;

    #gzip  on;

    #这里配置均很负载的服务地址,我这里配置的为两台win2008下的apache web服务,均在80端口下,地址后面的参数:weight--轮询几率
    upstream apache{
        server   .: weight= max_fails= fail_timeout=s;
        server   .: weight= max_fails= fail_timeout=s;
    }

    server {
        listen       ;
        #这里server_name 配置上面 upstream配置的名字(这里为apache)
        server_name  apache;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #location / {
        #    root   html;
        #    index  index.html index.htm;
        #}
        location / { 
             #http://后面与的参数与server_name相同(这里为apache)
             proxy_pass         http://apache; 
             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  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page        /x.html;
        location = /x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
           

配置完成之后重启 nginx 服务

numbgui@numbgui:/usr/local/nginx/conf$ sudo ./nginx -s reload 
           

2、部署window2008 Apache web服务(192.168.0.15、192.168.0.26)

关于apache服务的部署,这里不再赘述,如有疑问请自行查询相关资料

即可完成简单均很负载配置;

效果如下:

Nginx+Apache均衡负载
Nginx+Apache均衡负载

ps: 访问192.168.0.101 Nginx服务器,通过轮询的方式计算并转发给26或者15服务器的apache web服务;