配置
參考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)站: