Nginx負載平衡初步搭建

來源:互聯網
上載者:User

標籤:linux   負載平衡   nginx   

      負載平衡(Load Balance):將使用者的訪問分攤到多個伺服器上,負載平衡的前提就是要有多台伺服器才能實現,也就是兩台以上即可。

        負載平衡也是反向 Proxy所能實現的一個重要功能,區分於正向 Proxy:

反向 Proxy:以Proxy 伺服器來接受internet上的串連請求,然後將請求轉寄給內部網路上的伺服器,並將從伺服器上得到的結果返回給internet上請求串連的用戶端。

正向 Proxy:一個位於用戶端和原始伺服器(originserver)之間的伺服器,為了從原始伺服器取得內容,用戶端向代理髮送一個請求並指定目標(原始伺服器),然後代理向原始伺服器轉交請求並將獲得的內容返回給用戶端。

        nginx的負載平衡主要通過 upstream(ngx_http_upstream_module)和http_proxy(ngx_http_proxy_module)這兩個模組完成。

  1. upstream(ngx_http_upstream_module)

定義演算法,設定負載平衡的伺服器列表。

官方文檔:http://nginx.org/en/docs/http/ngx_http_upstream_module.html

用法:

upstream backend                //upstream是定義在server{ }之外的,不能定義在server{ }內部。{    server    backend1.example.com       weight=5;    server    backend2.example.com:8080;    server    unix:/tmp/backend3;     server    backup1.example.com:8080   backup;    server    backup2.example.com:8080   backup;}

Nginx的負載平衡模組目前支援4種調度演算法,下面進行分別介紹,其中後兩項屬於第三方調度演算法。  

輪詢(預設):每個請求按時間順序逐一分配到不同的後端伺服器,如果後端某台伺服器宕機,故障系統被自動剔除,使使用者訪問不受影響。Weight 指定輪詢權值,Weight值越大,分配到的訪問機率越高,主要用於後端每個伺服器效能不均的情況下。

ip_hash:每個請求按訪問IP的hash結果分配,這樣來自同一個IP的訪客固定訪問一個後端伺服器,有效解決了動態網頁存在的session共用問題。

fair:這是比上面兩個更加智能的負載平衡演算法。此種演算法可以依據頁面大小和載入時間長短智能地進行負載平衡,也就是根據後端伺服器的回應時間來分配請求,回應時間短的優先分配。Nginx本身是不支援fair的,如果需要使用這種調度演算法,必須下載Nginx的upstream_fair模組。

url_hash:此方法按訪問url的hash結果來分配請求,使每個url定向到同一個後端伺服器,可以進一步提高後端快取服務器的效率。Nginx本身是不支援url_hash的,如果需要使用這種調度演算法,必須安裝Nginx 的hash軟體包。

  2. http_proxy(ngx_http_proxy_module)

官方文檔:http://nginx.org/en/docs/http/ngx_http_proxy_module.html

用法:

location / {   proxy_pass      http://localhost:8000;   proxy_set_header Host      $host;   proxy_set_header X-Real-IP $remote_addr;}

此模組主要是引用server{}外的upstream定義。


下面由實際情況來初步配置nginx的負載平衡:

LB機:  192.168.73.160

lamp機: 192.168.73.155

lnmp機: 192.168.73.156

註:為了學習才分別搭建apache和nginx的web伺服器,正常生產一般只配一種。

  1. LB機安裝nginx

# yum install -y pcre-devel pcre openssl-devel   安裝必須的包# tar zxvf nginx-1.6.3.tar.gz# useradd -M -s /sbin/nologin nginx# ./configure --prefix=/application/nginx --user=nginx--group=nginx --with-http_ssl_module --with-http_stub_status_module# make&&make install

編輯nginx設定檔:

worker_processes  1;error_log logs/error.log error;events {   worker_connections  1024;        }http {   include       mime.types;   default_type application/octet-stream;   sendfile        on;   keepalive_timeout  65;   log_format  main  ‘$remote_addr - $remote_user [$time_local]"$request" ‘             ‘$status $body_bytes_sent"$http_referer" ‘             ‘"$http_user_agent" "$http_x_forwarded_for"‘;upstream bbs_server_pools{        server 192.168.73.155:80;        server 192.168.73.156:80;}   ##nginx vhosts config;  ##include extra/lb_www.conf;    include extra/lb_bbs.conf;  ##include extra/lb_blog.conf;    }

定義好之後,在extra/lb_bbs.conf中引用

# vim/application/nginx/conf/extra/lb_bbs.conf   server {       listen       80;        server_name bbs.etiantian.org;       location / {           proxy_pass http://bbs_server_pools;                   }              }

驗證:

LB主機配置完成,reload

# /etc/init.d/nginx reloadnginx: the configuration file /application/nginx/conf/nginx.conf syntax is oknginx: configuration file /application/nginx/conf/nginx.conf test is successful重新載入 nginx:                                           [確定]


在lamp主機編輯首頁檔案:

# vim /var/html/www/index.php<?php        echo "apache web ";?>

在lnmp主機編輯首頁檔案:

# vim /application/nginx/html/www/index.php <?php        echo "nginx web ";?>

使用其它同網段的主機訪問:

[[email protected] ~]# for n in `seq 10`;do curl 192.168.73.160;sleep 1;donenginx web apache web nginx web apache web nginx web apache web nginx web apache web nginx web apache web

簡單的輪詢負載平衡配置成功。

註:以上配置即可實現負載平衡功能,但請求bbs網站,返回的卻是www,原因是客戶請求BBS的header沒有被LB負載平衡器傳給web伺服器,需要server標籤添加參數:

vim/application/nginx/conf/extra/lb_bbs.conf

proxy_set_header Host $host;

此時查看web伺服器日誌,發現訪問地址顯示的是LB負載平衡的地址,我們需要的是使用者訪問源地址,需要在LB機nginx的server標籤中添加:

vim/application/nginx/conf/extra/lb_bbs.conf

proxy_set_header X-Forwarded-For    $remote_addr;

同時web伺服器的日誌格式也需要修改:

nginx伺服器:vimconf/nginx.conf

log_format main  ‘$remote_addr - $remote_user[$time_local] "$request" ‘

                      ‘$status $body_bytes_sent"$http_referer" ‘

                     ‘"$http_user_agent" "$http_x_forwarded_for"‘;

apache伺服器:vim /application/apache2/conf/httpd.conf

LogFormat "%h %l %u %t\"%r\" %>s %b \"%{X-Forwarded-For}i\""common

重啟nginx和apache即可




本文出自 “SweetSmile” 部落格,請務必保留此出處http://daizhancheng.blog.51cto.com/9708457/1766434

Nginx負載平衡初步搭建

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.