Nginx 負(fù)載均衡配置

配置

參考Nginx官網(wǎng),最簡單的負(fù)載均衡配置如下:

http {
    upstream myapp1 {
        server srv1.example.com;
        server srv2.example.com;
        server srv3.example.com;
    }

    server {
        listen 80;

        location / {
            proxy_pass http://myapp1;
        }
    }
}

Nginx支持三種類型的負(fù)載均衡實(shí)現(xiàn)算法:

round-robin — requests to the application servers are distributed in a round-robin fashion,

least-connected — next request is assigned to the server with the least number of active connections,

ip-hash — a hash-function is used to determine what server should be selected for the next request (based on the client’s IP address).

其對應(yīng)的配置如下:

//Least connected load balancing
upstream myapp1 {
    least_conn;
    server srv1.example.com;
    server srv2.example.com;
    server srv3.example.com;
}
    
//Session persistence
upstream myapp1 {
    ip_hash;
    server srv1.example.com;
    server srv2.example.com;
    server srv3.example.com;
}

//Weighted load balancing
upstream myapp1 {
    server srv1.example.com weight=3;
    server srv2.example.com;
    server srv3.example.com;
}


注意事項(xiàng)

1、負(fù)載均衡服務(wù)器的配置文件,如果是再原來的/etc/nginx/nginx.conf文件上進(jìn)行修改的話,需要將include /etc/nginx/sites-enabled/*;這句話給注釋掉,否則會返回負(fù)載均衡服務(wù)器的應(yīng)用響應(yīng)。

2、修改配置文件后,記得重啟nginx

3、還有一點(diǎn),配置文件的ip地址,可以是內(nèi)網(wǎng)ip也可以是公網(wǎng)ip,這在使用阿里云的時候要注意一下。


參考網(wǎng)站:

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容