美文网首页
Nginx 负载均衡配置

Nginx 负载均衡配置

作者: zshanjun | 来源:发表于2017-04-18 19:35 被阅读130次

配置

参考Nginx官网,最简单的负载均衡配置如下:

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

    server {
        listen 80;

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

Nginx支持三种类型的负载均衡实现算法:

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).

其对应的配置如下:

//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;
}


注意事项

1、负载均衡服务器的配置文件,如果是再原来的/etc/nginx/nginx.conf文件上进行修改的话,需要将include /etc/nginx/sites-enabled/*;这句话给注释掉,否则会返回负载均衡服务器的应用响应。

2、修改配置文件后,记得重启nginx

3、还有一点,配置文件的ip地址,可以是内网ip也可以是公网ip,这在使用阿里云的时候要注意一下。


参考网站:

相关文章

网友评论

      本文标题:Nginx 负载均衡配置

      本文链接:https://www.haomeiwen.com/subject/uzsqzttx.html