nginx反向代理負載均衡功能

客戶端====代理服務器===web服務器

反向代理功能架構
3臺web服務器,組建出web服務器集羣
web01  10.0.0.7   172.16.1.7
web02  10.0.0.8   172.16.1.8
web03  10.0.0.9   172.16.1.9
1臺負載均衡服務器
lb01   10.0.0.5   172.16.1.5    

①. 部署web服務器
    第一個里程:安裝部署nginx軟件  注:另外兩臺web服務器也要部署相同的nginx服務
    mkdir /server/tools -p
    cd /server/tools
    wget http://nginx.org/download/nginx-1.12.2.tar.gz
    tar xf nginx-1.12.2.tar.gz
    yum install -y pcre-devel openssl-devel
    useradd -M -s /sbin/nologin www
    cd nginx-1.12.2
    ./configure --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
    make && make install
    ln -s /application/nginx-1.12.2 /application/nginx
    /application/nginx/sbin/nginx
    netstat -lntup|grep nginx

   第二個里程:編輯nginx配置文件
   server {
       listen       80;
       server_name  www.etiantian.org;
       root   html/www;
       index  index.html index.htm;
   }
   server {
       listen       80;
       server_name  bbs.etiantian.org;
       root   html/bbs;
       index  index.html index.htm;
   }
   scp -rp /application/nginx/conf/nginx.conf 172.16.1.8:/application/nginx/conf/
   scp -rp /application/nginx/conf/nginx.conf 172.16.1.8:/application/nginx/conf/

   第三里程:創建模擬測試環境
   mkdir /application/nginx/html/{www,bbs} -p
   for name in www bbs;do echo "$(hostname) $name.etiantian.org" >/application/nginx/html/$name/oldboy.html;done
   for name in www bbs;do cat /application/nginx/html/$name/oldboy.html;done

   第四里程:在負載均衡服務器上,進行測試訪問
   curl -H host:www.etiantian.org 10.0.0.7/oldboy.html
   web01 www.etiantian.org
   curl -H host:bbs.etiantian.org 10.0.0.7/oldboy.html
   web01 bbs.etiantian.org
   curl -H host:www.etiantian.org 10.0.0.8/oldboy.html
   web02 www.etiantian.org
   curl -H host:bbs.etiantian.org 10.0.0.8/oldboy.html
   web02 bbs.etiantian.org
   curl -H host:www.etiantian.org 10.0.0.9/oldboy.html
   web03 www.etiantian.org
   curl -H host:bbs.etiantian.org 10.0.0.9/oldboy.html
   web03 bbs.etiantian.org

②. 部署負載均衡服務器
    第一個里程:安裝部署nginx軟件
    mkdir /server/tools -p
    cd /server/tools
    wget http://nginx.org/download/nginx-1.12.2.tar.gz
    tar xf nginx-1.12.2.tar.gz
    yum install -y pcre-devel openssl-devel
    useradd -M -s /sbin/nologin www
    cd nginx-1.12.2
    ./configure --prefix=/application/nginx-1.12.2 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module
    make && make install
    ln -s /application/nginx-1.12.2 /application/nginx
    /application/nginx/sbin/nginx
    netstat -lntup|grep nginx

    第二個里程:編寫nginx反向代理配置文件
    grep -Ev "#|^$" nginx.conf.default >nginx.conf

    官方鏈接:http://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream
    Syntax: upstream name { ... }
    Default:    —
    Context:    http
    eg:
    upstream oldboy {
       server 10.0.0.7:80;
       server 10.0.0.8:80;
       server 10.0.0.9:80;
    }
    說明:upstream模塊就類似定一個一個地址池或者說定一個web服務器組

    官方鏈接:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass
    Syntax: proxy_pass URL;
    Default:    —
    Context:    location, if in location, limit_except
    eg:
    location / {
       proxy_pass http://oldboy;
    }
    說明:proxy_pass主要用於進行拋送用戶訪問請求給upstream模塊中的相應節點服務器

    worker_processes  1;
    events {
        worker_connections  1024;
    }
    http {
        include       mime.types;
        default_type  application/octet-stream;
        sendfile        on;
        keepalive_timeout  65;
        upstream oldboy {
            server 10.0.0.7:80;
            server 10.0.0.8:80;
            server 10.0.0.9:80;
        }
        server {
            listen       80;
            server_name  localhost;
            root   html;
            index  index.html index.htm;
          location / {
            proxy_pass http://oldboy;
           }   
        }
    }

    /application/nginx/sbin/nginx -t
    /application/nginx/sbin/nginx -s reload

    第三個里程:進行訪問負載均衡服務器測試
    1)利用瀏覽器進行測試
       進行hosts解析
       http://www.etiantian.org/oldboy.html  <--利用ctrl+F5刷新測試,檢查是否進行負載調度
    2)利用curl命令進行測試
       [root@lb01 conf]# curl -H host:www.etiantian.org 10.0.0.5/oldboy.html
       web01 www.etiantian.org
       [root@lb01 conf]# curl -H host:www.etiantian.org 10.0.0.5/oldboy.html
       web02 www.etiantian.org
       [root@lb01 conf]# curl -H host:www.etiantian.org 10.0.0.5/oldboy.html
       web03 www.etiantian.org
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章