nginx負載均衡簡單示例


一、實驗環境

  主機名與IP

    3台VM虛擬機,1台做負載均衡,2台做RS。

HOSTNAME IP 說明
lb01 192.168.5.210 主負載均衡器
web01 192.168.5.212 web01服務器
web02 192.168.5.213 web02服務器

  軟件:

    系統:CentOS 6.9 x86_64

    軟件:nginx-1.15.2.tar.gz(http://nginx.org/download/nginx-1.15.2.tar.gz

 二、安裝Nginx軟件

  3台服務器均安裝Nginx。

  1、安裝依賴包

yum -y install openssl openssl-devel pcre pcre-devel

  2、安裝nginx軟件包

useradd nginx -s /sbin/nologin -M
tar zxvf nginx-1.15.2.tar.gz
./configure --user=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
make
make install

三、配置一個虛擬主機

  1、配置明細

    在兩台web服務器上操作,配置如下:

[root@web01 nginx-1.15.2]# cat /usr/local/nginx/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
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;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  www.test.com;
        location / {
            root   html/test;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

   2、創建測試文件

mkdir /usr/local/nginx/html/test
echo "www.test.com 212" > /usr/local/nginx/html/test/index.html

  3、測試結果

    在windows客戶端測試,需先開通服務器防火牆的80端口,或關閉防火牆。

四、負載均衡配置

  在lb01服務器上操作,配置如下:

[root@lb01 nginx-1.15.2]# cat /usr/local/nginx/conf/nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
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;
    keepalive_timeout  65;
    upstream test_server_pools { server 192.168.5.212:80 weight=1; server 192.168.5.213:80 weight=1; }     server {
        listen       80;
        server_name  www.test.com;
        location / {
            proxy_pass http://test_server_pools;
     } error_page
500 502 503 504 /50x.html; location = /50x.html { root html; } } }

  參數說明:

  • upstream:Nginx負載均衡依賴於 ngx_http_upstream_module 模塊,ngx_http_upstream_module 允許Nginx定義一組或多組節點服務器組,使用時可以通過 proxy_pass代理方式把網站的請求放送到事先定義好的對應upstream組中的服務器。

      官方示例:

upstream backend {
    server backend1.example.com weight=5;
    server 127.0.0.1:8080       max_fails=3 fail_timeout=30s;
    server unix:/tmp/backend3;

    server backup1.example.com  backup;
}

      server的參數說明:

upstream 模塊內參數 參數說明
server 127.0.0.1:8080 可以是IP或域名,默認端口80,高並發場景下,IP可換成域名,通過DNS做負載均衡
weight=1 代表服務器權重,默認值是1,數字越大表示接受的請求比例越大
max_fails=1 nginx嘗試連接后端主機的次數,默認值是1,連續嘗試失敗后,會將請求轉發給后端正常的服務器
backup 標志此服務器作為備份服務器,主服務器全部宕機時,向備份服務器轉發請求;負載調度算法使用ip_hash時不可用
fail_timeout=10s 定義連接后端服務器失敗之后,距離下次檢查的時間,默認值是10s
down

標志服務器永遠不可用

 

  • proxy_pass:proxy_pass指令屬於 ngx_http_proxy_module 模塊;在實際的反向代理工作中,把接收到的符合 location 匹配的URI請求轉發給定義好的 upstream 節點池。
Nginx 反向代理重要參數 參數說明
proxy_pass http://server_pools; 把請求轉發至定義好的服務器池
proxy_set_header Host $Host 向后端服務器發送請求時加入 host 字段信息,可識別代理的哪個虛擬主機
proxy_set_header X-Forwarded-For $remote_addr 向后端服務器發送請求時加入 X-Forwarded-For 字段信息,用於后端服務器接收記錄真實的用戶IP,而非代理服務器IP

  可把參數寫成一個文件,使用 include 包含,看起來更規范。

location / {
     proxy_pass http://test_server_pools;
     include proxy.conf;    
}
[root@lb01 conf]# cat proxy.conf
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_connect_timeout 60;
proxy_send_timeout 60;
proxy_read_timeout 60;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

 

五、測試

  1、修改hosts地址進行測試,Windows系統hosts文件路徑:C:\Windows\System32\drivers\etc

192.168.5.210    www.test.com

  2、瀏覽器端采用無緩存刷新頁面,請求配均勻的分配到后端服務器。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM