1. 程式人生 > >大資料叢集架之——nginx 反向代理的安裝配置文件。

大資料叢集架之——nginx 反向代理的安裝配置文件。

二、Nginx安裝配置


    1.安裝gcc
    方式1 - yum線上安裝:
        //yum install gcc
        yum install gcc-c++ 
    方式2 - rpm離線安裝:
        檢查是否安裝過gcc
            gcc -v 
        如果能列印gcc版本則證明系統已經安裝過gcc,不需要再安裝,跳過此步驟

        下載安裝包
            http://vault.centos.org/6.5/os/x86_64/Packages/
            ppl-0.10.2-11.el6.x86_64.rpm
            cloog-ppl-0.15.7-1.2.el6.x86_64.rpm
            mpfr-2.4.1-6.el6.x86_64.rpm
            cpp-4.4.7-4.el6.x86_64.rpm
            kernel-headers-2.6.32-431.el6.x86_64.rpm
            glibc-headers-2.12-1.132.el6.x86_64.rpm
            glibc-devel-2.12-1.132.el6.x86_64.rpm
            gcc-4.4.7-4.el6.x86_64.rpm
            libstdc++-devel-4.4.7-4.el6.x86_64.rpm
            gcc-c++-4.4.7-4.el6.x86_64.rpm

        上傳到linux中

        按照如上順序安裝
            rpm -ivh xxx.rpm

    2.安裝pcre
        方式1 - yum線上安裝:
            yum -y install gcc pcre-devel openssl openssl-devel
        方式2 - 原始碼離線安裝:
            下載pcre安裝包
                https://sourceforge.net/projects/pcre/files/pcre/
                pcre-8.39.gz
                
            上傳到linux
                
            解壓
                tar -zxvf pcre-8.39.gz
            進入pcre目錄,執行
                ./configure
            編譯
                make
            安裝
                make install

    3.安裝zlib-devel  
        rpm -ivh zlib-devel-1.2.3-29.el6.x86_64.rpm

    4.安裝nginx
            下載ngnix 原始碼包
                http://nginx.org/download
                nginx-1.9.9.tar.gz
                
            上傳到linux

            解壓
                tar -zxvf nginx-1.9.9.tar.gz

            進入資料夾
                
            編譯安裝
                ./configure --prefix=安裝位置 --with-pcre=/root/work/pcre-8.39 #預設位置 /usr/local/nginx
                make
                make install

    5.配置nginx
        在nginx下配置conf/nginx.conf

        upstream backend {
            #ip_hash;
            server 123.56.71.150:8080;
            server 123.56.71.151:8080;
            server 123.56.71.152:8080;
        }
        ----------------------------------------
        upstream backend {
            #對應的你的日誌伺服器的ip地址
server mini08:8080;
server mini09:8080;
        }
         -----------------------------------------
        
        server {  
            listen       80;  
            server_name  123.56.71.150;  #這個IP地址就是你的nginx所在的IP地址

            location / {     
                proxy_connect_timeout   3;     
                proxy_send_timeout      30;     
                proxy_read_timeout      30;     
                proxy_pass http://backend;   #這裡面的配置其實就只要配置這個就可以了。   
            }

        }
        ==================================================
        1、輪詢
            輪詢即Round Robin,根據Nginx配置檔案中的順序,依次把客戶端的Web請求分發到不同的後端伺服器。
            配置的例子如下:
                http{ 
                 upstream sampleapp { 
                   server <<dns entry or IP Address(optional with port)>>; 
                   server <<another dns entry or IP Address(optional with port)>>; 
                 } 
                 .... 
                 server{ 
                   listen 80; 
                   ... 
                   location / { 
                    proxy_pass http://sampleapp; 
                   }  
                 } 
            上面只有1個DNS入口被插入到upstream節,即sampleapp,同樣也在後面的proxy_pass節重新提到。
        2、最少連線
            Web請求會被轉發到連線數最少的伺服器上。
            配置的例子如下:
                http{ 
                  upstream sampleapp { 
                    least_conn; 
                    server <<dns entry or IP Address(optional with port)>>; 
                    server <<another dns entry or IP Address(optional with port)>>; 
                  } 
                  .... 
                  server{ 
                    listen 80; 
                    ... 
                    location / { 
                     proxy_pass http://sampleapp; 
                    }  
                  } 
            上面的例子只是在upstream節添加了least_conn配置。其它的配置同輪詢配置。
        3、IP地址雜湊
            前述的兩種負載均衡方案中,同一客戶端連續的Web請求可能會被分發到不同的後端伺服器進行處理,因此如果涉及到會話Session,那麼會話會比較複雜。常見的是基於資料庫的會話持久化。要克服上面的難題,可以使用基於IP地址雜湊的負載均衡方案。這樣的話,同一客戶端連續的Web請求都會被分發到同一伺服器進行處理。
            配置的例子如下:
                http{ 
                  upstream sampleapp { 
                    ip_hash; 
                    server <<dns entry or IP Address(optional with port)>>; 
                    server <<another dns entry or IP Address(optional with port)>>; 
                  } 
                  .... 
                  server{ 
                    listen 80; 
                    ... 
                    location / { 
                     proxy_pass http://sampleapp; 
                    }  
                  } 
                上面的例子只是在upstream節添加了ip_hash配置。其它的配置同輪詢配置。
        4、基於權重的負載均衡
            基於權重的負載均衡即Weighted Load Balancing,這種方式下,我們可以配置Nginx把請求更多地分發到高配置的後端伺服器上,把相對較少的請求分發到低配伺服器。
            配置的例子如下:
                http{ 
                  upstream sampleapp { 
                    server <<dns entry or IP Address(optional with port)>> weight=2; 
                    server <<another dns entry or IP Address(optional with port)>>; 
                  } 
                  .... 
                  server{ 
                    listen 80; 
                    ... 
                    location / { 
                     proxy_pass http://sampleapp; 
                    } 
                 } 
                上面的例子在伺服器地址和埠後weight=2的配置,這意味著,每接收到3個請求,前2個請求會被分發到第一個伺服器,第3個請求會分發到第二個伺服器,其它的配置同輪詢配置。
                還要說明一點,基於權重的負載均衡和基於IP地址雜湊的負載均衡可以組合在一起使用。
        ==================================================

    6.啟動和關閉nginx
        nginx 啟動

        nginx -s reload  :修改配置後重新載入生效
        nginx -s reopen  :重新開啟日誌檔案
        nginx -t -c /path/to/nginx.conf 測試nginx配置檔案是否正確

        關閉nginx:
        nginx -s stop  :快速停止nginx
                 quit  :完整有序的停止nginx

        其他的停止nginx 方式:

        ps -ef | grep nginx

        kill -QUIT 主程序號     :從容停止Nginx
        kill -TERM 主程序號     :快速停止Nginx
        pkill -9 nginx          :強制停止Nginx

nginx注意:如果你在linux環境下面是普通使用者的話,需要需要nginx的訪問埠,預設是80埠,要修改到1024之前的埠。因為linux系統規範1024及以下的埠需要root許可權來訪問的。