nginx四層、七層負載均衡配置示例


所謂四層就是基於IP+端口的負載均衡,通過虛擬IP+端口接收請求,然后再分配到真實的服務器;七層通過虛擬的URL或主機名接收請求,然后再分配到真實的服務器七層就是基於URL等應用層信息的負載均衡。

七層負載

# 定義
upstream phpserver {
        server192.168.2.3;
        server192.168.2.4;
        }
upstream htmlserver {
        server192.168.2.1;
        server192.168.2.2;
    }
# 引用
location / {
    root  /usr/share/nginx/html;
    index  index.html index.htm;
    if ($request_uri ~*\.html$){
        proxy_pass http://htmlserver;
    }
    if ($request_uri~* \.php$){
        proxy_pass http://phpserver;
    }
}

四層負載

stream {
    log_format  proxy '$remote_addr $remote_port - [$time_local] $status $protocol '
                  '"$upstream_addr" "$upstream_bytes_sent" "$upstream_connect_time"' ;
    access_log /var/log/nginx/proxy.log proxy;
    upstream lb {
            server 172.16.1.5:80 weight=5 max_fails=3 fail_timeout=30s;
            server 172.16.1.6:80 weight=5 max_fails=3 fail_timeout=30s;
    }

    server {
            listen 80;
            proxy_connect_timeout 3s;
            proxy_timeout 3s;
            proxy_pass lb;
    }
}


免責聲明!

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



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