Nginx 负载均衡配置

2022/7/4 5:20:11

本文主要是介绍Nginx 负载均衡配置,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

#定义负载均衡设备的 Ip及设备状态

在需要使用负载的Server节点下添加

proxy_pass http://myServer;

upstream 每个设备的状态:

down 表示单前的server暂时不参与负载
weight  默认为1.weight越大,负载的权重就越大。
max_fails :允许请求失败的次数默认为1.当超过最大次数时,返回proxy_next_upstream 模块定义的错误
fail_timeout:max_fails 次失败后,暂停的时间。
backup: 其它所有的非backup机器down或者忙的时候,请求backup机器。所以这台机器压力会最轻。

upstream a.com{
# hash $cookie_jsessionid;
 hash $remote_addr consistent;
 server 192.168.142.128:8080 weight=3;
 server 192.168.142.129:80 weight=10;
# hash $cookie_jsessionid;
# server 192.168.142.130:80;
# ip_hash;
# hash_again 1;
# consistent_hash $remote_addr:可以根据客户端ip映射
# consistent_hash $request_uri: 根据客户端请求的uri映射
# consistent_hash $args:根据客户端携带的参数进行映射
 }
server{ 
 listen 80; 
 server_name a.com;
 location / { 
     index index.html; 
     root /usr/html; 
     proxy_pass http://a.com;
     proxy_set_header Host $host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 }
}

 



这篇关于Nginx 负载均衡配置的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程