使用nginx进行负载均衡配置

原创
2021/01/21 16:05
阅读数 188

在日常项目中,在进行一个服务多台机器部署时,会碰到一个域名或访问地址对外,但是实际上内部N台服务器的情况。这时可以采用简单的负载均衡配置,以nginx为例。 具体配置如下: `upstream mark-server{ server localhost:8081 weight=1; server localhost:8082 weight=2; server xxxxx; }

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;

    #access_log  logs/host.access.log  main;

    location / {
       proxy_pass http://mark-server;
       proxy_redirect default;
    }

}`

其中,upstream节点是定义负载均衡时的服务器列表,在该例中有两台服务器,分别对应了不同的权重,这样每当请求过来的时候,服务器会通过hash的方式来决定访问哪一台服务器。 另外要注意的一点就是,proxy_pass的配置,此处对应upstream后面的名称。 在upstream节点还可以指定具体负载均衡的策略,如ip_hash、least_conn、fair等,具体如: upstream mark-server{ ip_hash; server localhost:8081 weight=1; server localhost:8082 weight=2; }

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部