Nginx 负载均衡配置

配置

参考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,这在使用阿里云的时候要注意一下。


参考网站:

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容