1. 程式人生 > >Nginx七層負載均衡

Nginx七層負載均衡

Nginx七層負載均衡

先利用ngx_http_upstream_module模組定義一個後端伺服器組
在利用ngx_http_proxy_module模組中的 proxy_pass指令進行代理轉發到定義的後端伺服器組

ngx_http_upstream_module模組常用配置項可以參考這個文章
Nginx利用ngx_http_upstream_module模組定義後端伺服器組

示例

環境:
後端伺服器:
192.168.253.129 (提供了web服務)
192.168.253.139(提供了web服務)
代理伺服器:
192.168.253.128

代理伺服器的主配置檔案

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types; 
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream backend {  #定義後端伺服器組
          server 192.168.253.129 weight=2;
          server 192.168.253.139 weight=5;
    }       
    server {
        listen       80;
        server_name  localhost;
        location / {
              proxy_pass http://backend;  #將請求代理到backend這個後端伺服器組中
        }
 
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}