天天看點

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服務;